Spring Change Transaction Isolation Mode

问题描述 投票:1回答:2

我想通过Spring注释将事务隔离模式更改为serializable,但我得到一个例外:

@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
2个回答
1
投票

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

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

0
投票

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

谢谢你的帮助。

谁有更好的解决方案?

© www.soinside.com 2019 - 2024. All rights reserved.