grails螺纹->;hibernateException:没有绑定到线程的Hibernate会话
我试图在服务中创建一些线程,但我得到了HibernateeException:无会话。我已经在stackoverflow中看到了关于这一点的讨论,以及抛出RuntimeException的解决方案。在我的情况下,它不起作用。 这是我的服务代码:grails螺纹->;hibernateException:没有绑定到线程的Hibernate会话,hibernate,grails,Hibernate,Grails,我试图在服务中创建一些线程,但我得到了HibernateeException:无会话。我已经在stackoverflow中看到了关于这一点的讨论,以及抛出RuntimeException的解决方案。在我的情况下,它不起作用。 这是我的服务代码: class MatchService { static transactional = true def void start(Match match) { Thread.start { Match updateMatch =
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)
}
}
}
我得到这个错误
错误事件。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)
}
}
}
我们使用的石英插件工作正常 我以前在另一个案例中遇到过同样的问题,解决这个问题的方法是将域访问代码包装在
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)
}
}
}
我认为您的问题在于将实际的域对象传递给线程。
尝试将域对象ID传递给函数,并在该函数/线程内获取和保存。将域对象传递给另一个线程可能会导致问题。我认为您的问题在于将实际的域对象传递给线程。 尝试将域对象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
}
}
类测试作业{
def匹配服务
列表匹配列表=新的ArrayList()
静态触发器={
cron名称:“触发器”,cron表达式:“0 0/1 13?*THU”
}
def group=“threadGroup”
def execute(){
Cal.get(1).matches.each{match->
匹配列表。添加(匹配)
}
用于(匹配m:匹配列表){
如果(!m.validate()){
抛出新的匹配异常(消息:“匹配无效!!”,匹配:m)
}
matchService.run(m.id)
}
}
}
类匹配服务{
静态事务=真
//匹配更新匹配
def后台服务
公共无效运行(长匹配ID){
backgroundService.execute(“计算匹配”{
def backgroundMatch=Match.findById(matchId)
backgroundMatch=比赛情况(backgroundMatch)
println backgroundMatch.teamH.name+“-”+backgroundMatch.teamA.name+”:“+backgroundMatch.golTeamH+“-”+backgroundMatch.golTeamA
如果(!backgroundMatch.save()){
抛出新运行时异常(“匹配无效,无法保存!”)
}
})
//Thread.start{
//println“run thread(“+matchId+”):“+String.format(“%tH:%)现在正在工作。
以下是我所做的更改:
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
}
}
类测试作业{
def匹配服务
列表匹配列表=新的ArrayList()
静态触发器={
cron名称:“触发器”,cron表达式:“0 0/1 13?*THU”
}
def group=“threadGroup”
def execute(){
Cal.get(1).matches.each{match->
匹配列表。添加(匹配)
}
用于(匹配m:匹配列表){
如果(!m.validate()){
抛出新的匹配异常(消息:“匹配无效!!”,匹配:m)
}
matchService.run(m.id)
}
}
}
类匹配服务{
静态事务=真
//匹配更新匹配
def后台服务
公共无效运行(长匹配ID){
backgroundService.execute(“计算匹配”{
def backgroundMatch=Match.findById(matchId)
backgroundMatch=比赛情况(backgroundMatch)
println backgroundMatch.teamH.name+“-”+backgroundMatch.teamA.name+”:“+backgroundMatch.golTeamH+“-”+backgroundMatch.golTeamA
如果(!backgroundMatch.save()){
抛出新运行时异常(“匹配无效,无法保存!”)
}
})
//Thread.start{
//println“运行线程(“+matchId+”):+String.format(“%tH:%%来自Hibernate的惰性异常可以通过设置来修复。来自Hibernate的惰性异常可以通过设置来修复。在我的情况下,它不能解决问题,我得到了以下异常:错误Hibernate.LazyInitializationException-无法初始化代理-没有会话org.Hibernate.LazyInitializationException:could未初始化代理-没有会话可能是服务出了问题,我尝试了任何可能的解决方案,但没有成功-请尝试以下内容:applicationContext.getBean(“transactionManager”).getTransaction(new DefaultTransactionDefinition()),但我想你需要拉取正确的应用程序上下文,并在最后提交事务。或者看看:它的语法与后台插件几乎相同,但至少在我们的例子中,它可以正常工作。解决方案也可以是使用quartz创建5个触发器,然后像线程一样使用它们。但我的问题是,是否有可能o使触发器执行与其他触发器不同的操作?因为我看到在不同的时间或同时执行execute()的触发器更多…在我的情况下,它不能解决问题,我得到了以下异常:错误hibernate.LazyInitializationException-无法初始化代理-没有会话org.hibernate.LazyInitializationException:无法初始化代理-没有会话可能服务出了问题,我尝试了任何可能的解决方案,但没有成功-尝试一些方法大致如下:applicationContext.getBean(“transactionManager”).getTransaction(新的DefaultTransactionDefinition()),但我想你需要拉取正确的应用程序上下文,并在最后提交事务。或者看看:它的语法与后台插件几乎相同,但至少在我们的例子中,它可以正常工作。解决方案也可以是使用quartz创建5个触发器,然后像线程一样使用它们。但我的问题是,是否有可能o做一个不同于另一个的触发器?因为我在di上看到更多触发器