Configure Hibernate to obtain a fresh connection from a connection pool

坚强是说给别人听的谎言 提交于 2019-12-24 12:18:02

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2648422/configure-hibernate-to-obtain-a-fresh-connection-from-a-connection-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!