繁体   English   中英

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

[英]grails thread -> hibernateException: No Hibernate session bound to thread

我正在尝试在服务中创建一些线程,但出现了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插件(应该处理休眠会话):

    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-无法将数据库状态与会话同步

我们使用的石英插件效果很好。

在不同的情况下,我之前遇到过相同的问题,解决该问题的方法是将域访问代码包装在

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)

    }
  }
}

通过将Domain-class中fetchmode设置为eager,可以修复来自Hibernate的懒惰异常。

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

现在正在工作。 这是我所做的更改:

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM