【解决】SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out

我在用quartz开发多线程任务时,使用的c3p0连接池,查看日志发现错误日志如下:

2020-02-11 00:15:27.260 [SchedulerFactory_Worker-1] ERROR 插入失败:nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 926653ms.
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 926653ms.

原因:连接超时,应该是使用了无效的连接没有释放或出现错误,导致客户端再次获取时候连接不可用。

解决办法:

    重启应用,但也不是个事情,还会出现。

    根源解决方法,修改c3p0配置,因为是使用了错误的c3p0连接,需要检测是否连接可用,如果不可用扔掉换新的。

修改添加c3p0配置属性:

<property name="idleConnectionTestPeriod">60</property>
<property name="testConnectionOnCheckin">true</property>
<property name="testConnectionOnCheckout">true</property>

testConnectionOnCheckout :true表示在每次从pool内checkout连接的时候测试其有效性,这是个同步操作,因此应用端的每次数据库调用,都会先通过测试sql测试其有效性,如果连接无效,会关闭此连接并剔除出pool,并尝试从pool内取其他连接,默认为false,此特性要慎用,会造成至少多一倍的数据库调用。

testConnectionOnCheckin :true表示每次把连接checkin到pool里的时候测试其有效性,因为是个事后操作,所以是异步的,应用端不需要等待测试结果,但同样会造成至少多一倍的数据库调用。

idleConnectionTestPeriod :C3P0会有一个Task检测pool内的连接是否正常,此参数就是Task运行的频率。默认值为0,表示不进行检测。

配置完后,再次启动,不再出现

发布了106 篇原创文章 · 获赞 29 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_18769269/article/details/104262916