阶段八:完成统一事务repository收口
This commit is contained in:
1 parent
a1bf5078d8
commit
0e8cea087b
5 files changed
+131
-73
No files matched your search
@@ -75,6 +75,8 @@ type Server struct {
|
||||
Addr string
|
||||
// applications 保存统一装配的应用服务实例。
|
||||
applications *applicationServices
|
||||
// transactionRepository 提供统一事务执行所需的最小持久化能力。
|
||||
transactionRepository transactionRepository
|
||||
|
||||
publishMu sync.Mutex
|
||||
publishCancels map[string]publishBatchWorker
|
||||
@@ -151,6 +153,7 @@ func New(store *db.Store, manager *account.Manager, secure bool, webDir, addr st
|
||||
}
|
||||
}
|
||||
server.applications = newApplicationServices(server)
|
||||
server.transactionRepository = newStoreTransactionRepository(store)
|
||||
return server, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// transactionRepository 定义 Server 统一事务入口所需的最小持久化能力。
|
||||
type transactionRepository interface {
|
||||
// WithTransaction 在一个事务中执行工作并负责提交或回滚。
|
||||
WithTransaction(ctx context.Context, work func(*sql.Tx) error) error
|
||||
}
|
||||
|
||||
// storeTransactionRepository 将完整 Store 适配为事务执行窄 repository。
|
||||
type storeTransactionRepository struct {
|
||||
// store 保存数据库聚合入口,仅在适配器内部使用。
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
// WithTransaction 创建、提交或回滚数据库事务。
|
||||
func (r storeTransactionRepository) WithTransaction(ctx context.Context, work func(*sql.Tx) error) error {
|
||||
if r.store == nil || r.store.DB == nil {
|
||||
return errors.New("事务 repository 未初始化")
|
||||
}
|
||||
// tx 是本次用例使用的数据库事务。
|
||||
// err 表示事务创建错误。
|
||||
tx, err := r.store.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// committed 标识事务是否已经成功提交,避免提交失败后重复处理回滚错误。
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
if err := work(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
// err 表示事务提交错误。
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
committed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// newStoreTransactionRepository 从完整 Store 构造事务执行窄 repository。
|
||||
func newStoreTransactionRepository(store *db.Store) transactionRepository {
|
||||
if store == nil || store.DB == nil {
|
||||
return nil
|
||||
}
|
||||
return storeTransactionRepository{store: store}
|
||||
}
|
||||
|
||||
// 确保 Store 适配器始终覆盖事务执行边界。
|
||||
var _ transactionRepository = storeTransactionRepository{}
|
||||
@@ -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