java - Grails:groovy.sql.Sql 和 Model.withTransaction 会发生什么?

标签 java grails groovy grails-orm

如果我将存储过程的 sql 调用放在 withTransaction 中,

Grails GORM withTransactiongroovy.sql.Sql 将使用相同的连接?例如:

假设我有一个命令:

@Validateable
class MyCommand {
  List<MyModel> listOfModel
}

然后我有一个服务来处理这个命令

class MyService {
  def dataSource

  def handleCommand( MyCommand command ) {
    MyModel.withTransaction { status ->
      for( MyModel m : command.listOfModel ) {
         if( !m.save() ) {
           status.setRollbackOnly()
           throw new MyException(m.errors)
         }
      }

      //now I need to call a stored proc. This will use the same connection?
      //withTransaction will commit the call?
      Sql s = new Sql(dataSource)
      s.call('my_stored_proc')
    }
  }

}

最佳答案

我已经找到了如何做到这一点。

def sessionFactory

//after GORM saves...  
sessionFactory.currentSession.flush()
Sql s = new Sql( sessionFactory.currentSession.connection()  )
s.call()

更多信息请参见 this topic .

关于java - Grails:groovy.sql.Sql 和 Model.withTransaction 会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10235210/

相关文章:

java - 无法为 com.android.build.gradle.internal.api.DefaultAndroidSourceSet 类型的源集 main 设置未知属性 's'

java - 使用 Eclipse Developer Studio 启动 wso2 ESB 服务器时出现无效的初始堆大小错误

java - 从带有 UTF-16 BOM 字符的文件中读取文本

java - printf 显示同一个变量的不同值

grails - 从 bootstrap 调用 grails Controller ?

groovy - groovy扩展运算符的Java翻译

grails - Grails Webdriver安装错误

hibernate - Grails sessonFactory在 Controller 中为null

java - 从 Groovy 类创建 Spring bean 导致 "cannot find symbol error"

groovy - 在 groovy 脚本中包含一些 groovy 脚本