Hibernate AssertionFailure在不同的线程中 [英] Hibernate AssertionFailure in different Threads
问题描述
我用一个会话连接到我的数据库。我在整个项目中都有同样的课程。我的线程1从数据库中捕获主数据。用户必须被允许取消该线程。所以如果用户经常按下取消按钮或快速(这是我的解释),会发生以下错误:
ERROR org .hibernate.AssertionFailure - HHH000099:发生断言失败(这可能表示Hibernate中存在错误,但更可能是由于会话的不安全使用)
org.hibernate.AssertionFailure:可能的非线程安全访问会话
同样的错误发生在我取消我的线程2后在后台运行1ist已完成,并试图从数据库中加载另一个主数据集。
是我在两个线程中使用同一个会话的失败吗?
解决这个问题的正确方法是什么?
线程应该从Hibernate会话工厂获取它自己的会话。
它并不意味着实现者是线程安全的。相反,每个线程/事务应该从SessionFactory获得自己的实例。
请参阅: Hibernate Session JavaDoc
你取消一个线程 - 它应该做自己的清理,如事务回滚,会话关闭等。
I connect to my database with one session. I have always the same session in my whole program. My Thread "1" catches primary data from the database. The user must be allowed to cancel this thread. So if the user presses the cancel button to often or to fast (this is my interpretation) the following error occures:
ERROR org.hibernate.AssertionFailure - HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: possible non-threadsafe access to the session
The same errors occures if i cancel my thread "2" which is running in the background after my thread "1" ist finished and the try to load another primary data set from the database.
Is the failure that i am using the same session in my two threads?
What is the right way to solve such a problem?
Each thread should obtain its own session from Hibernate session factory.
It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.
See here: Hibernate Session JavaDoc
When you "cancel" a thread - it should do its own cleanup like transactions rollback, session close etc.
这篇关于Hibernate AssertionFailure在不同的线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!