繁体   English   中英

没有域类的Grails手动交易

[英]Grails manual transactions with no domain classes

我不确定是否在这里丢失了某些东西,但是可以在Grails中(在src / groovy中的groovy类中)进行手动事务管理,而无需使用withTransaction方法吗?

我正在调用另一个Java Web应用程序的服务层时,我的应用程序中没有任何域类。

服务方法默认为事务性的。 这是在grails中获得交易行为的最简单方法:

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

如果您需要比这更精细的控制,则可以通过休眠以编程方式启动和结束事务:

class CustomTransactions {
    def sessionFactory

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

在Grails应用程序中启动事务的唯一方法是此答案中提到的方法。

我正在调用另一个Java Web应用程序的服务层时,我的应用程序中没有任何域类。

这真的是一个单独的应用程序,还是Grails应用程序所依赖的Java JAR? 如果是前者,则事务应由执行持久性的应用程序管理。

以上方法也是正确的。

您也可以使用@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

  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM