```
refactor(db): 优化事务错误状态管理 - 添加 txFailed 指针字段用于跟踪事务内SQL执行状态 - 确保事务错误状态在 HoTimeDB 实例间正确共享 - 当事务内SQL出错时强制回滚以维护数据一致性 ```
This commit is contained in:
@@ -20,24 +20,25 @@ type HoTimeDB struct {
|
|||||||
ContextBase
|
ContextBase
|
||||||
DBName string
|
DBName string
|
||||||
*cache.HoTimeCache
|
*cache.HoTimeCache
|
||||||
Log *logrus.Logger
|
Log *logrus.Logger
|
||||||
Type string // 数据库类型: mysql, sqlite3, postgres
|
Type string // 数据库类型: mysql, sqlite3, postgres
|
||||||
Prefix string
|
Prefix string
|
||||||
LastQuery string
|
LastQuery string
|
||||||
LastData []interface{}
|
LastData []interface{}
|
||||||
ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
|
ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
|
||||||
LastErr *Error
|
LastErr *Error
|
||||||
limit Slice
|
limit Slice
|
||||||
*sql.Tx //事务对象
|
*sql.Tx //事务对象
|
||||||
SlaveDB *sql.DB // 从数据库
|
SlaveDB *sql.DB // 从数据库
|
||||||
Mode int // mode为0生产模式,1为测试模式,2为开发模式
|
Mode int // mode为0生产模式,1为测试模式,2为开发模式
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
limitMu sync.Mutex
|
limitMu sync.Mutex
|
||||||
Dialect Dialect // 数据库方言适配器
|
Dialect Dialect // 数据库方言适配器
|
||||||
testTx *sql.Tx // 测试事务:设置后所有操作都在此事务内,测试结束回滚
|
testTx *sql.Tx // 测试事务:设置后所有操作都在此事务内,测试结束回滚
|
||||||
testMu *sync.Mutex // 保护 testTx 单连接不被并发访问
|
testMu *sync.Mutex // 保护 testTx 单连接不被并发访问
|
||||||
testLogBuf *bytes.Buffer // 测试模式:SQL 日志写入此 buffer 而非直接打印,失败时才输出
|
testLogBuf *bytes.Buffer // 测试模式:SQL 日志写入此 buffer 而非直接打印,失败时才输出
|
||||||
testLogBufMu sync.Mutex // 保护 testLogBuf 并发写入
|
testLogBufMu sync.Mutex // 保护 testLogBuf 并发写入
|
||||||
|
txFailed *bool // 事务内是否有SQL出错,出错则事务必须回滚(指针确保按值传递 HoTimeDB 时状态共享)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConnect 设置数据库配置连接
|
// SetConnect 设置数据库配置连接
|
||||||
|
|||||||
Reference in New Issue
Block a user