Skip to content

Commit fa72414

Browse files
committed
fix(server): improve error handling for transaction execution
(cherry picked from commit b462f4c)
1 parent f82bfd4 commit fa72414

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

sqle/model/task.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
)
4242

4343
const TaskExecResultOK = "OK"
44+
const TaskExecResultRollback = "The transaction has been rolled back"
4445

4546
const ExecModeSqlFile = "sql_file"
4647
const ExecModeSqls = "sqls"

sqle/server/sqled.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -714,24 +714,28 @@ func (a *action) execSQLs(executeSQLs []*model.ExecuteSQL) error {
714714

715715
results, txErr := a.plugin.Tx(context.TODO(), qs...)
716716
for idx, executeSQL := range executeSQLs {
717+
if results != nil && idx < len(results.ExecResult) {
718+
rowAffects, _ := results.ExecResult[idx].RowsAffected()
719+
executeSQL.RowAffects = rowAffects
720+
}
717721
if txErr != nil {
718722
executeSQL.ExecStatus = model.SQLExecuteStatusFailed
719723
executeSQL.ExecResult = txErr.Error()
720724
if a.hasTermination() && _errors.Is(mysql.ErrInvalidConn, txErr) {
721725
executeSQL.ExecStatus = model.SQLExecuteStatusTerminateSucc
722-
if idx >= len(results) {
723-
continue
724-
}
725-
if results[idx] == nil {
726-
continue
727-
}
728-
rowAffects, _ := results[idx].RowsAffected()
729-
executeSQL.RowAffects = rowAffects
730726
}
731727
continue
732728
}
733-
rowAffects, _ := results[idx].RowsAffected()
734-
executeSQL.RowAffects = rowAffects
729+
if results != nil && results.ExecErr != nil {
730+
if results.ExecErr.ErrSqlIndex == uint32(idx) {
731+
executeSQL.ExecStatus = model.SQLExecuteStatusFailed
732+
executeSQL.ExecResult = results.ExecErr.SqlExecErrMsg
733+
} else {
734+
executeSQL.ExecStatus = model.SQLExecuteStatusFailed
735+
executeSQL.ExecResult = model.TaskExecResultRollback
736+
}
737+
continue
738+
}
735739
executeSQL.ExecStatus = model.SQLExecuteStatusSucceeded
736740
executeSQL.ExecResult = model.TaskExecResultOK
737741
}
@@ -741,6 +745,9 @@ func (a *action) execSQLs(executeSQLs []*model.ExecuteSQL) error {
741745
if txErr != nil {
742746
return txErr
743747
}
748+
if results != nil && results.ExecErr != nil {
749+
return fmt.Errorf("sql execute err msg: %v", results.ExecErr.SqlExecErrMsg)
750+
}
744751
return nil
745752
}
746753

0 commit comments

Comments
 (0)