簡體   English   中英

使用Hibernate的Spring用戶事務

[英]Spring user transaction with hibernate

我想自己控制休眠事務,以便可以隨時回滾。 我調用一個線程來做生意,但不等待它完成工作並更新數據庫。 此更新僅在方法結束時可用,但我想在每個for循環中提交更改,因此我需要控制休眠事務。

我的示例代碼如下:

for(BaseFileprocess fileProcess : unprocessedFiles) {
            BaseFileprocessfunctype functionType = fileProcessFunctionTypeService.findBySerno(fileProcess.getFunctioncodeserno());
            if(functionType != null) {
                taskExecutor.execute(new ServiceCallThread(functionType.getFunctionname(), fileProcess.getSerno(), fileProcess.getFilename()));
                fileProcess.setStatu("1");
                fileProcessService.update(fileProcess);//I need commit here
            }
            else {
                System.out.println("There is no defined Function Type");
            }
        }

有什么建議嗎?

查看Spring的transactionTemplate 從文檔:

// single TransactionTemplate shared amongst all methods in this instance
private final TransactionTemplate transactionTemplate;

// use constructor-injection to supply the PlatformTransactionManager
public SimpleService(PlatformTransactionManager transactionManager) {
    Assert.notNull(transactionManager, "The 'transactionManager' argument must not be null.");
    this.transactionTemplate = new TransactionTemplate(transactionManager);
}

public Object someServiceMethod() {
    return transactionTemplate.execute(new TransactionCallback() {

        // the code in this method executes in a transactional context
        public Object doInTransaction(TransactionStatus status) {
            updateOperation1();
            return resultOfUpdateOperation2();
        }
    });
}

暫無
暫無

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

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