簡體   English   中英

無法為事務嵌套異常打開 JPA EntityManager 為 java.lang.IllegalStateException:已為鍵綁定到線程的值

[英]Could not open JPA EntityManager for transaction nested exception is java.lang.IllegalStateException:Already value for key bound to thread

我們正在使用 JdbcTemplate 在一些 DAO 中保存數據,在一些 DAO 中使用 JPA。 從另一個程序化事務相關代碼調用@Transaction 方法(此處為 saveAll 方法)時,我們面臨以下問題。

堆棧跟蹤:

org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; 

nested exception is 
java.lang.IllegalStateException:Already value [org.springframework.jdbc.datasource.ConnectionHolder@303ef11] for key [HikariDataSource (HikariPool-1)] bound to thread [http-nio-8091-exec-3]

示例代碼片段:

OneServiceImpl:這里我們使用 jdbcTemplate 來保存數據並使用程序化事務。 從程序化事務方法中,我們調用另一個 serviceImpl class 方法,它使用 JPA。

public CommonResponse saveCaseConfigurations(List<CaseDTO> caseList){
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                    protected void doInTransactionWithoutResult(TransactionStatus status) {
                        try {
                           scheduleServiceRepo.saveCases(caseList)); //Using JDBC template
                           ......
                           serviceTwo.updateCaseLogs(caseLogs); // calling secod service method.

                            
                        } catch (Exception ex) {
                            status.setRollbackOnly();
                            throw new CustomRunTimeException("Error due to"+ex.getMessage());
                        }

                    }
                });
}

TwoServiceImpl:這里我們使用 Jpa 和 saveAll 方法有@Transactional 注釋。

@Override
public CommonResponse updateCaseLogs(List<CaseLogs> caseLogs) {
    caseLogs = caseLogMasterRepo.saveAll(caseLogs);//Using JPA to save all in CaseLogs entity, failing here as saveAll is having @Transactional annotion.
    return new CommonResponse("SUCCESS", null,HttpStatus.OK);
}

我們將兩個事務管理器用於兩個事務。 請幫助我如何禁用內部事務並將相同的事務傳遞給 saveAll 方法調用。

必須將所需的傳播級別添加到您的 JPA 事務中。 請參閱下面的帖子,其中詳細介紹了所有 JPA 事務傳播級別。 傳播級別設置為需要在單個 scope 中執行多個事務。

Spring JPA 多方法交易

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM