繁体   English   中英

使用SpringBoot处理事务隔离级别

[英]Dealing with Transaction isolation level using SpringBoot

我正在使用Spring-Boot应用程序并使用Spring Boot版本1.5.9。

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

@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定义,并且我不确定如何实现此目标。

任何帮助将不胜感激!

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

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

它将从路径(其中根是项目中的资源目录)导入XML文件。

根据我在Spring文档中的了解 ,您可能希望研究使用DataSourceTransactionManager还是使用XML文件(如我提到的那样),因为根据该文档(与错误消息状态非常相似,但至少可以选择使用设置)

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

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

希望这可以帮助。

暂无
暂无

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

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