@Override public synchronized void refresh() { if( configuration != null ) { configuration = null; uow.discard(); uow = null; } }
@Override public void tearDown() throws Exception { if( this.unitOfWork != null ) { this.unitOfWork.discard(); this.unitOfWork = null; } super.tearDown(); } }
/** * Discard unit of work if the discard policy match. * * @param aMethod The invoked method. This argument must not be {@code null}. * @param aUnitOfWork The current unit of work. This argument must not be {@code null}. * @param aThrowable The exception thrown. This argument must not be {@code null}. */ protected void discardIfRequired( Method aMethod, UnitOfWork aUnitOfWork, Throwable aThrowable ) { UnitOfWorkDiscardOn discardPolicy = aMethod.getAnnotation( UnitOfWorkDiscardOn.class ); Class<?>[] discardClasses; if( discardPolicy != null ) { discardClasses = discardPolicy.value(); } else { discardClasses = DEFAULT_DISCARD_CLASSES; } Class<? extends Throwable> aThrowableClass = aThrowable.getClass(); for( Class<?> discardClass : discardClasses ) { if( discardClass.isAssignableFrom( aThrowableClass ) ) { aUnitOfWork.discard(); } } } }
@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(); } }
@Override public Object getAttribute( String name ) throws AttributeNotFoundException, MBeanException, ReflectionException { UnitOfWork uow = module.newUnitOfWork(); try { EntityComposite configuration = uow.get( EntityComposite.class, identity ); AssociationStateHolder state = spi.stateOf( configuration ); AccessibleObject accessor = propertyNames.get( name ); Property<Object> property = state.propertyFor( accessor ); return property.get(); } catch ( Exception ex ) { throw new ReflectionException( ex, "Could not get attribute " + name ); } finally { uow.discard(); } }
} catch ( NoSuchEntityException ex ) { LOGGER.debug( "404: No Entity found for the requested identity ('{}')", ex.identity().identity(), ex ); uow.discard(); response.setStatus( Status.CLIENT_ERROR_NOT_FOUND ); response.setEntity( new ExceptionRepresentation( ex ) ); } catch ( UnitOfWorkException ex ) { LOGGER.warn( "406: {}", ex.getMessage(), ex ); uow.discard(); response.setStatus( Status.CLIENT_ERROR_NOT_ACCEPTABLE ); response.setEntity( new ExceptionRepresentation( ex ) ); uow.discard(); response.setStatus( Status.CLIENT_ERROR_CONFLICT ); response.setEntity( new ExceptionRepresentation( ex ) ); uow.discard(); response.setStatus( Status.CLIENT_ERROR_NOT_ACCEPTABLE ); response.setEntity( new ExceptionRepresentation( ex ) ); } catch ( WrongParametersException ex ) { LOGGER.warn( "400: {}", ex.getMessage(), ex ); uow.discard(); response.setStatus( Status.CLIENT_ERROR_BAD_REQUEST ); response.setEntity( new ExceptionRepresentation( ex ) );
@AfterClass public void tearDown() throws Exception { if( module != null && module.isUnitOfWorkActive() ) { while( module.isUnitOfWorkActive() ) { UnitOfWork uow = module.currentUnitOfWork(); if( uow.isOpen() ) { uow.discard(); } else { throw new InternalError( "I have seen a case where a UoW is on the stack, but not opened. First is" + uow .usecase() .name() ); } } new Exception( "UnitOfWork not properly cleaned up" ).printStackTrace(); } if( application != null ) { application.passivate(); } } }
PropertyMapper.map( asStream, configBuilder.instance() ); } catch ( IOException e1 ) { uow.discard(); InstantiationException exception = new InstantiationException( "Could not read underlying Properties file." ); exception.initCause( e1 );
@After @Override public void tearDown() throws Exception { if( module != null && module.isUnitOfWorkActive() ) { while( module.isUnitOfWorkActive() ) { UnitOfWork uow = module.currentUnitOfWork(); if( uow.isOpen() ) { System.err.println( "UnitOfWork not cleaned up:" + uow.usecase().name() ); uow.discard(); } else { throw new InternalError( "I have seen a case where a UoW is on the stack, but not opened. First is: " + uow .usecase() .name() ); } } new Exception( "UnitOfWork not properly cleaned up" ).printStackTrace(); } super.tearDown(); } }
@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 ); } } } }
module.currentUnitOfWork().discard(); Thread.sleep( initialDelay + retry * delayFactor ); retry++;
@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(); } }
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 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(); } }
} catch (Exception e) uow.discard(); if (e instanceof EventReplayException) throw ((EventReplayException) e);