How to detect open database connection with Hibernate / JPA?
- by John K
I am learning JPA w/Hibernate using a Java SE 6 project. I'd simply like to be able to detect if the connection between Hibernate and my database (MS SQL Server) is open. For example, I'd like to be able to detect this, log it, and try reconnecting again in 60 seconds.
This is what I thought would work but isOpen() doesn't appear to be what I want (always is true):
EntityManagerFactory emf = Persistence.createEntityManagerFactory("rcc", props);
if (emf != null && emf.isOpen()) {
EntityManager em = emf.createEntityManager();
if (em == null || !emf.isOpen())
// error connecting to database
else ...
This seems to me to be a simple problem, but I cannot find an answer!