Skip to content

Commit 2491655

Browse files
committed
[feat] support mongo transaction
1 parent 6c30273 commit 2491655

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

pkg/database/mongo/client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ func (d *DB) Ping() error {
122122
return d.client.Ping(2)
123123
}
124124

125+
// DoTransaction executes the given callback within a MongoDB transaction.
126+
// All operations performed using the sessCtx within the callback will be
127+
// part of the same transaction. The transaction is automatically committed
128+
// if the callback returns nil, or aborted if it returns an error.
129+
// Note: MongoDB transactions require a replica set or sharded cluster.
130+
func (d *DB) DoTransaction(ctx context.Context, callback func(sessCtx context.Context) (interface{}, error)) (interface{}, error) {
131+
return d.client.DoTransaction(ctx, callback)
132+
}
133+
125134
// client return mongodb connection instance handler
126135
func client(config *Config) (*qmgo.Client, func() error) {
127136
if config.MaxPoolSize < config.MinPoolSize {

0 commit comments

Comments
 (0)