2013-09-23 60 views
1

我不確定如果我在這裏丟失了某些東西,但是可以在不使用withTransaction方法的情況下在Grails中執行手動事務管理(在src/groovy中的groovy類中)?沒有域類的Grails手動交易

當我調用另一個Java Web應用程序的服務層時,我的應用程序中沒有任何域類。

回答

2

默認情況下,服務方法是事務性的。這是爲了得到Grails的事務行爲的最簡單的方法:

class SomethingService { 
    def doSomething() { 
     // transactional stuff here 
    } 
} 

如果您需要更細粒度的控制比這個,你就可以開始和結束交易程序通過Hibernate:

class CustomTransactions { 
    def sessionFactory 

    def doSomething() { 
     def tx 
     try { 
      tx = sessionFactory.currentSession.beginTransaction() 
      // transactional stuff here 
     } finally { 
      tx.commit() 
     } 
    } 
} 
+1

你也可以使用Spring'@在服務方法 –

+0

Transactional'註釋如果應用沒有域類就沒有理由有安裝了hibernate插件,所以上面都不可能 –

2

只有這樣,才能啓動Grails應用中的交易是this answer中提到的交易。

當我調用另一個Java Web應用程序的服務層時,我的應用程序中沒有任何域類。

這真的是一個單獨的應用程序,或者是您的Grails應用程序依賴的Java JAR嗎?如果前者,那麼事務應該由執行持久化的應用程序來管理。

2

上述方法也是正確的。

您還可以使用@Transactional(propagation=Propagation.REQUIRES_NEW)

class SomethingService{ 

    def callingMathod(){ 
    /** 
    * Here the call for doSomething() will 
    * have its own transaction 
    * and will be committed as method execution is over 
    */ 
    doSomething() 
    } 


    @Transactional(propagation=Propagation.REQUIRES_NEW)  
    def doSomething() {  

     // transactional stuff here 

    } 

}