refactor(db): 优化测试事务中的互斥锁使用

- 重构 queryWithRetry 和 execWithRetry 方法中的互斥锁逻辑,提升代码可读性
- 确保在测试事务中对 testMu 的锁定和解锁操作更加清晰,避免潜在的并发问题
This commit is contained in:
2026-03-20 13:52:23 +08:00
parent 808a782db5
commit 9c5a7630a4
+15 -5
View File
@@ -63,15 +63,21 @@ func (that *HoTimeDB) queryWithRetry(query string, retried bool, args ...interfa
processedArgs := that.processArgs(args) processedArgs := that.processArgs(args)
if that.testTx != nil { if that.testTx != nil {
if that.testMu != nil { that.testMu.Lock() } if that.testMu != nil {
that.testMu.Lock()
}
resl, err = that.testTx.Query(query, processedArgs...) resl, err = that.testTx.Query(query, processedArgs...)
that.LastErr.SetError(err) that.LastErr.SetError(err)
if err != nil { if err != nil {
if that.testMu != nil { that.testMu.Unlock() } if that.testMu != nil {
that.testMu.Unlock()
}
return nil return nil
} }
result := that.Row(resl) result := that.Row(resl)
if that.testMu != nil { that.testMu.Unlock() } if that.testMu != nil {
that.testMu.Unlock()
}
return result return result
} else if that.Tx != nil { } else if that.Tx != nil {
resl, err = that.Tx.Query(query, processedArgs...) resl, err = that.Tx.Query(query, processedArgs...)
@@ -129,9 +135,13 @@ func (that *HoTimeDB) execWithRetry(query string, retried bool, args ...interfac
processedArgs := that.processArgs(args) processedArgs := that.processArgs(args)
if that.testTx != nil { if that.testTx != nil {
if that.testMu != nil { that.testMu.Lock() } if that.testMu != nil {
that.testMu.Lock()
}
resl, e = that.testTx.Exec(query, processedArgs...) resl, e = that.testTx.Exec(query, processedArgs...)
if that.testMu != nil { that.testMu.Unlock() } if that.testMu != nil {
that.testMu.Unlock()
}
} else if that.Tx != nil { } else if that.Tx != nil {
resl, e = that.Tx.Exec(query, processedArgs...) resl, e = that.Tx.Exec(query, processedArgs...)
} else { } else {