2009-07-22 72 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