2009-07-22 9 views
0

Ich möchte Ruhezustand Transaktion selbst steuern, so dass ich jederzeit Rollback kann. Ich rufe einen Thread an, um Geschäfte zu machen, warte aber nicht darauf, dass er seinen Job beendet und die DB aktualisiert. Dieses Update ist nur verfügbar, wenn die Methode beendet wird, aber ich möchte Änderungen in jeder for-Schleife festschreiben, sodass ich die Ruhezustand-Transaktion steuern muss.Spring Benutzer Transaktion mit Ruhezustand

Meine ist folgende Beispielcode:

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

Jeder Vorschlag?

+0

Das bisschen Code ist nicht wirklich interessant, wir müssen das Bit sehen, das den Winterschlaf macht. – skaffman

Antwort

2

Blick in Spring transactionTemplate. Aus der Dokumentation:

// 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

Danke. Transaktionsvorlage ist für mich geeignet. – firstthumb

Verwandte Themen