views:

172

answers:

1

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

+2  A: 

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.

Pascal Thivent
Thanks so much, I got it working.
ferrari fan
@ferrari You're welcome. Glad it's working.
Pascal Thivent