資料庫事務耗時過長導致Could not retrieve transaction read-only status from server異常

翎野君發表於2024-10-23

背景

[11-06 02:02:09:005] [ERROR] - DruidDataSource - discard connection
java.sql.SQLException: Could not retrieve transaction read-only status from server
 
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
 
The last packet successfully received from the server was 1,114,012 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
 
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

最開始只看到Could not retrieve transation read-only status server沒看到下面的Communications link failure,以為是在程式碼中手動設定事務狀態為只讀或者事務回滾,後來查程式碼也沒發現。
當看到Communications link failure意識到連線異常,但與當前方法連線同一個庫的其他方法可以執行成功,所以資料庫服務也是正常的。

最終發現原因:事務內部處理邏輯過多,導致大事務執行時間過長,超過了mysql連線的超時時間啊in,導致服務端丟棄連線後,過了很久事務才執行完畢。

問題原因

wait_timeout 設定過小,mysql自動丟棄連線,但是程式端沒有超時,造成依然在這個被丟棄的連線上執行語句

解決方案

設定mysql wait_timeout 引數
show global variables like '%timeout%';
SET GLOBAL wait_timeout=86400;
此處的單位應該秒。

  

相關連結

https://segmentfault.com/a/1190000020125846

https://blog.csdn.net/u013097383/article/details/88581938

https://blog.csdn.net/qq_38214534/article/details/106021187

https://study.sf.163.com/documents/read/service_support/dsc-p-a-0149

本篇文章如有幫助到您,請給「翎野君」點個贊,感謝您的支援。

首發連結:https://www.cnblogs.com/lingyejun/p/18498531

相關文章