服务层和事务

时间:2014-01-10 15:08:55

标签: grails service transactions

我有2个DB,我正在使用的服务方法传递来自DB 1的对象,并尝试从第2个DB中检索和处理hasMany项目。当我将进程包装在.withTransaction中时,该方法可以正常工作。

def buildApoMap( Apo pApo ) {           
  def lResults = [:]
  Apo.withTransaction() {
  if ( pApo ) {
    lResults.apo = [ id: pApo.id, apoId: pApo.apoId, 
          accountName: pApo.account?.accountName, 
          subProgram: [ id: pApo.subProgram?.id, name: pApo.subProgram?.name],
          eventCount: pApo.events?.toList().size ]

   if ( pApo.events ) {
      lResults.apo.events = pApo.events.collect { lEvent ->
            [id: lEvent.id, eventReferenceId: lEvent.eventReferenceId, 
            sellingMethod: lEvent.sellingMethod?.toString(),
            accountCommissionMethod: lEvent.accountCommissionMethod]
    }
  }
    }
  }
  return lResults
}

当我宣布

 static transactional = true

该方法失败,并显示以下错误消息

ERROR 2014-01-10 08:44:48,084 [JDBCExceptionReporter] [http-bio-8080-exec-4] [unknown 8:44:47 AM|] - Connection is closed.
 ERROR 2014-01-10 08:44:48,091 [ApoRestController] [http-bio-8080-exec-4] [unknown 8:44:47 AM|] - Unexpected exception while processing Rest request to search for APOs.
 org.hibernate.exception.GenericJDBCException: could not initialize a collection: [com.company.Apo.events#7]
at com.company.ApoService$$EOSYMFqe.buildApoMap(ApoService.groovy:240)
at com.company.ApoRestController$_search_closure2.doCall(ApoRestController.groovy:78)
at com.company.ApoRestController.search(ApoRestController.groovy:77)
at org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:65)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.sql.SQLException: Connection is closed.
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.checkOpen(PoolingDataSource.java:185)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:312)
... 7 more

任何想法?我们将做很多事情。希望不必在自己的事务中包装每个方法。

1 个答案:

答案 0 :(得分:1)

我认为您需要使用类似atomikos插件(http://www.grails.org/plugin/atomikos)的东西来支持跨多个数据库的事务。