You are on page 1of 7

04/05/2019 [Solved] JDBCConnectionException: Unable to acquire JDBC Connection with Hibernate and MySQL

Ad closed by 
Stop seeing this ad Why this ad? 

Home

Java Core

Java SE

Java EE

Frameworks

Servers

Coding

IDEs

Books

Videos

Interview Questions

Certifications

Testing

c Home Frameworks Hibernate


[Solved] JDBCConnectionException: Unable to acquire JDBC Connection with Hibernate and MySQL

  
 

  
https://www.codejava.net/frameworks/hibernate/solved-jdbcconnectionexception-unable-to-acquire-jdbc-connection-with-hibernate-and-mysql 1/7
Featured Video
04/05/2019 [Solved] JDBCConnectionException: Unable to acquire JDBC Connection with Hibernate and MySQL

 Courses
Java Servlet, JSP and
Hibernate: Build a
Complete Website
Java Persistence:
Hibernate and JPA
Fundamentals
Spring and Hibernate
for Beginners
Latest Hibernate
Books
Hibernate Made Easy:
Simplified Data
Persistence with
Hibernate and JPA
(Java Persistence API)
Annotations
Hibernate Search in
Action
Java Persistence with
Hibernate
Hibernate in Action (In
Action series)
Beginning Hibernate
(Expert's Voice in Java
Technology)
Hibernate Recipes: A
Problem­Solution
Approach (Expert's
Voice in Open Source)
Harnessing Hibernate
Pro Hibernate and
MongoDB (The
Expert's Voice)
Latest Hibernate
Projects
Hibernate ORM
5.0.9.Final
Hibernate Search
5.5.3.Final
Hibernate Tools for
Eclipse and Ant
Hibernate Validator
5.2.4.Final
Hibernate Metamodel
Generator
Hibernate OGM
4.2.0.Final
Popular Hibernate
Downloads
Hibernate 5.0.9.Final
Hibernate Search
4.5.2.Final
Hibernate Validator
5.2.4.Final
Hibernate Metamodel
Generator 1.3.0.Final
https://www.codejava.net/frameworks/hibernate/solved-jdbcconnectionexception-unable-to-acquire-jdbc-connection-with-hibernate-and-mysql 2/7
04/05/2019Hibernate OGM [Solved] JDBCConnectionException: Unable to acquire JDBC Connection with Hibernate and MySQL

 4.1.0.CR1
JBoss Tools
Official Hibernate
Documentation
Hibernate Community
Material
Last updated: 2016­04­29
 

[Solved] JDBCConnectionException: Unable
to acquire JDBC Connection with Hibernate
and MySQL
Written by  Nam Ha Minh
Last Updated on 02 November 2018   |    Print   Email

When developing Java database web applications with Hibernate/JPA and MySQL, you
will face a connection error if the user has not used the application for a long time. The
exception stack trace would look like this:

1 The last packet successfully received from the server was 297,215,018
2 The last packet sent successfully to the server was 35 milliseconds a
3 ...
4 javax.persistence.PersistenceException: org.hibernate.exception.JDBCC
5 Unable to acquire JDBC Connection
6 ...
7 Caused by: org.hibernate.exception.JDBCConnectionException:
8 Unable to acquire JDBC Connection
9 ...
10 Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: C
11 ...
12 Caused by: java.net.SocketException: Software caused connection abort
13 …

If you refresh or try to connect again, the application successfully connected to the
database. And the problem keeps happening again if the user left the application for a
quite long time and come back. Though this is not a serious problem in development, but
it’s not good under the end­user’s perspective and they will see the application is buggy
and their work is interrupted. So this problem should be solved completely.

So why is JDBCConnectionException thrown?
By default, Hibernate uses its internal database connection pool library. That means it
keeps a database connection open to be reused later. And MySQL database server has a
timeout value for each connection (default is 8 hours or 28,800 seconds). So if a
connection has been idle longer than this timeout value, it will be dropped by the server.

Therefore, when the Java database application has been idle longer than MySQL server’s
connection timeout value, and the end user tries to connect again, Hibernate reuses the
idle connection which was already dropped by the server, hence
JDBCConnectionExceptionis thrown.

https://www.codejava.net/frameworks/hibernate/solved-jdbcconnectionexception-unable-to-acquire-jdbc-connection-with-hibernate-and-mysql 3/7
04/05/2019 [Solved] JDBCConnectionException: Unable to acquire JDBC Connection with Hibernate and MySQL
Solutions to fix JDBCConnectionException

If you look closely at the exception stack trace, you will see some suggestions to fix the
problem:

1 The last packet sent successfully to the server was 390,061 millisecon
2 is longer than the server configured value of 'wait_timeout'.
3 You should consider either expiring and/or testing connection validity
4 increasing the server configured values for client timeouts,
5 or using the Connector/J connection property 'autoReconnect=true' to a

Let’s consider each suggestion mentioned above.

The Data-Driven HR Outlook


Is Data Driving A Revolution In HR?
Download The Free Raconteur
Report To Learn More.

Expiring and/or testing connection validity before use in your application:

This requires changing or re­structuring the existing code, which is difficult to implement.
Furthermore, opening a database connection is an expensive operation so it’s not optimal
to open and close database connection for every user’s request ­ that’s why database
connection pooling comes into play.

Increasing the server configured values for client timeouts:

This is possible by updating the setting file (my.ini) on MySQL server machine ­ changing
the wait_timeoutvalue with longer value. However, the problem can occur again if the
connection has been idle longer than the timeout value.

Using the Connector/J connection property 'autoReconnect=true'

I tried this solution by appending the property autoReconnect=true to the database URL
but it doesn’t work. The end user still sees the exception before it is successfully
connected again. Also, the user of this feature is not recommended ­ accordingly to
MySQL documentation.

If you look around on the web, you will see someone suggest adding the following
properties to Hibernate configuration file:

https://www.codejava.net/frameworks/hibernate/solved-jdbcconnectionexception-unable-to-acquire-jdbc-connection-with-hibernate-and-mysql 4/7
04/05/2019 [Solved] JDBCConnectionException: Unable to acquire JDBC Connection with Hibernate and MySQL

 1
2
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
3 <property name="connection.is­connection­validation­required">true</pr

I already tried this, and it doesn’t work.

A solution that really works to solve
JDBCConnectionException problem:
Finally I found a solution that actually works to avoid an error occur if the database
connection is idle too long. It is using the c3p0 database connection pooling library.

If you project is Maven­based, add the following dependency to use c3p0:

1 <dependency>
2     <groupId>org.hibernate</groupId>
3     <artifactId>hibernate­c3p0</artifactId>
4     <version>5.3.6.Final</version>
5 </dependency>

If not, let find, download on Maven Central Repository add the following JAR files to your
project’s classpath:

1 hibernate­c3p0­5.3.6.Final.jar
2 c3p0­0.9.5.2.jar
3 mchange­commons­java­0.2.11.jar

Then add the following properties to Hibernate/JPA configuration file of your project:

1 <property name="hibernate.c3p0.min_size" value="5" />
2 <property name="hibernate.c3p0.max_size" value="20" />
3 <property name="hibernate.c3p0.timeout" value="300" />

Let me explain these properties:

hibernate.c3p0.min_size: the minimum number of connections maintained in the pool
at any given time.
hibernate.c3p0.max_size: the maximum number of connections maintained in the
pool at any given time.
hibernate.c3p0.timeout: the number of seconds an idle connection is kept in the pool.
If a connection is idle longer than this timeout value, then it will be replaced by a new
one.

So that means you have to set the value of hibernate.c3p0.timeout less than the
wait_timeout value on MySQL server. And the value 300 seconds in the above example
is pretty good. This definitely solves the problem because the pool maintains no
connections that are idle longer than the MySQL server’s timeout value.

And c3p0 can be used in production to replace Hibernate’s default internal pool.

 
https://www.codejava.net/frameworks/hibernate/solved-jdbcconnectionexception-unable-to-acquire-jdbc-connection-with-hibernate-and-mysql 5/7
About
04/05/2019
the Author: [Solved] JDBCConnectionException: Unable to acquire JDBC Connection with Hibernate and MySQL

 Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started
programming with Java in the time of Java 1.4 and has been falling in love
with Java since then. Make friend with him on Facebook. 
 

The Data-Driven
HR Outlook
Is Data Driving A
Revolution In HR?
Download The Free
Raconteur Report To Learn
Sponsored By Zellis More.

Add comment

Name E-mail

comment

500 symbols left

Notify me of follow­up comments 

I'm not a robot


reCAPTCHA
Privacy - Terms

Send

Comments  

#1Popup hell 2019­02­26 01:54
I needed 4 clicks to finally read your article. 2 on banners, 1 on cookies and 1 on
some newspaper. 
Writing this feedback message, I got number 5. 
 
Do you even realize how annoying that is UX wise? I would skip your page anytime
now, just to avoid the too agressive way of spamming the user before he can finally
read the content you probably really want to spread? 
 
Let the user decide WHEN he wants to enlist whereever. Only the cookie wall is
acceptable.
Quote
https://www.codejava.net/frameworks/hibernate/solved-jdbcconnectionexception-unable-to-acquire-jdbc-connection-with-hibernate-and-mysql 6/7
04/05/2019 [Solved] JDBCConnectionException: Unable to acquire JDBC Connection with Hibernate and MySQL
Refresh comments list

About CodeJava.net: 
CodeJava.net shares Java tutorials, code examples and sample projects for programmers at all levels. 
CodeJava.net is created and managed by Nam Ha Minh ­ a passionate programmer.  
 
About  Advertise  Contact  Terms of Use  Privacy Policy  Sitemap  Newsletter  Facebook  Twitter YouTube   
 
Copyright © 2012 ­ 2019 CodeJava.net, all rights reserved.

https://www.codejava.net/frameworks/hibernate/solved-jdbcconnectionexception-unable-to-acquire-jdbc-connection-with-hibernate-and-mysql 7/7

You might also like