How to detect open database connection with Hibernate / JPA?

Posted by John K on Stack Overflow See other posts from Stack Overflow or by John K
Published on 2010-01-21T16:39:02Z Indexed on 2010/05/01 0:37 UTC
Read the original article Hit count: 335

Filed under:
|
|
|
|

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!

© Stack Overflow or respective owner

Related posts about jpa

Related posts about java