Indrek Ots
by Indrek Ots
~1 min read

Categories

  • articles

Tags

  • java
  • mysql
  • jdbc
  • exception
  • dbcp
  • spring

Notice! This post is more than a year old. It may be outdated.

I was trying to connect to a MySql database but every once in a while I got the following exception

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

As usual, I opened a new browser tab and started googling for possible solutions. To my amazement, I found a lot of proposed solutions. I’m going to list 2 of them that worked for me.

1. Add ?autoReconnect=true to you JDBC URL

jdbc:mysql://localhost:3306/?autoReconnect=true

2. Using DBCP as connection pool with Spring

Add the following entries to your datasource definition (the last 2 property elements):

<bean id=”datasource” destroy-method=”close”>
	<property name=”driverClassName” value=”${.jdbc.driver}” />
	<property name=”url” value=”${jdbc.url}” />
	<property name=”username” value=”${jdbc.username}” />
	<property name=”password” value=”${jdbc.password}” />
	<property name=”validationQuery” value=”SELECT 1″ />
	<property name=”testOnBorrow” value=”true” />
</bean>