多个线程在Grails中将行插入表中

时间:2017-07-04 21:57:17

标签: multithreading grails gorm executorservice

我试图通过标准Groovy-Grails应用程序中的multiThreaded程序将多行数据插入表中。该应用程序使用标准GORM对象进行数据库交互。每个线程都插入一组不同的行。我在调用Runnable Groovy类的Grails服务中使用ExecutorService和FixedThreadPool。 Runnable类查询DB,执行大量计算并将派生值插入表中。我遇到了以下异常。

`Error java.lang.NullPointerException

Error at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)     Error at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:85)

这是Runnable类。

@Transactional
class MyRunnable{
  def aService
  def bService
def run(){
  for (i=1; i<n; i++){//n is a finite integer
   def x = aService.doSomething()
   def y = bService.doSomething(x)
   AGormClass.addToBGormClass(y)
  }
  AGormClass.save flush:true
 }
}

这里的Grails服务初始化线程池并调用Runnable类。

@Transactional
class SvcClass{
  ExecutorService exeSvc = Executors.newFixedThreadPool(2)
  MyRunnable runnable = new MyRunnable()
  exeSvc.execute(runnable as Runnable)
  exeSvc.shutdown()
}

0 个答案:

没有答案
相关问题