title: Error while extracting DatabaseMetaData; nested exception is com.mysql.jdbc.exceptions.jdbc4. No operations allowed after connection closed
body: <p>I have a scheduled task running every 20 minutes which was configured through "@Scheduled(cron = "0 0/20 * * * ?")" based on spring boot. During this task, I need to search some data from mysql and then save them into redis. The following are my configure of database:</p>
  
  <pre><code>mybatis.configuration.map-underscore-to-camel-case=true
  spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  spring.datasource.initialSize=1
  spring.datasource.minIdle=1
  spring.datasource.maxActive=20
  spring.datasource.maxWait=60000
  spring.datasource.timeBetweenEvictionRunsMillis=60000
  spring.datasource.minEvictableIdleTimeMillis=300000
  spring.datasource.validationQuery=SELECT 'x'
  spring.datasource.testWhileIdle=true
  spring.datasource.testOnBorrow=false
  spring.datasource.testOnReturn=false
  spring.datasource.poolPreparedStatements=true
  spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
  spring.datasource.filters=stat,wall,slf4j
  spring.datasource.initialize=false
  </code></pre>
  
  <p>The mysql version is v5.7.19. My mysql-connector-java version is 5.1.41. My druid version is 1.0.29. JDK is 18.</p>
  
  <p>My problem is my scheduled task only can execute successfully sometimes. For example, my application runs 24 hours. There must be one or two times failed executing tasks. But I have another scheduled task which runs every 10 minutes, need to connect to the same mysql through the same configuration and it is successful every time.
  When the task failed, the log information are:</p>
  
  <pre><code>2018-02-14 02:20:00,003 [pool-5-thread-1] INFO  c.x.r.b.r.provider.YourFavoriates Method:executeItemInfoUpdateTaskRun Line:359 - Host IP 10.189.145.64: Scheduled task(executeItemInfoUpdateTask) is begining to save the available skuId information of all storeId from mysql to cache.
  2018-02-14 02:20:00,026 [pool-5-thread-1] INFO  o.s.b.f.xml.XmlBeanDefinitionReader Method:loadBeanDefinitions Line:317 - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
  2018-02-14 02:20:00,060 [pool-5-thread-1] INFO  o.s.j.support.SQLErrorCodesFactory Method:&lt;init&gt; Line:127 - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
  2018-02-14 02:20:00,067 [pool-5-thread-1] WARN  o.s.j.support.SQLErrorCodesFactory Method:getErrorCodes Line:218 - Error while extracting database name - falling back to empty error codes
  org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:342)
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:366)
      at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:212)
      at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:134)
      at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.&lt;init&gt;(SQLErrorCodeSQLExceptionTranslator.java:97)
      at org.mybatis.spring.MyBatisExceptionTranslator.initExceptionTranslator(MyBatisExceptionTranslator.java:87)
      at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:72)
      at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
      at com.sun.proxy.$Proxy86.selectOne(Unknown Source)
      at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
      at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
      at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
      at com.sun.proxy.$Proxy87.countForQueryAvailableSku(Unknown Source)
      at com.xstore.recommend.biz.recommend.provider.YourFavoriates.getAvailableItems(YourFavoriates.java:68)
      at com.xstore.recommend.biz.recommend.provider.YourFavoriates.executeItemInfoUpdateTaskRun(YourFavoriates.java:360)
      at com.xstore.recommend.biz.recommend.provider.YourFavoriates.executeItemInfoUpdateTask(YourFavoriates.java:467)
      at sun.reflect.GeneratedMethodAccessor126.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
      at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
      at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      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:425)
      at com.mysql.jdbc.Util.getInstance(Util.java:408)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
      at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1198)
      at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1193)
      at com.mysql.jdbc.ConnectionImpl.getMetaData(ConnectionImpl.java:2903)
      at com.mysql.jdbc.ConnectionImpl.getMetaData(ConnectionImpl.java:2898)
      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.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
      at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
      at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:70)
      at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
      at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
      at com.sun.proxy.$Proxy118.getMetaData(Unknown Source)
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:331)
      ... 28 common frames omitted
  Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 31,199,913 milliseconds ago.  The last packet sent successfully to the server was 31,199,929 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      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:425)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
      at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3746)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2509)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2501)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
      at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
      at sun.reflect.GeneratedMethodAccessor119.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:138)
      at com.sun.proxy.$Proxy119.execute(Unknown Source)
      at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63)
      at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
      at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
      at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
      at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
      at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
      at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
      at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
      at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
      at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
      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.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
      ... 21 common frames omitted
  Caused by: java.net.SocketException: 断开的管道
      at java.net.SocketOutputStream.socketWrite0(Native Method)
      at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
      at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
      at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
      at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
      at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3728)
      ... 46 common frames omitted
  2018-02-14 02:20:00,069 [pool-5-thread-1] ERROR c.x.r.b.r.provider.YourFavoriates Method:executeItemInfoUpdateTask Line:469 - Exception while executing executeItemInfoUpdateTaskRun.
  org.springframework.dao.RecoverableDataAccessException: 
  ### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 31,199,913 milliseconds ago.  The last packet sent successfully to the server was 31,199,929 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
  ### The error may exist in URL [jar:file:/home/export/App/intelligenceRS.man.7fresh.com/WEB-INF/lib/xstore-intelligence-service-1.0.0-SNAPSHOT.jar!/mapper/popMd/PopMdMapper.xml]
  ### The error may involve defaultParameterMap
  ### The error occurred while setting parameters
  ### SQL: select count(1) from search_base_data where           isIgnoreSearchVisible = 0 and has_stock = 1 and search_invisible = 1
  ### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 31,199,913 milliseconds ago.  The last packet sent successfully to the server was 31,199,929 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
  ; SQL []; The last packet successfully received from the server was 31,199,913 milliseconds ago.  The last packet sent successfully to the server was 31,199,929 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 31,199,913 milliseconds ago.  The last packet sent successfully to the server was 31,199,929 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
      at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
      at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
      at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
      at com.sun.proxy.$Proxy86.selectOne(Unknown Source)
      at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
      at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
      at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
      at com.sun.proxy.$Proxy87.countForQueryAvailableSku(Unknown Source)
      at com.xstore.recommend.biz.recommend.provider.YourFavoriates.getAvailableItems(YourFavoriates.java:68)
      at com.xstore.recommend.biz.recommend.provider.YourFavoriates.executeItemInfoUpdateTaskRun(YourFavoriates.java:360)
      at com.xstore.recommend.biz.recommend.provider.YourFavoriates.executeItemInfoUpdateTask(YourFavoriates.java:467)
      at sun.reflect.GeneratedMethodAccessor126.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
      at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
      at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 31,199,913 milliseconds ago.  The last packet sent successfully to the server was 31,199,929 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      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:425)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
      at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3746)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2509)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2501)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
      at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
      at sun.reflect.GeneratedMethodAccessor119.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:138)
      at com.sun.proxy.$Proxy119.execute(Unknown Source)
      at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63)
      at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
      at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
      at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
      at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
      at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
      at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
      at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
      at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
      at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
      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.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
      ... 21 common frames omitted
  Caused by: java.net.SocketException: 断开的管道
      at java.net.SocketOutputStream.socketWrite0(Native Method)
      at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
      at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
      at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
      at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
      at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3728)
      ... 46 common frames omitted
  </code></pre>
  
  <p>What is the real reason? I search the answer from the Internet. Many peple said to change the testOnBorrow to true, or extend the mysql connection timeout, etc. These answer cannot meet my problem. So, help me, please. I have been beset by this problem for a week.</p>