【问题标题】:Dealing with Transaction isolation level using SpringBoot使用 Spring Boot 处理事务隔离级别
【发布时间】:2019-03-17 21:17:18
【问题描述】:

我正在开发一个 Spring-Boot 应用程序并使用 Spring Boot 1.5.9 版。

我有一个包含一组数据库插入的付款转移方法,我想将其设置为带有锁定的事务性以避免双重支出。所以我需要将隔离级别设置为可序列化。 这就是我所做的:

@Transactional(isolation = Isolation.SERIALIZABLE)
public void transfer() {
...
}

问题是调用此方法时出现以下异常:

{
    "timestamp": 1539357851437,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.transaction.InvalidIsolationLevelException",
    "message": "JtaTransactionManager does not support custom isolation levels by default - switch 'allowCustomIsolationLevels' to 'true'",
    "path": "app/transfer"
}

我一般是 Spring 新手,SpringBoot 似乎没有 XML 配置,我可以覆盖它以将 JtaTransactionManager bean 的 allowCustomIsolationLevels 设置为 true。但相反,我应该使用 Java 注释和 @Bean 定义,但我不确定如何实现我的目标。

任何帮助将不胜感激!

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    如果您想使用 XML 文件进行配置,请在具有 @SprintBootApplication 注释的主类中添加以下内容:

    @ImportResource("classpath:/static/context/name-of-context-file.xml")

    这将从根是项目中的资源目录的路径导入 XML 文件。

    据我从Spring's Documentation 得知,您可能需要考虑使用 DataSourceTransactionManager 或使用我提到的 XML 文件,因为根据该文档(很像错误消息状态,但至少给出你设置的选项)

    JTA 不支持自定义隔离级别。

    我知道我链接的页面很长,但是如果您只是在页面上查找“自定义隔离”,它会将其范围缩小到三个选项,其中两个是彼此相邻的。

    希望这会有所帮助。

    【讨论】:

    • so setAllowCustomIsolationLevels(true) 表示 Spring 将通过 Spring JTA over JTA 支持自定义隔离级别?
    猜你喜欢
    • 2020-06-07
    • 2015-07-21
    • 2018-04-30
    • 1970-01-01
    • 2015-01-06
    • 2011-09-30
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多