quartz和数据库断连接的解决办法

时间:2022-03-20 21:50:46

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 109,769,894 milliseconds ago.  The last packet sent successfully to the server was 109,769,894 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) ~[na:1.7.0_67]
         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_67]
         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_67]
         at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_67]
         at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
         at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
         at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3609) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
         at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2417) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
         at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
         at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2531) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
         at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4852) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]
         at com.mchange.v2.c3p0.impl.NewProxyConnection.setAutoCommit(NewProxyConnection.java:881) ~[c3p0-0.9.1.1.jar:0.9.1.1]
         at org.quartz.impl.jdbcjobstore.AttributeRestoringConnectionInvocationHandler.restoreOriginalAtributes(AttributeRestoringConnectionInvocationHandler.java:141) ~[quartz-2.2.1.jar:na]
         at org.quartz.impl.jdbcjobstore.JobStoreSupport.cleanupConnection(JobStoreSupport.java:3600) [quartz-2.2.1.jar:na]
         at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3819) [quartz-2.2.1.jar:na]
         at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTriggers(JobStoreSupport.java:2756) [quartz-2.2.1.jar:na]
         at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:272) [quartz-2.2.1.jar:na]
Caused by: java.net.SocketException: Broken pipe
         at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.7.0_67]
         at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113) ~[na:1.7.0_67]
         at java.net.SocketOutputStream.write(SocketOutputStream.java:159) ~[na:1.7.0_67]
         at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82) ~[na:1.7.0_67]
         at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140) ~[na:1.7.0_67]
         at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3591) ~[mysql-connector-java-5.1.35-bin.jar:5.1.35]


quartz将配置保存在数据库时,如果数据库和quartz断连接,那么quartz读取数据库时会发生异常。

做如下配置:

org.quartz.dataSource.myDS.driver = org.mariadb.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/dxtdatabase?useUnicode=true&characterEnco
ding=UTF-8&autoReconnect=true&autoReconnectForPools=true
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = 123456
org.quartz.dataSource.myDS.maxConnections = 30
org.quartz.dataSource.myDS.validateOnCheckout = true
org.quartz.dataSource.myDS.validationQuery = select 1


org.quartz.dataSource.myDS.validateOnCheckout = true
org.quartz.dataSource.myDS.validationQuery = select 1

这两个配置的意思是使用连接时先判断是否有效。


&autoReconnect=true&autoReconnectForPools=true

这个配置没有效果,是开始尝试的第一个方案,没有坏的影响就没删。