解决C3P0连接MySQL链接失效的问题

  昨天把一个小项目挂服务器上,测试了项目的运行是可以的。第二天打开项目,一旦打开需要使用数据库的页面直接就挂了。报JDBC EXCEPTION,还提示去修改一个wait_timeout的字段。网上搜了下,这个wait_timeout是管理MySQL链接有效期的变量,可以通过以下语句查出MySQL链接的超时时间。

  

  此处wait_timeout是28800秒,也就是8个小时。

  C3P0作为连接池,它的最大的任务就是来管理链接的,可是实际上它并不知道这个链接是否还有效,所以,当MySQL某个链接的建立时间已经超过了wait_timeout的时候,那么这个链接实际上已经不能用了。而C3P0却还不知道,相当于C3P0持有了不能使用的链接。

  那么解决这个问题也就有两个思路了。

  1.延长MySQL的wait_timeout

  2.让C3P0主动去测试持有的链接是否有效

  考虑到实际中的应用,第一种思路其实并不是很好,因为C3P0作为连接池,理所应当地管理一切跟链接有关的东西,所以,从第二种思路入手比较合适些。

  关于链接失效的问题,C3P0官方文档给出了这样子的解决方式

  

Begin by setting testConnectionOnCheckout to true and get your application to run correctly and stably. If you are happy with your application's performance, you can stop here! This is the simplest, most reliable form of Connection-testing, but it does have a client-visible performance cost.

If you'd like to improve performance by eliminating Connection testing from clients' code path:

Set testConnectionOnCheckout to false

Set testConnectionOnCheckin to true

Set idleConnectionTestPeriod to 30, fire up you application and observe. This is a pretty robust setting, all Connections will tested on check-in and every 30 seconds thereafter while in the pool. Your application should experience broken or stale Connections only very rarely, and the pool should recover from a database shutdown and restart quickly. But there is some overhead associated with all that Connection testing.

  简单点概括,在配置C3P0的时候,如果要防止链接失效的情况,可以配置一个testConnectionOnCheckout的属性,当时该属性会比较大的影响性能。

  如果比较在意性能的,可以设置testConnectionOnCheckout为false,设置testConnectionOnCheckin为true,设置idleConnectionTestPeriod为30秒

  spring中配置数据源

  

  properties文件

  

   

  启动项目,通过输出日志可看到,每30秒连接池会自动测试连接了。

  

转载于:https://www.cnblogs.com/xiaosheblog/p/7637612.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C3P0是一个Java数据库连接池,可以用于管理和维护与数据库连接。下面是使用C3P0连接MySQL数据库的步骤: 1. 添加C3P0依赖库到项目中。可以在Maven中添加以下依赖: ```xml <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 2. 在项目中添加C3P0配置文件c3p0-config.xml。以下是一个简单的配置示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb</property> <property name="user">root</property> <property name="password">password</property> <property name="maxPoolSize">10</property> <property name="minPoolSize">5</property> <property name="acquireIncrement">1</property> <property name="maxStatements">0</property> <property name="idleConnectionTestPeriod">30</property> <property name="maxIdleTime">600</property> </default-config> </c3p0-config> ``` 其中,`driverClass`指定MySQL的JDBC驱动程序类,`jdbcUrl`指定MySQL连接URL,`user`和`password`指定数据库的用户名和密码。其他属性如`maxPoolSize`、`minPoolSize`等可以根据需要进行调整。 3. 在Java代码中获取C3P0连接池,然后使用连接池获取数据库连接,并执行SQL语句。以下是一个简单的示例: ```java ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setConfigFile("c3p0-config.xml"); Connection conn = cpds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable"); while (rs.next()) { // 处理结果集 } rs.close(); stmt.close(); conn.close(); ``` 在上面的代码中,`ComboPooledDataSource`是C3P0提供的连接池类,`setConfigFile`方法指定了C3P0配置文件的路径。然后就可以使用`getConnection`方法获取连接使用`createStatement`方法创建`Statement`对象,然后执行SQL语句并处理结果集。最后记得关闭连接、语句和结果集。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值