org.hibernate.LazyInitializationException在Quartz作业中 [英] org.hibernate.LazyInitializationException in a Quartz job
问题描述
我可以在我的 Quartz
作业的域类中使用动态查找器,但获取 org.hibernate.LazyInitializationException
当访问关系时。我认为他们要么都工作,要么两者都不工作。
class MyJob {
def author = Author.list() .first()//罚款
def book = Book.get(1)//罚款
println author.books //懒惰异常
}
任何想法为什么会发生这种情况?根据Quartz插件文档,每个作业线程都会获得Hibernate会话,但我遇到了这个问题。
Grails 2.1.1
,
quartz:1.0-RC9
完整错误:
2013-07-16 16:08:10,008 [quartzScheduler_Worker-10]错误grails.plugins.quartz.listeners.ExceptionPrinterJobListener - 作业中发生异常:null
org.quartz.JobExecutionException:org.hibernate.LazyInitializationException:无法懒惰地初始化角色集合:test.Author.books,没有会话或会话已关闭[请参阅嵌套异常:org.hibernate.LazyInitializationException:未能延迟初始化一个角色集合:test.Author.books,没有会话或会话已关闭] $ 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.ja va:213)
在org.quartz.simpl.SimpleThreadPool $ WorkerThread.run(SimpleThreadPool.java:557)
导致:org.hibernate.LazyInitializationException:无法懒惰地初始化一个角色集合:test .Author.books,没有会话或会话已关闭
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 artefact,因此默认情况下不是事务性的。
解决方案: -
请关注@ Alidad的评论。
class MyJob {
def author = Author.list()。first()
def book = Book.get(1)
Book.withTransaction {
// withSession也可以使用。您也可以使用Autor.withTransaction。
//实体引用不重要。
println author.books
}
}
I can use dynamic finders on my domain classes in my Quartz
job, but get org.hibernate.LazyInitializationException
when accessing relationships. I thought they would either both work, or none.
class MyJob {
def author = Author.list().first() // fine
def book = Book.get(1) // fine
println author.books // lazy exception
}
Any idea why this might be happening? According to the Quartz plugin documentation each job thread gets a Hibernate session, yet I'm running into this problem.
Grails 2.1.1
, quartz:1.0-RC9
Full Error:
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
is not a grails artefact, hence not transactional by default. Associations which will be fetched lazily has to be under transactional boundary.
Solution:-
Follow @Alidad's comment.
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
}
}
这篇关于org.hibernate.LazyInitializationException在Quartz作业中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!