2009-07-22 153 views
0

我想自己控制hibernate事務,所以我可以在任何時候回滾。我調用一個線程來做生意,但沒有等待它完成工作並更新數據庫。此更新僅在方法結束時可用,但我想在每個for循環中提交更改,因此我需要控制hibernate事務。用戶休眠的春天用戶交易

我的示例代碼如下:

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"); 
      } 
     } 

什麼建議嗎?

+0

這段代碼並不真正有趣,我們需要看看冬眠工作的位。 – skaffman 2009-07-22 07:51:43

回答

2

調查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(); 
     } 
    }); 
} 
+0

謝謝。交易模板適合我。 – firstthumb 2009-07-24 14:04:01