Announcement Announcement Module
No announcement yet.
Spring Transactions with Isolation.SERIALIZABLE does not block method call Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Transactions with Isolation.SERIALIZABLE does not block method call

    Hello everybody

    I have a problem with a spring transaction. I want to make sure that the following method is not executed concurrently.
        @Transactional( propagation = Propagation.REQUIRED, isolation=Isolation.SERIALIZABLE  )    
        public int getAutoIncrementNumber()
            SimpleJdbcInsert insertActor = new SimpleJdbcInsert(
                jdbcTemplate.getDataSource() ).withTableName( 
                    "GalifaIdGenerator" ).usingGeneratedKeyColumns( "id" );
            LOGGER.debug( "START getAutoIncrementNumber()" );
            LOGGER.debug( "Transaction active=" + TransactionSynchronizationManager.isActualTransactionActive() );
            LOGGER.debug( "Isolation-Level=" + TransactionSynchronizationManager.getCurrentTransactionIsolationLevel() );
            try {
                Thread.sleep( 5000 );
            catch ( InterruptedException ex ) { }
            Date date = new Date();
            Map<String, Object> parameter = new HashMap<String, Object>(1);
            parameter.put( "name", date.toString() );
            Number newId = insertActor.executeAndReturnKey( parameter );
            LOGGER.debug( "END getAutoIncrementNumber()=" + newId.intValue() );
            return newId.intValue();

    What I want to achieve is, that a first call of the method executes the whole method and a second caller has to wait until the first call has reached the end of the method.

    The method is called by an Spring @Service bean which is defined as a singleton
    @Scope( value = "singleton" ).
    The method of the Service Bean looks as follows:
        public int getNextEvenGalifaId()
            LOGGER.debug( "START getNextEvenGalifaId()" );
            try {
                Thread.sleep( 5000 );
            catch ( InterruptedException ex ) {}
            int nextNumber = patientDAO.getAutoIncrementNumber();
            // when the returned number is odd, we simply retrieve the next$
            // auto incremented number which must be even!
            if ( ( nextNumber % 2 ) != 0 ) {
                nextNumber = patientDAO.getAutoIncrementNumber();
            LOGGER.debug( "END getNextEvenGalifaId()" );
            return nextNumber;
    According to the log output of the spring framework the transaction is successfully instantiated but a second call of the method is not blocked. The log of the spring framework can be found here:

    I added Threed.sleep() statements for testing purposes.

    How can I achieve, that the method getAutoIncrementNumber() blockes a second call while another caller executes the method?