不同线程中的休眠断言失败

编程入门 行业动态 更新时间:2024-10-24 04:43:23
本文介绍了不同线程中的休眠断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通过一个会话连接到我的数据库.在我的整个程序中,我总是有相同的会话.我的线程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.

更多推荐

不同线程中的休眠断言失败

本文发布于:2023-10-24 17:17:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524533.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:断言   线程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!