从 Hibernate 获取后台线程的新会话
我正在开发一个网络应用程序。通常在请求开始时(通过架构代码),会打开一个 Hibernate 会话来处理数据库事务。请求结束时,会话关闭。这对于我们所有的事务都很有效,除了在一个特定的实例中,我想从请求中触发一个线程。该线程将调用数据库事务。
从线程中,我调用“sessionFactory.openSession()”,并通过此会话执行我的事务。问题的出现是当请求完成时,线程本身可能不一定完成。因此,当请求完成并且线程尝试执行另一个数据库事务时,我得到一个 Hibernate Session is Closed!错误。
无论如何,从我的线程中,我可以打开一个“干净”的会话,而不是与请求开始时打开的会话关联吗?
I'm working on a web application. Usually at the beginning of a request (through the architecture code), a Hibernate session is opened to work with DB transactions. At the end of the request the session is closed. This works great for all of our transactions, except that in one particular instance, I want to fire a thread from a request. This thread will call DB transactions.
From the thread, I call "sessionFactory.openSession()", and with this session I perform my transactions. The problem arises in that when the request finishes, the thread itself may not necessarily be finished. So when the request finishes and the thread tries to perform another DB transaction I get a Hibernate Session is Closed! error.
Is there anyway that from my thread, I can open a "clean" session, not associated to the one opened at the start of the request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个线程服务,它扩展了
HibernateAccessor
,作为一个独立的 Spring 服务,在spring.xml
中定义,并向其发送您想要处理的代码/数据。像这样的东西:You can create an threading service, which extends
HibernateAccessor
, as a standalone Spring service, defined atspring.xml
, and send to it code/data you want to process. Something like this:您可以使用 JPA Entity Manager 手动开始和提交事务。 这是我的扩展答案
You can use JPA Entity Manager to begin and commit transactions manually. Here is my extended answer