Communications link failure due to underlying exception

数据库为Mysql,当登录网站操作时出现以下异常信息:

 

No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error: ** BEGIN NESTED EXCEPTION ** com.mysql.jdbc.CommunicationsException MESSAGE: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.net.SocketException MESSAGE: Software caused connection abort: socket write error STACKTRACE: java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at java.io.BufferedOutputStream.flushBuffer(Unknown Source) at java.io.BufferedOutputStream.flush(Unknown Source) at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2692) at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2621) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1552) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666) at com.mysql.jdbc.Connection.execSQL(Connection.java:2994) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139) at
............
经查看相关资料,发现问题的原因如下:

   Mysql服务器默认的“wait_timeout”是8小时,如果connection空闲超过8个小时,Mysql将自动断开该 connection。在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0中的该connection仍然存在,如果这时有 Client请求connection,C3P0将该断开的Connection提供给Client,将会造成上面的异常。

可以这么解决该问题:
         使得Connection pools中connection的lifetime与Mysql的wait_timeout时间达到一致,或者测试Connection pools中connection的有效性。
在wait_timeout的时间为8小时的情况下:

C3P0增加以下配置信息:

  1. //set to 'SELECT 1'      
  2. preferredTestQuery = 'SELECT 1'   
  3. //set to something much less than wait_timeout, prevents connections from going stale   
  4. idleConnectionTestPeriod = 18000     
  5. //set to something slightly less than wait_timeout, preventing 'stale' connections from being //handed out   
  6. maxIdleTime = 25000   
  7. //if you can take the performance 'hit', set to "true"   
  8. testConnectionOnCheckout = true    

 

 

-----------------------------------------------------------------------------------------------------------------------------------

MySQL连接如果8小时未使用,在查询使用到该连接会报:

异常名称:com.mysql.jdbc.CommunicationsException
异常信息: Communications link failure due to underlying exception

如果是MySQL5以前的版本,需要修改连接池配置中的URL,添加autoReconnect=true

如果是MySQL5 以后的版本,需要修改my.cnf(或者my.ini)文件,在[mysqld]后面添加

wait_timeout = 172800
interactive-timeout = 172800

单位都是秒,记得必须都添加,否则不起作用,通过show variables查看wait_timeout的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`com.mysql.jdbc.CommunicationsException`是MySQL JDBC驱动程序中的一个异常,它表示在尝试连接到MySQL数据库时出现通信故障。这个异常通常是由于以下原因之一引起的: 1. 数据库服务器已关闭或未启动。 2. 数据库服务器上的端口号不正确。 3. 防火墙或其他网络设备阻止了连接。 4. 数据库服务器上的连接数已达到最大值。 要解决这个问题,可以尝试以下几个步骤: 1. 确保MySQL服务器正在运行,并且端口号正确。 2. 检查防火墙或其他网络设备是否阻止了连接。 3. 检查MySQL服务器的日志文件以获取更多信息。 4. 如果连接数已达到最大值,请增加MySQL服务器上的最大连接数。 以下是一个示例代码,可以用来连接MySQL数据库并捕获`com.mysql.jdbc.CommunicationsException`异常: ```java import java.sql.*; public class Example { public static void main(String[] args) { try { // 连接到MySQL数据库 String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "root"; String password = "mypassword"; Connection conn = DriverManager.getConnection(url, user, password); // 执行SQL查询 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable"); // 处理查询结果 while (rs.next()) { System.out.println(rs.getString("column1")); } // 关闭连接 rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { // 捕获CommunicationsException异常 if (e instanceof CommunicationsException) { System.out.println("通信故障:" + e.getMessage()); } else { e.printStackTrace(); } } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值