refactor(db): 优化测试事务中的互斥锁使用
- 重构 queryWithRetry 和 execWithRetry 方法中的互斥锁逻辑,提升代码可读性 - 确保在测试事务中对 testMu 的锁定和解锁操作更加清晰,避免潜在的并发问题
This commit is contained in:
+15
-5
@@ -63,15 +63,21 @@ func (that *HoTimeDB) queryWithRetry(query string, retried bool, args ...interfa
|
||||
processedArgs := that.processArgs(args)
|
||||
|
||||
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...)
|
||||
that.LastErr.SetError(err)
|
||||
if err != nil {
|
||||
if that.testMu != nil { that.testMu.Unlock() }
|
||||
if that.testMu != nil {
|
||||
that.testMu.Unlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
result := that.Row(resl)
|
||||
if that.testMu != nil { that.testMu.Unlock() }
|
||||
if that.testMu != nil {
|
||||
that.testMu.Unlock()
|
||||
}
|
||||
return result
|
||||
} else if that.Tx != nil {
|
||||
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)
|
||||
|
||||
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...)
|
||||
if that.testMu != nil { that.testMu.Unlock() }
|
||||
if that.testMu != nil {
|
||||
that.testMu.Unlock()
|
||||
}
|
||||
} else if that.Tx != nil {
|
||||
resl, e = that.Tx.Exec(query, processedArgs...)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user