Search code examples
hibernatec3p0

Hibernate C3P0: Checking out connection and releasing it


I am trying to obtain Connections from Hibernate C3P0 as follows:

@Autowired
private DataSource dataSource; // ComboPooledDataSource

public Connection getConnection() {
    dataSource.getConnection();
}

This is correctly working. What I am unsure of is releasing/closing these connections.

Do I need to call connection.close()? I just want the connection to be put back to pool and not closed.


Solution

  • Yes, you need to close the connection. Closing a pooled connection is what gives it back to the pool. It doesn't actually close the physical connection.

    Closing it in a finally block (or better, using try-with-resources) is critical, otherwise your pool will quickly go out of available connections.