gpt4 book ai didi

grails - Quartz 作业中的 org.hibernate.LazyInitializationException

转载 作者:行者123 更新时间:2023-12-01 09:59:59 25 4
gpt4 key购买 nike

我可以在我的 Quartz 作业中对我的域类使用动态查找器,但在访问关系时得到 org.hibernate.LazyInitializationException。我认为它们要么都有效,要么都无效。

class MyJob {
def author = Author.list().first() // fine
def book = Book.get(1) // fine
println author.books // lazy exception
}

知道为什么会这样吗?根据 Quartz 插件文档,每个作业线程都有一个 Hibernate session ,但我遇到了这个问题。

Grails 2.1.1, quartz:1.0-RC9

完整错误:

2013-07-16 16:08:10,008 [quartzScheduler_Worker-10] ERROR grails.plugins.quartz.listeners.ExceptionPrinterJobListener  - Exception occurred in job: null
org.quartz.JobExecutionException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: test.Author.books, no session or session was closed [See nested exception: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: test.Author.books, no session or session was closed]
at grails.plugins.quartz.GrailsJobFactory$GrailsJob.execute(GrailsJobFactory.java:96)
at grails.plugins.quartz.QuartzDisplayJob.execute(QuartzDisplayJob.groovy:29)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: test.Author.books, no session or session was closed
at test.MyJob$_execute_closure1$$EOBjSWum.doCall(MyJob.groovy:7)
at test.MyJob$$EOBjSWum.execute(MyJob.groovy:7)
at grails.plugins.quartz.GrailsJobFactory$GrailsJob.execute(GrailsJobFactory.java:89)
... 3 more

最佳答案

MyJob 不是 grails 人工制品,因此默认情况下不是事务性的。延迟获取的关联必须在事务边界下。

解决方案:-
关注@Alidad 的评论。

class MyJob {
def author = Author.list().first()
def book = Book.get(1)
Book.withTransaction{
//withSession can also be used. You can also use Autor.withTransaction.
//The entity reference is immaterial.
println author.books
}
}

关于grails - Quartz 作业中的 org.hibernate.LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17669340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com