Search code examples
hibernatejpac3p0

How to catch c3p0 and JPA exceptions?


I got this app where the user must connect the DB manually, I do that with this line

EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit", connectionProperties);

when the user/password is incorrect or the host timeouts due to wrong url C3P0 breaks the connection

then I get these in console

com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask, 1853 - A RESOURCE POOL IS PERMANENTLY BROKEN! [com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@18e3f02a]

THE PROBLEM: I can't capture that exception so I can translate it to a user friendly message on screen, it is like emf is always created and I got no way to tell when is broken or not

UPDATE: I found on Hibernate http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/transactions.html#transactions-demarcation-exceptions

But I surrounded my emf with EVERY single try catch possible and still can't catch a thing


Solution

  • The JPA spec says that Persistence.createEntityManagerFactory will return null if a persistence-unit can't be instantiated (and states nothing about possible exceptions). Consequently it's as much use as a chocolate teapot for detecting the reason programmatically. Complain to the people who wrote the JPA spec.