繁体   English   中英

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 Boot 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;
}

谢谢你的帮助。

有人有更好的解决方案吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM