Search code examples
javaspringspring-bootspring-transactionstransactional

Spring Change Transaction Isolation Mode


I want to change the transaction isolation mode to serializable with Spring annotation but I get an exception :

@Transactional(isolation = Isolation.SERIALIZABLE)

org.springframework.transaction.InvalidIsolationLevelException:
JtaTransactionManager does not support custom isolation levels by default - switch 'allowCustomIsolationLevels' to 'true'

I'm using the Atomikos transaction manager.

Is it possible to do it with the Spring Boot application.properties file? Otherwise how do it in Java (I don't want to use an xml configuration)?

Thanks


Solution

  • I have found this solution for resolving the exception (IllegalStateException: No JTA UserTransaction available - specify either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName') :

    @Bean(initMethod = "init", destroyMethod = "close")
    public UserTransactionManager atomikosTransactionManager() {
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(false);
    
        return userTransactionManager;
    }
    
    @Bean
    public UserTransaction atomikosUserTransaction() throws SystemException {
        UserTransactionImp userTransaction = new UserTransactionImp();
        userTransaction.setTransactionTimeout(300);
    
        return userTransaction;
    }
    
    @Bean
    public PlatformTransactionManager platformTransactionManager() throws SystemException {
        JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
    
        jtaTransactionManager.setTransactionManager(atomikosTransactionManager());
        jtaTransactionManager.setUserTransaction(atomikosUserTransaction());
        jtaTransactionManager.setAllowCustomIsolationLevels(true);
    
        return jtaTransactionManager;
    }
    

    Thanks for your help.

    Has anyone a better solution?