0

I am new in using hibernate connection, i am having difficulties in checking the connection before executing a query. Currently if i execute a query, my java swing application hangs roughly around 30 seconds before it confirms that the connection is not available. I already used session.isConnected() but even the server is down, the return is always true. Please help.Thanks

Here is my connection properties :

"hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider" 
"hibernate.c3p0.validate", "true" 
"hibernate.c3p0.acquire_increment", "2" 
"hibernate.c3p0.idle_test_period", "30" 
"hibernate.c3p0.min_size", "5" 
"hibernate.c3p0.max_size", "25" 
"hibernate.c3p0.max_statements", "0" 
"hibernate.c3p0.testConnectionOnCheckout", "true" 
"hibernate.c3p0.testConnectionOnCheckIn", "true" 
"hibernate.c3p0.preferredTestQuery", "SELECT 1" 
"hibernate.c3p0.acquireRetryAttempts", "3" 
"hibernate.c3p0.acquireRetryDelay", "200" 
"hibernate.c3p0.timeout", "1000" 
"hibernate.c3p0.maxConnectionAge", "1100" 
"hibernate.connection.autoReconnect", "true" 
"javax.persistence.lock.timeout", "1" 
"javax.persistence.query.timeout", "1" 

1 Answers1

0

Are you using a connection pool? If so you can typically configure it with a validationQuery (e.g. "SELECT 1") that it will execute before handing out the connection to you. No need to manually do it on your own.

Are you not using a connection pool yet? Use one!

Answering to your comment: You might need to explicitly switch on validation. E.g. set hibernate.c3p0.validate to true

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Hi, i am using c3p0. My connection property is "hibernate.c3p0.preferredTestQuery", "SELECT 1". – Rally Vincent Sep 17 '14 at 07:15
  • Hi that's included on my connection properties. I think my entitymanager thinks that the connection is still valid, even though the server is already offline. – Rally Vincent Sep 17 '14 at 07:35
  • can you monitor the queries that are actually executed? There are also max-age settings that might have an influence on the frequency of those queries. Alternatively you might be able to set a shorter timeout if all queries are expected to come back in very short time. Also, now that we're that far, it might be good if you add the full connectionpool config to the question. Maybe somebody else spots an obvious issue with the settings that we'd otherwise have to iterate on in order to find it. – Olaf Kock Sep 17 '14 at 08:22
  • Here is my connection settings : – Rally Vincent Sep 17 '14 at 09:34
  • markdown hides them well (I'm assuming you pasted xml). Please edit your question and give the information there. In the question editor you'll have formatting options that help you to make sure your xml doesn't get filtered (e.g. format it as code) – Olaf Kock Sep 17 '14 at 10:05
  • Hi Thank you for your response, I followed you instruction to post my connection properties. I am not using .xml for connection properties, properties were loaded using hashmap. – Rally Vincent Sep 18 '14 at 01:41