【问题标题】:Spring Change Transaction Isolation ModeSpring Change 事务隔离模式
【发布时间】:2018-03-02 09:13:02
【问题描述】:

我想使用 Spring 注解将事务隔离模式更改为可序列化,但出现异常:

@Transactional(isolation = Isolation.SERIALIZABLE)

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

我正在使用 Atomikos 事务管理器。

是否可以使用 Spring Boot application.properties 文件来做到这一点?否则在Java中怎么做(我不想使用xml配置)?

谢谢

【问题讨论】:

    标签: java spring spring-boot spring-transactions transactional


    【解决方案1】:

    您可以自定义和覆盖spring boot使用的默认Jta事务管理器

    @Bean public PlatformTransactionManager platformTransactionManager() {
     JtaTransactionManager manager = new JtaTransactionManager()
       manager.setAllowCustomIsolationLevels(true);
     return manager ; }
    

    【讨论】:

    • 有了这个解决方案,我会保留一个 Atomikos 事务管理器吗?
    • 这个解决方案有一个例外:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'platformTransactionManager'Caused by: java.lang.IllegalStateException: No JTA UserTransaction available - specify either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName'你知道出了什么问题吗?
    【解决方案2】:

    我找到了解决(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;
    }
    

    感谢您的帮助。

    有没有更好的解决方案?

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 1970-01-01
      • 2020-06-07
      • 2014-03-06
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多