不同线程中的休眠断言失败 [英] Hibernate AssertionFailure in different Threads

查看:44
本文介绍了不同线程中的休眠断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个会话连接到我的数据库.在我的整个程序中,我总是有相同的会话.我的线程1"从数据库中捕获主要数据.必须允许用户取消此线程.因此,如果用户频繁或快速按下取消按钮(这是我的解释),则会发生以下错误:

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

如果我在线程1"完成后取消在后台运行的线程2"并尝试从数据库加载另一个主数据集,则会发生同样的错误.

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?

推荐答案

每个线程都应该从 Hibernate 会话工厂获取自己的会话.

Each thread should obtain its own session from Hibernate session factory.

实现者并不是线程安全的.相反,每个线程/事务都应该从 SessionFactory 中获取自己的实例.

It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.

参见此处:Hibernate Session JavaDoc

当你取消"一个线程时——它应该做自己的清理,比如事务回滚、会话关闭等.

When you "cancel" a thread - it should do its own cleanup like transactions rollback, session close etc.

这篇关于不同线程中的休眠断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆