Caused by:java.net.SocketException: Connection reset

11-Apr-2023 21:08:06.550 涓ラ噸 [Druid-ConnectionPool-Create-919379416] com.alibaba.druid.pool.DruidDataSource.error create connection SQLException, url: jdbc:mysql://localhost:3306/renthouse?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC, errorCode 0, state 08S01

 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

 

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

  at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)

  at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)

  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)

  at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)

  at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)

  at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)

  at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1596)

  at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1662)

  at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2697)

 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

 

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

  at sun.reflect.GeneratedConstructorAccessor16.newInstance(Unknown Source)

  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)

  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)

  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)

  at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)

  at com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:562)

  at com.mysql.cj.protocol.a.NativeProtocol.readServerCapabilities(NativeProtocol.java:514)

  at com.mysql.cj.protocol.a.NativeProtocol.beforeHandshake(NativeProtocol.java:404)

  at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1450)

  at com.mysql.cj.NativeSession.connect(NativeSession.java:165)

  at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)

  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)

  ... 6 more

 Caused by: java.net.SocketException: Connection reset

  at java.net.SocketInputStream.read(SocketInputStream.java:210)

  at java.net.SocketInputStream.read(SocketInputStream.java:141)

  at com.mysql.cj.protocol.ReadAheadInputStream.fill(ReadAheadInputStream.java:107)

  at com.mysql.cj.protocol.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:150)

  at com.mysql.cj.protocol.ReadAheadInputStream.read(ReadAheadInputStream.java:180)

  at java.io.FilterInputStream.read(FilterInputStream.java:133)

  at com.mysql.cj.protocol.FullReadInputStream.readFully(FullReadInputStream.java:64)

  at com.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:63)

  at com.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:45)

  at com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:556)

  ... 12 more

`JedisConnectionException: java.net.SocketException: Connection reset by peer: socket write error` 异常表明Redis客户端尝试与服务器通信时出现了问题,通常是因为网络断开或者是客户端在发送请求后没有及时从连接池中正确释放资源,导致服务器关闭了连接。 1. **网络问题**:可能是由于临时的网络波动或者服务器端重启,导致连接中断。客户端需要重新建立连接才能继续执行操作。 2. **资源管理问题**:如您提到的,如果在使用完Redis后没有正确地调用 `returnResource()` 方法,可能会造成连接未被释放,从而引发连接重置异常。每次执行完Redis操作后确保调用这个方法来归还连接资源是关键。 3. **代码实现检查**:检查您的代码逻辑,确认在每个操作后是否确实调用了 `returnResource()` 函数,并传入正确的参数(如果`shardedJedis` 是健康的,则`broken` 应该为 `false`)。另外,确认 `returnBrokenResource()` 只在连接被认为已损坏(比如抛出了异常)时才被调用。 修复此问题的一种可能方法是在finally块中自动调用返回资源的方法,以确保无论操作成功与否都能释放连接: ```java try { // Redis操作... } catch (JedisConnectionException e) { // 如果遇到异常,标记连接为损坏并返回 returnResource(shardedJedis, true); throw e; // 或者记录错误并重新抛出 } finally { returnResource(shardedJedis, false); // 操作完成后正常返回 } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值