Spring 更改事务隔离模式

Spring Change Transaction Isolation Mode

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

@Transactional(isolation = Isolation.SERIALIZABLE)

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

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

是否可以使用 Spring 引导 application.properties 文件来完成?否则如何在 Java 中进行(我不想使用 xml 配置)?

谢谢

您可以自定义和覆盖 spring boot

使用的默认 Jta 事务管理器
@Bean public PlatformTransactionManager platformTransactionManager() {
 JtaTransactionManager manager = new JtaTransactionManager()
   manager.setAllowCustomIsolationLevels(true);
 return manager ; }

我找到了解决异常的解决方案 (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;
}

感谢您的帮助。

有没有更好的解决方案?