grails线程 - > hibernateException:没有Hibernate会话绑定到线程 [英] grails thread -> hibernateException: No Hibernate session bound to thread
问题描述
我试图在服务中创建一些线程,但是我得到了hibernateException:没有会话......。我已经在一个抛出RuntimeException的解决方案中看到了关于这个在stackoverflow中的讨论。在我的情况下是行不通的。
这是我的服务代码:
class MatchService {
static transactional = true
def void start(Match match){
Thread.start {
match updateMatch = matchSituation(match)
if(!updateMatch.save() ){
抛出新的RuntimeException(匹配无效且无法保存!)
}
}
}
def匹配matchSituation(Match m){
随机随机=新随机()
if(m.teamH.averagePlayerValue> m.teamA.averagePlayerValue){
m.golTeamH = random.nextInt(5)
else {
m.golTeamA = random.nextInt(4)
}
return m
}
}
code>
工作类:
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)
}
编辑
backgroundService.execute(计算匹配,{
def backgroundMatch =匹配
backgroundMatch = matchSituation(backgroundMatch)
if(!backgroundMatch.save()){
throw new RuntimeException(match is not valid并且无法保存!)
}
})
我得到这个错误
错误events.PatchedDefaultFlushEventListener - 无法与会话同步数据库状态
现在正在工作。
以下是我所做的更改:
class TestJob {
def matchService
列表< 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新的MatchException(消息:match not valid !!,match:m)
}
matchService.run(m.id)
}
}
}
class MatchService {
static transactional = true
//匹配updateMatch
def backgroundService
public void运行(Long matchId){
backgroundService.execute(计算匹配,{
def backgroundMatch = Match.findById(matchId)
backgroundMatch = matchSituation(backgroundMatch)
println backgroundMatch.teamH.name + - + backgroundMatch.teamA.name +:+ backgroundMatch.golTeamH + - + backgroundMatch.golTeamA
if(!backgroundMatch.save()){
抛出新的RuntimeException(匹配无效并且无法保存!)
}
// Thread.start {
// printlnrun thread(+ matchId +):+ String.format('%tH:%< tM: % // 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 can not 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
}
}
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
}
}
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):
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
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
}
}
这篇关于grails线程 - > hibernateException:没有Hibernate会话绑定到线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!