Search code examples
hibernatesessionconnectionc3p0

hibernate : can multiple sessions share the same connection?


We have a C3P0 pool as datasource, and we are using org.springframework.orm.hibernate4.LocalSessionFactoryBean as session factory.

Some of our (big) MySQL requests are configured to stream the result set. However, MySQL only allow to stream a single result per connection.

That's why I'm wondering if 2 different Hibernate session can use the same connection, or is it 1 connection = 1 session ?


Solution

  • Just to complement JB Nizet's answer: a connection is provided by the pool to the application (a Hibernate session, in this case). The application uses this connection and returns it to the pool. The pool will decide whether to discard this connection or to reuse it in the future for another session. So, two different sessions can end up using the same connection, but not at the same time.