简体   繁体   English

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

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

I'm trying to create some threads in a service, but I got the hibernateException: no session... . 我正在尝试在服务中创建一些线程,但出现了hibernateException:没有会话...。 I have already seen a discussion about this in stackoverflow with a solution of throwing RuntimeException. 我已经在stackoverflow中看到了有关抛出RuntimeException的解决方案的讨论。 In my case is not working. 就我而言是行不通的。 Here is my service code: 这是我的服务代码:

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

job class: 工作类别:

 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)

      }
        }
    }

EDIT 编辑

With backgroundThread plugin (that should handle hibernate sessione): 使用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!")
        }
    })

I get this error ERROR events.PatchedDefaultFlushEventListener - Could not synchronize database state with session 我收到此错误ERROR events.PatchedDefaultFlushEventListener-无法将数据库状态与会话同步

We're using the quartz plugin which works fine. 我们使用的石英插件效果很好。

I have had the same problem before in a different case and what solved it was wrapping the domain access code in 在不同的情况下,我之前遇到过相同的问题,解决该问题的方法是将域访问代码包装在

DomainClass.withTransaction {
}

For example: 例如:

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的懒惰异常。

I think your problem is in passing the actual domain object to the thread. 我认为您的问题在于将实际的域对象传递给线程。 Try just passing the domain object ID to the function, and obtaining and saving inside that function/thread. 尝试仅将域对象ID传递给函数,然后在函数/线程中获取并保存。 Passing a domain object to another thread can cause problems. 将域对象传递给另一个线程可能会导致问题。

now is working. 现在正在工作。 Here are the changes I made: 这是我所做的更改:

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.

相关问题 HibernateException:没有 Hibernate Session 绑定到线程 - HibernateException: No Hibernate Session bound to thread Grails服务抛出异常“ org.hibernate.HibernateException:没有将Hibernate Session绑定到线程,并且配置不允许……” - Grails Service throwing Exception “org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow …” 异常是org.hibernate.HibernateException:没有绑定到线程的Hibernate会话 - exception is org.hibernate.HibernateException: No Hibernate Session bound to thread org.hibernate.HibernateException:没有Hibernate会话绑定到线程 - org.hibernate.HibernateException:No Hibernate Session bound to thread 没有Hibernate Session绑定到线程 - No Hibernate Session bound to thread 没有Hibernate Session绑定到线程 - No Hibernate Session bound to thread HTTP状态500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:没有绑定到线程的Hibernate会话 - HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread 在执行spring hibernate时得到“ HibernateException:没有Hibernate Session绑定到线程,&config不允许创建..” - Getting “HibernateException: No Hibernate Session bound to thread, & config doesn't allow creation ..” while execting spring hibernate Hibernate 4和Spring 4 HibernateException:未找到当前线程的会话 - Hibernate 4 and Spring 4 HibernateException: No Session found for current thread Spring 4 + Hibernate 4-HibernateException:找不到当前线程的会话 - Spring 4 + hibernate 4 - HibernateException: No Session found for current thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM