报错信息:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 
The last packet successfully received from the server was 20,820,001 milliseconds ago. 
 The last packet sent successfully to the server was 20,820,002 milliseconds ago.

原因是mysql有个wait_timeout参数默认是8小时,如果8小时没有交互就将连接关闭。

解决办法就是使用数据库连接池,比如dbcp

pom:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-dbcp2</artifactId>
  <version>2.5.0</version>
</dependency>
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverName);
dataSource.setUrl(dbURL);
if (username != null) {
    dataSource.setUsername(username);
}
dataSource.setPassword(password);

dataSource.setTestWhileIdle(true);

Connection connection = dataSource.getConnection();