圣杯线程 ->hibernateException: 没有 Hibernate 会话绑定到线程 [英] grails thread -> hibernateException: No Hibernate session bound to thread
本文介绍了圣杯线程 ->hibernateException: 没有 Hibernate 会话绑定到线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在服务中创建一些线程,但我收到了 hibernateException: no session... .我已经在 stackoverflow 中看到了有关此问题的讨论,其中包含抛出 RuntimeException 的解决方案.在我的情况下不起作用.这是我的服务代码:
I'm trying to create some threads in a service, but I got the hibernateException: no session... . I have already seen a discussion about this in stackoverflow with a solution of throwing 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
}
}
工作类别:
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 - 无法将数据库状态与会话同步
推荐答案
现在正在工作.以下是我所做的更改:
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
}
}
这篇关于圣杯线程 ->hibernateException: 没有 Hibernate 会话绑定到线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文