配置 Hibernate 以从连接池获取新连接

发布于 2024-08-29 05:32:51 字数 133 浏览 2 评论 0 原文

如何配置 Hibernate 以便每次调用 sessionFactory.openSession() 时它都会与连接池中的新连接连接?连接池由Websphere Application Server 管理,是一个JDBC 数据源。

谢谢

How do I configure Hibernate so that each time I call sessionFactory.openSession() it connects with a new connection from the connection pool? The connection pool is managed by Websphere Application Server and is a JDBC Data Source.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-09-05 05:32:51

如何配置 Hibernate,以便每次调用 sessionFactory.openSession() 时它都会与连接池中的新连接建立连接?

这是默认行为,每个会话都会从连接池中获取一个专用连接。

现在,两个会话似乎都在使用相同的连接,因为当第一个会话关闭(手动调用 session.close())时,有时另一个会话在尝试运行时会抛出“会话已关闭”异常更多查询。

不,他们不是。但也许第二个连接会在为请求发起的事务结束时被释放。看看 hibernate.connection.release_mode 配置参数,您可能需要使用on_close。但如果没有有关您的交易策略的更多详细信息,就无法说什么。

第二个会话由子线程打开,这意味着即使 (HTTP) 请求完成后子线程也可以保持活动状态。

接受我之前的建议,您不应该生成 非托管线程,我不知道应用程序服务器将如何运行。我在 这个其他答案中解释 什么是正确的方法。

How do I configure Hibernate so that each time I call sessionFactory.openSession() it connects with a new connection from the connection pool?

This is the default behavior, each session will get a dedicated connection from the connection pool.

Right now, it appears that both sessions are using the same connection, because when the first session is closed (manually calling session.close()) sometimes, the other session will throw a "session closed" exception when trying to run more queries on it.

No they are not. But maybe the second connection gets released at the end of the transaction initiated for the request. Have a look at the hibernate.connection.release_mode configuration parameter, you might want to use on_close. But without more details on your transaction strategy, it's impossible to say anything.

The second session is open by a child thread which means that the child thread can keep living even after the (HTTP) request is complete.

Take my previous advice with a grain of salt, you should just not spawn unmanaged threads and I don't know how the application server will behave. I explain in this other answer what would be the right way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文