@Override public void save() { if( uow != null ) { try { uow.complete(); uow = null; } catch( UnitOfWorkCompletionException e ) { // Should be impossible e.printStackTrace(); } configuration = null; // Force refresh } }
@Override public void log( LogType type, Composite composite, String category, String message, Serializable... params ) { UnitOfWork uow = uowf.newUnitOfWork(); try { List<Serializable> paramsList = new ArrayList<Serializable>( Arrays.asList( params ) ); createLogRecord( uow, type, composite, category, message, paramsList ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
public void log( LogType type, Composite composite, String category, String message, Serializable param1, Serializable param2 ) { UnitOfWork uow = uowf.newUnitOfWork(); try { List<Serializable> paramsList = new ArrayList<Serializable>(); paramsList.add( param1 ); paramsList.add( param2 ); createLogRecord( uow, type, composite, category, message, paramsList ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
@Override public void debug( Composite composite, String message, Serializable param1, Serializable param2 ) { UnitOfWork uow = uowf.newUnitOfWork(); try { List<Serializable> paramsList = new ArrayList<Serializable>(); paramsList.add( param1 ); paramsList.add( param2 ); createDebugRecord( uow, composite, message, paramsList ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
@Override public void debug( Composite composite, String message, Serializable param1 ) { UnitOfWork uow = uowf.newUnitOfWork(); try { List<Serializable> paramsList = new ArrayList<Serializable>(); paramsList.add( param1 ); createDebugRecord( uow, composite, message, paramsList ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
@Override public void debug( Composite composite, String message, Serializable... params ) { UnitOfWork uow = uowf.newUnitOfWork(); try { List<Serializable> paramsList = new ArrayList<Serializable>( Arrays.asList( params ) ); createDebugRecord( uow, composite, message, paramsList ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
public void log( LogType type, Composite composite, String category, String message ) { UnitOfWork uow = uowf.newUnitOfWork(); try { List<Serializable> paramsList = new ArrayList<Serializable>(); createLogRecord( uow, type, composite, category, message, paramsList ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
public void log( LogType type, Composite composite, String category, String message, Serializable param1 ) { UnitOfWork uow = uowf.newUnitOfWork(); try { List<Serializable> paramsList = new ArrayList<Serializable>(); paramsList.add( param1 ); createLogRecord( uow, type, composite, category, message, paramsList ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
@Override public void debug( Composite composite, String message ) { UnitOfWork uow = uowf.newUnitOfWork(); try { List<Serializable> paramsList = new ArrayList<Serializable>(); createDebugRecord( uow, composite, message, paramsList ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
@Override public void traceSuccess( Class compositeType, Composite object, Method method, Object[] args, Object result, long entryTime, long durationNano ) { UnitOfWork uow = uowf.newUnitOfWork(); try { createTraceRecord( uow, compositeType, object, method, args, entryTime, durationNano, null ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
@Override public void traceException( Class compositeType, Composite object, Method method, Object[] args, Throwable t, long entryTime, long durationNano ) { UnitOfWork uow = uowf.newUnitOfWork(); try { createTraceRecord( uow, compositeType, object, method, args, entryTime, durationNano, t ); uow.complete(); } catch( ConcurrentEntityModificationException e ) { // ignore for now. Perhaps discard() and try again. } catch( UnitOfWorkCompletionException e ) { // ignore for now. Perhaps discard() and try again. } }
@Override public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException { UnitOfWork unitOfWork = uowf.newUnitOfWork(); try { chain.doFilter( request, response ); if( unitOfWork.isOpen() ) { unitOfWork.complete(); } } catch( Exception e ) { unitOfWork.discard(); } }
@Override public void setAttribute( Attribute attribute ) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { UnitOfWork uow = module.newUnitOfWork(); try { EntityComposite configuration = uow.get( EntityComposite.class, identity ); AssociationStateHolder state = spi.stateOf( configuration ); AccessibleObject accessor = propertyNames.get( attribute.getName() ); Property<Object> property = state.propertyFor( accessor ); property.set( attribute.getValue() ); try { uow.complete(); } catch ( UnitOfWorkCompletionException e ) { throw new ReflectionException( e ); } } finally { uow.discard(); } }
@Test public void givenEntityIsNotModifiedWhenUnitOfWorkCompletesThenDontStoreState() throws UnitOfWorkCompletionException { TestEntity testEntity; String version; { UnitOfWork unitOfWork = module.newUnitOfWork(); EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder( TestEntity.class ); testEntity = builder.newInstance(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); version = spi.entityStateOf( testEntity ).version(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); String newVersion = spi.entityStateOf( testEntity ).version(); assertThat( "version has not changed", newVersion, equalTo( version ) ); unitOfWork.complete(); } }
@Test public void givenPropertyIsModifiedWhenUnitOfWorkCompletesThenStoreState() throws UnitOfWorkCompletionException { TestEntity testEntity; String version; { UnitOfWork unitOfWork = module.newUnitOfWork(); EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder( TestEntity.class ); testEntity = builder.newInstance(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); testEntity.name().set( "Rickard" ); version = spi.entityStateOf( testEntity ).version(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); String newVersion = spi.entityStateOf( testEntity ).version(); assertThat( "version has changed", newVersion, not( equalTo( version ) ) ); unitOfWork.complete(); } }
@Test public void givenManyAssociationIsModifiedWhenUnitOfWorkCompletesThenStoreState() throws UnitOfWorkCompletionException { TestEntity testEntity; String version; { UnitOfWork unitOfWork = module.newUnitOfWork(); EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder( TestEntity.class ); testEntity = builder.newInstance(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); testEntity.manyAssociation().add( 0, testEntity ); version = spi.entityStateOf( testEntity ).version(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); String newVersion = spi.entityStateOf( testEntity ).version(); assertThat( "version has changed", newVersion, not( equalTo( version ) ) ); unitOfWork.complete(); } }
@Override public void run() { if ( !vetoed( message ) ) { final UnitOfWork uow = uowf.newUnitOfWork(); for ( S eachSubscriber : subscribers( message.getMessageType() ) ) { try { message.deliver( eachSubscriber ); uow.complete(); } catch ( UnitOfWorkCompletionException ex ) { uow.discard(); throw new SOSFailure( "Error during message delivery", ex ); } } } }
@Test public void whenRemovedEntityThenCannotFindEntity() throws Exception { UnitOfWork unitOfWork = module.newUnitOfWork(); TestEntity newInstance = createEntity( unitOfWork ); String identity = newInstance.identity().get(); unitOfWork.complete(); // Remove entity unitOfWork = module.newUnitOfWork(); TestEntity instance = unitOfWork.get( newInstance ); unitOfWork.remove( instance ); unitOfWork.complete(); // Find entity unitOfWork = module.newUnitOfWork(); try { unitOfWork.get( TestEntity.class, identity ); fail( "Should not be able to find entity" ); } catch( NoSuchEntityException e ) { // Ok! } finally { unitOfWork.discard(); } }
private void loadSchedules() throws UnitOfWorkCompletionException { UnitOfWork uow = module.newUnitOfWork(); try { Schedules schedules = uow.get( Schedules.class, getSchedulesIdentity( me ) ); for( Schedule schedule : schedules.schedules() ) { dispatchForExecution( schedule ); } } catch( NoSuchEntityException e ) { // Create a new Schedules entity for keeping track of them all. uow.newEntity( Schedules.class, getSchedulesIdentity( me ) ); uow.complete(); } finally { if( uow.isOpen() ) { uow.discard(); } } }
@Test public void givenEntityStoredLoadedChangedWhenUnitOfWorkDiscardsThenDontStoreState() throws UnitOfWorkCompletionException { UnitOfWork unitOfWork = module.newUnitOfWork(); try { String identity = createEntity( unitOfWork ).identity().get(); unitOfWork.complete(); unitOfWork = module.newUnitOfWork(); TestEntity entity = unitOfWork.get( TestEntity.class, identity ); assertThat( entity.intValue().get(), is( 42 ) ); entity.intValue().set( 23 ); unitOfWork.discard(); unitOfWork = module.newUnitOfWork(); entity = unitOfWork.get( TestEntity.class, identity ); assertThat( entity.intValue().get(), is( 42 ) ); } finally { unitOfWork.discard(); } }