github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/datasource/transaction/delay_transaction.go (about) 1 // Copyright 2021 ecodeclub 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package transaction 16 17 import ( 18 "context" 19 "database/sql" 20 "fmt" 21 "sync" 22 23 "github.com/ecodeclub/eorm/internal/datasource" 24 "go.uber.org/multierr" 25 ) 26 27 type DelayTxFactory struct{} 28 29 func (DelayTxFactory) TxOf(ctx Context, finder datasource.Finder) (datasource.Tx, error) { 30 return NewDelayTx(ctx, finder), nil 31 } 32 33 type DelayTx struct { 34 ctx Context 35 lock sync.RWMutex 36 txs map[string]datasource.Tx 37 finder datasource.Finder 38 } 39 40 func (t *DelayTx) findTgt(ctx context.Context, query datasource.Query) (datasource.TxBeginner, error) { 41 return t.finder.FindTgt(ctx, query) 42 } 43 44 func (t *DelayTx) findOrBeginTx(ctx context.Context, query datasource.Query) (datasource.Tx, error) { 45 t.lock.RLock() 46 tx, ok := t.txs[query.DB] 47 t.lock.RUnlock() 48 if ok { 49 return tx, nil 50 } 51 t.lock.Lock() 52 defer t.lock.Unlock() 53 if tx, ok = t.txs[query.DB]; ok { 54 return tx, nil 55 } 56 var err error 57 db, err := t.findTgt(ctx, query) 58 if err != nil { 59 return nil, err 60 } 61 tx, err = db.BeginTx(t.ctx.TxCtx, t.ctx.Opts) 62 if err != nil { 63 return nil, err 64 } 65 t.txs[query.DB] = tx 66 return tx, nil 67 } 68 69 func (t *DelayTx) Query(ctx context.Context, query datasource.Query) (*sql.Rows, error) { 70 // 防止 GetMulti 的查询重复创建多个事务 71 tx, err := t.findOrBeginTx(ctx, query) 72 if err != nil { 73 return nil, err 74 } 75 return tx.Query(ctx, query) 76 } 77 78 func (t *DelayTx) Exec(ctx context.Context, query datasource.Query) (sql.Result, error) { 79 tx, err := t.findOrBeginTx(ctx, query) 80 if err != nil { 81 return nil, err 82 } 83 return tx.Exec(ctx, query) 84 } 85 86 func (t *DelayTx) Commit() error { 87 var err error 88 for name, tx := range t.txs { 89 if er := tx.Commit(); er != nil { 90 err = multierr.Combine( 91 err, fmt.Errorf("masterslave DB name [%s] Commit error: %w", name, er)) 92 } 93 } 94 return err 95 } 96 97 func (t *DelayTx) Rollback() error { 98 var err error 99 for name, tx := range t.txs { 100 if er := tx.Rollback(); er != nil { 101 err = multierr.Combine( 102 err, fmt.Errorf("masterslave DB name [%s] Rollback error: %w", name, er)) 103 } 104 } 105 return err 106 } 107 108 func NewDelayTx(ctx Context, finder datasource.Finder) *DelayTx { 109 return &DelayTx{ 110 ctx: ctx, 111 finder: finder, 112 txs: make(map[string]datasource.Tx, 8), 113 } 114 }