grails thread - > hibernateException:没有绑定到线程的Hibernate会话

时间:2010-11-03 14:47:16

标签: hibernate grails

我正在尝试在服务中创建一些线程,但是我得到了hibernateException:没有会话....我已经在stackoverflow中看到了一个关于抛出RuntimeException的解决方案的讨论。在我的情况下是行不通的。 这是我的服务代码:

class MatchService {

    static transactional = true

 def void start(Match match) {

  Thread.start {
   Match updateMatch = matchSituation(match)
   if(!updateMatch.save()) {
    throw new RuntimeException("match is not valid and cannot be saved!")
   }
  }
 }

 def Match matchSituation(Match m) {
  Random random = new Random()
  if(m.teamH.averagePlayerValue > m.teamA.averagePlayerValue) {
   m.golTeamH = random.nextInt(5)
  }
  else {
   m.golTeamA = random.nextInt(4)
  }
  return m
 }
}

工作班:

 class TestJob {

     def matchService
     List<Match> matchList = new ArrayList()

     static triggers = {
      cron name: 'trigger',  cronExpression: "0 0/1 15 ? * WED"
      }

     def group = "threadGroup"


        def execute() {
      Cal.get(1).matches.each{
       match ->
        matchList.add(match)
      }

      for(Match m: matchList) {
       if(!m.validate()) {
        throw new MatchException( message: "match not valid!!" , match:m)
       }
       matchService.start(m)

      }
        }
    }

修改

使用backgroundThread插件(应该处理hibernate sessione):

    backgroundService.execute("Calculating match", {
        def backgroundMatch = match
        backgroundMatch = matchSituation(backgroundMatch)
        if(!backgroundMatch.save()) {
            throw new RuntimeException("match is not valid and cannot be saved!")
        }
    })

我收到此错误 ERROR events.PatchedDefaultFlushEventListener - 无法将数据库状态与会话同步

4 个答案:

答案 0 :(得分:4)

我们正在使用石英插件,效果很好。

我之前在另一种情况下遇到了同样的问题,解决它的问题是在

中包装域访问代码
DomainClass.withTransaction {
}

例如:

def execute() {
  Cal.withTransaction {
    Cal.get(1).matches.each{
        match ->
        matchList.add(match)
    }

    for(Match m: matchList) {
        if(!m.validate()) {
            throw new MatchException( message: "match not valid!!" , match:m)
        }
        matchService.start(m)

    }
  }
}

答案 1 :(得分:1)

通过设置fetchmode in Domain-class to eager可以解决来自休眠的懒惰异常。

答案 2 :(得分:0)

我认为你的问题是将实际的域对象传递给线程。 尝试将域对象ID传递给函数,并获取并保存在该函数/线程中。将域对象传递给另一个线程可能会导致问题。

答案 3 :(得分:0)

现在正在运作。 以下是我所做的更改:

class TestJob {

    def matchService
    List<Match> matchList = new ArrayList()

    static triggers = {
        cron name: 'trigger',  cronExpression: "0 0/1 13 ? * THU"
    }

    def group = "threadGroup"


    def execute() {
        Cal.get(1).matches.each{ match ->
            matchList.add(match)
        }

        for(Match m: matchList) {
            if(!m.validate()) {
                throw new MatchException( message: "match not valid!!" , match:m)
            }
            matchService.run(m.id)
        }
    }
}

class MatchService {

    static transactional = true

//  Match updateMatch
    def backgroundService

    public void run(Long matchId) {

        backgroundService.execute("Calculating match", {
            def backgroundMatch = Match.findById(matchId)
            backgroundMatch = matchSituation(backgroundMatch)
            println backgroundMatch.teamH.name + " - " + backgroundMatch.teamA.name + ": " + backgroundMatch.golTeamH + " - " + backgroundMatch.golTeamA
            if(!backgroundMatch.save()) {
                throw new RuntimeException("match is not valid and cannot be saved!")
            }
        })
//      Thread.start {
//          println "run thread (" + matchId + ") : " + String.format('%tH:%<tM:%<tS.%<tL',System.currentTimeMillis())
//          this.updateMatch = matchSituation(Match.findById(matchId))
//          println updateMatch.teamH.name + " - " + updateMatch.teamA.name + ": " + updateMatch.golTeamH + " - " + updateMatch.golTeamA
//          if(!updateMatch.save(flush: true)) {
//              throw new RuntimeException("match is not valid and cannot be saved!")
//          }
//      }
    }

    def Match matchSituation(Match m) {
        Random random = new Random()
        if(m.teamH.averagePlayerValue > m.teamA.averagePlayerValue) {
            m.golTeamH = random.nextInt(5)
        }
        else {
            m.golTeamA = random.nextInt(4)
        }
        return m
    }
}