阶段八:完成统一事务repository收口
This commit is contained in:
1 parent
a1bf5078d8
commit
0e8cea087b
5 files changed
+131
-73
No files matched your search
@@ -3,29 +3,16 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// withTransaction 在 Server 应用层统一创建、提交或回滚数据库事务。
|
||||
// 调用方只描述事务内的数据库操作,不再直接管理事务生命周期。
|
||||
func (s *Server) withTransaction(ctx context.Context, work func(*sql.Tx) error) error {
|
||||
// tx 是本次用例使用的数据库事务。
|
||||
tx, err := s.Store.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
// repository 是当前 Server 装配的事务执行边界。
|
||||
repository := s.transactionRepository
|
||||
if repository == nil {
|
||||
return errors.New("事务 repository 未初始化")
|
||||
}
|
||||
// committed 标识事务是否已经成功提交,避免提交失败后重复处理回滚错误。
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
if err := work(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
committed = true
|
||||
return nil
|
||||
return repository.WithTransaction(ctx, work)
|
||||
}
|
||||
Reference in new issue
Block a user