From 277e244e801a083daecf3983b2b87bb322634ecd Mon Sep 17 00:00:00 2001 From: hoteas <925970985@qq.com> Date: Sat, 11 Apr 2026 22:34:13 +0800 Subject: [PATCH] =?UTF-8?q?```=20refactor(db):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=8B=E5=8A=A1=E9=94=99=E8=AF=AF=E7=8A=B6=E6=80=81=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 txFailed 指针字段用于跟踪事务内SQL执行状态 - 确保事务错误状态在 HoTimeDB 实例间正确共享 - 当事务内SQL出错时强制回滚以维护数据一致性 ``` --- db/db.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/db/db.go b/db/db.go index 2ec76a9..e535d14 100644 --- a/db/db.go +++ b/db/db.go @@ -20,24 +20,25 @@ type HoTimeDB struct { ContextBase DBName string *cache.HoTimeCache - Log *logrus.Logger - Type string // 数据库类型: mysql, sqlite3, postgres - Prefix string - LastQuery string - LastData []interface{} - ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB) - LastErr *Error - limit Slice - *sql.Tx //事务对象 - SlaveDB *sql.DB // 从数据库 - Mode int // mode为0生产模式,1为测试模式,2为开发模式 - mu sync.RWMutex - limitMu sync.Mutex - Dialect Dialect // 数据库方言适配器 - testTx *sql.Tx // 测试事务:设置后所有操作都在此事务内,测试结束回滚 - testMu *sync.Mutex // 保护 testTx 单连接不被并发访问 + Log *logrus.Logger + Type string // 数据库类型: mysql, sqlite3, postgres + Prefix string + LastQuery string + LastData []interface{} + ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB) + LastErr *Error + limit Slice + *sql.Tx //事务对象 + SlaveDB *sql.DB // 从数据库 + Mode int // mode为0生产模式,1为测试模式,2为开发模式 + mu sync.RWMutex + limitMu sync.Mutex + Dialect Dialect // 数据库方言适配器 + testTx *sql.Tx // 测试事务:设置后所有操作都在此事务内,测试结束回滚 + testMu *sync.Mutex // 保护 testTx 单连接不被并发访问 testLogBuf *bytes.Buffer // 测试模式:SQL 日志写入此 buffer 而非直接打印,失败时才输出 testLogBufMu sync.Mutex // 保护 testLogBuf 并发写入 + txFailed *bool // 事务内是否有SQL出错,出错则事务必须回滚(指针确保按值传递 HoTimeDB 时状态共享) } // SetConnect 设置数据库配置连接