Skip to content

feat(server): support PostgreSQL connection termination detection#3266

Merged
BugsGuru merged 2 commits intomainfrom
feat-2727
Apr 21, 2026
Merged

feat(server): support PostgreSQL connection termination detection#3266
BugsGuru merged 2 commits intomainfrom
feat-2727

Conversation

@LordofAvernus
Copy link
Copy Markdown
Collaborator

@LordofAvernus LordofAvernus commented Apr 20, 2026

User description

Summary

  • Replace MySQL-specific _errors.Is(mysql.ErrInvalidConn, execErr) with unified isConnectionTerminatedError function
  • Support PostgreSQL termination detection via string matching on error messages ("57P01" SQLSTATE and "conn closed")
  • Add termination detection for execSQLs ExecErr branch (covers SQL execution-time termination in transaction mode)
  • Remove direct dependency on github.com/go-sql-driver/mysql package in sqled.go

Test plan

  • Unit tests: 16 cases covering MySQL positive (4), PostgreSQL positive (5), and negative matches (7)
  • go build ./sqle/server/... passes
  • go vet ./sqle/server/... passes
  • go test -run Test_isConnectionTerminatedError ./sqle/server/... all PASS

Closes https://github.com/actiontech/sqle-ee/issues/2727


Description

  • 添加统一的连接终止错误检测函数

  • 使用字符串匹配支持 MySQL 与 PostgreSQL

  • 替换原有 mysql 包错误检测逻辑

  • 新增单元测试覆盖多种错误场景


Diagram Walkthrough

flowchart LR
  A["\"sqled.go 文件修改\""] -- "新增统一检测函数" --> B["\"添加 isConnectionTerminatedError\""]
  B -- "替换旧错误判断" --> C["\"修改 SQL 执行流程\""]
  C -- "新增测试覆盖" --> D["\"新增单元测试文件\""]
Loading

File Walkthrough

Relevant files
Bug fix
sqled.go
修改 SQL 执行中的连接终止错误检测                                                                           

sqle/server/sqled.go

  • 引入字符串库以支持错误匹配
  • 新增 isConnectionTerminatedError 函数及详细注释
  • 修改 executeSQLBatch、execSQL、execSQLs 错误判断逻辑
+38/-4   
Tests
sqled_terminate_test.go
新增连接终止错误检测单元测试                                                                                     

sqle/server/sqled_terminate_test.go

  • 新增单元测试文件检测连接终止错误
  • 包含 MySQL 与 PostgreSQL 多场景测试用例
+92/-0   

…sConnectionTerminatedError

Support PostgreSQL connection termination detection alongside MySQL.
The new function uses string matching on error messages to identify
termination errors across database drivers and gRPC boundaries.

Issue: #2727
Cover MySQL positive matches (4 cases), PostgreSQL positive matches
(5 cases), and negative matches (7 cases) using map case pattern.

Issue: #2727
@actiontech-bot actiontech-bot requested a review from BugsGuru April 20, 2026 10:56
@github-actions
Copy link
Copy Markdown

PR Reviewer Guide 🔍

🎫 Ticket compliance analysis 🔶

2727 - Partially compliant

Compliant requirements:

  • 替换了原有 mysql 包错误检测逻辑
  • 新增了 isConnectionTerminatedError 函数用于统一错误检测
  • 添加了单元测试覆盖多种错误场景

Non-compliant requirements:

Requires further human verification:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ No major issues detected

@github-actions
Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
使用不区分大小写匹配

考虑将错误信息转换为统一的小写格式,然后使用不区分大小写的字符串匹配。这样可以提高对不同数据库驱动错误信息变体的识别能力,避免因大小写问题漏检错误。

sqle/server/sqled.go [275-298]

 func isConnectionTerminatedError(err error) bool {
 	if err == nil {
 		return false
 	}
-	errMsg := err.Error()
+	errMsg := strings.ToLower(err.Error())
 	if strings.Contains(errMsg, "invalid connection") {
 		return true
 	}
-	if strings.Contains(errMsg, "57P01") {
+	if strings.Contains(errMsg, "57p01") {
 		return true
 	}
 	if strings.Contains(errMsg, "conn closed") {
 		return true
 	}
 	return false
 }
Suggestion importance[1-10]: 7

__

Why: Converting the error message to lower case improves robustness in error matching, making the function more tolerant of case variations. This is a moderate quality improvement that aligns well with the existing function logic.

Medium
使用直接错误构造

建议使用_error.New_来构造错误对象,而不是通过_
fmt.Errorf_包装错误字符串,以避免丢失原始错误的格式信息。这样可以更精确地传递错误内容给检查函数,防止因格式问题产生误判。

sqle/server/sqled.go [774-776]

-if a.hasTermination() && isConnectionTerminatedError(fmt.Errorf(results.ExecErr.SqlExecErrMsg)) {
+if a.hasTermination() && isConnectionTerminatedError(_errors.New(results.ExecErr.SqlExecErrMsg)) {
 				executeSQL.ExecStatus = model.SQLExecuteStatusTerminateSucc
 			}
Suggestion importance[1-10]: 3

__

Why: Replacing fmt.Errorf with _errors.New offers little real benefit since both produce errors with similar string content. The improvement is marginal and does not significantly affect functionality.

Low

@BugsGuru BugsGuru merged commit 82d3f6e into main Apr 21, 2026
4 checks passed
@BugsGuru BugsGuru deleted the feat-2727 branch April 21, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants