2009-07-22 16 views
0

私はいつでもロールバックできるように、自分自身で休止状態のトランザクションを制御したいと考えています。私はビジネスを行うスレッドを呼び出すが、仕事を終えてDBを更新するのを待っているわけではない。このアップデートは、メソッドが終了したときにのみ使用できますが、各forループの変更をコミットして、休止状態のトランザクションを制御する必要があります。休止状態のSpringユーザートランザクション

私のサンプルコードは以下の通りです:

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

答えて

2

春を参照してください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