2012-04-19 32 views
2

如果我把存儲過程的sql調用放在withTransaction裏面,Grails GORM withTransactiongroovy.sql.Sql會使用相同的連接嗎?例如:Grails:groovy.sql.Sql和Model.withTransaction會發生什麼?

可以說我有一個命令:

@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') 
    } 
    } 

} 

回答

2

我發現如何做到這一點。

def sessionFactory 

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

更多的信息在this topic