配置Hibernate以从连接池中获取新的连接 [英] Configure Hibernate to obtain a fresh connection from a connection pool

查看:337
本文介绍了配置Hibernate以从连接池中获取新的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

谢谢

解决方案


我配置了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? The connection pool is managed by Websphere Application Server and is a JDBC Data Source.

Thanks

解决方案

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.

这篇关于配置Hibernate以从连接池中获取新的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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