[英]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.