Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 581461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:34:52+00:00 2026-05-13T14:34:52+00:00

I am learning JPA w/Hibernate using a Java SE 6 project. I’d simply like

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:34:52+00:00Added an answer on May 13, 2026 at 2:34 pm

    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.

    You could indeed use getDelegate() to get the underlying provider object for the EntityManager and then access the underlying JDBC Connection. But read carefully the javadoc:

    Return the underlying provider object for the EntityManager, if available. The result of this method is implementation specific.

    This makes your code doubly non portable. First it ties you to the underlying implementation (Hibernate’s Session here). Second, you may not always get the same result (at least not in an Java EE container).

    Really, you don’t want this as this totally defeats the point of JPA. Plus, you don’t want to handle this low level connection verification yourself.

    No, instead, you should use a (standalone) connection pool that will take care of that for you. c3p0 is one of them and it can be configured for connection testing and logging. Going the connection pool way means less code, better reliability, better recovery capabilities, better checks, without loosing portability. This looks like a 100% win-win situation to me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are good Java EE real world (not like learning projects like PetStore) open
I'm still learning how to set up these mappings using the JPA annotations so
I'm trying to mix HQL and SQl in the same query. Like using from
I'm just learning about JPA and session beans. I've worked through a simple database
Learning a little about T-SQL, and thought an interesting exercise would be to generate
I'm learning about table design in SQL and I'm wonder how to create a
I am learning JPA from this tutorial . I have some confusions in understanding
I am learning JPA and have one question: In which situations we need more
For the last three days i am learning JPA by various examples, to change
Learning WPF nowadays. Found something new today with .Net dependency properties. What they bring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.