简体   繁体   English

Hibernate AssertionFailure在不同的线程中

[英]Hibernate AssertionFailure in different Threads

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. 我的线程“1”从数据库中捕获主数据。 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. 如果我在我的线程“1”完成并尝试从数据库加载另一个主数据集后取消我在后台运行的线程“2”,则会发生相同的错误。

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. 每个线程都应该从Hibernate会话工厂获取自己的会话。

It is not intended that implementors be threadsafe. 并不意味着实现者是线程安全的。 Instead each thread/transaction should obtain its own instance from a SessionFactory. 相反,每个线程/事务应从SessionFactory获取自己的实例。

See here: Hibernate Session JavaDoc 请参见: Hibernate Session JavaDoc

When you "cancel" a thread - it should do its own cleanup like transactions rollback, session close etc. 当你“取消”一个线程时 - 它应该像事务回滚,会话关闭等那样进行自己的清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM