title: Spring Batch - Error while getting connection from MySQL after 16K records
body: <p>I have a Spring batch job that is reading data from a csv file and inserting data into Solr. After inserting around 16K records i am getting an error while getting connection from MySQL(used for Spring batch job repository data). Could someone pls suggest what is wrong here?</p>
  
  <pre><code>org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615)
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:680)
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:712)
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:722)
      at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:790)
      at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:814)
      at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.synchronizeStatus(JdbcJobExecutionDao.java:303)
      at org.springframework.batch.core.repository.support.SimpleJobRepository.update(SimpleJobRepository.java:161)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy9.update(Unknown Source)
      at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:357)
      at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
      at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
      at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
      at com.turvo.batch.carrier.main.CarrierLoadMain.main(CarrierLoadMain.java:49)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at sun.reflect.GeneratedConstructorAccessor26.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:357)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:834)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
      at sun.reflect.GeneratedConstructorAccessor24.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
      at java.sql.DriverManager.getConnection(DriverManager.java:664)
      at java.sql.DriverManager.getConnection(DriverManager.java:208)
      at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
      at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
      at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
      at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120)
      at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
      at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
      ... 26 more
  Caused by: java.net.ConnectException: Connection refused
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:589)
      at java.net.Socket.connect(Socket.java:538)
      at java.net.Socket.&lt;init&gt;(Socket.java:434)
      at java.net.Socket.&lt;init&gt;(Socket.java:244)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:307)
      ... 45 more
  </code></pre>
  
  <p>MySQL Configuration - </p>
  
  <pre><code>&lt;!-- connect to database --&gt;
  &lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="${mysql.driver}" /&gt;
      &lt;property name="url" value="${mysql.url}" /&gt;
      &lt;property name="username" value="${mysql.username}" /&gt;
      &lt;property name="password" value="${mysql.password}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /&gt;
  
  &lt;!-- create job-meta tables automatically --&gt;
  &lt;jdbc:initialize-database data-source="dataSource"&gt;
      &lt;jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" /&gt;
      &lt;jdbc:script location="org/springframework/batch/core/schema-mysql.sql" /&gt;
  &lt;/jdbc:initialize-database&gt;
  </code></pre>