I am investigating a connection leak in my code. We are using c3p0 to manage the connection pool and my general Hibernate use pattern is something like this:
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//..Work involving calls to find() and merge()
transaction.commit();
} catch (... e) {
//..log message, throw nicer exceptions
} finally {
entityManager.close();
}
Does this code have the potential to leak DB connections? Do I have to explicitly rollback a transaction in the case of a failure, or is that done automatically? Does entityManager.close() ensure that DB connections are returned into the connection pool?
Use transaction.rollback() in all your catch blocks.
The transaction will be closed without a commit then