-
Notifications
You must be signed in to change notification settings - Fork 402
/
lockedtx.go
42 lines (34 loc) · 933 Bytes
/
lockedtx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"sync"
"storj.io/storj/satellite/console"
)
// BeginTransaction is a method for opening transaction
func (m *lockedConsole) BeginTx(ctx context.Context) (console.DBTx, error) {
m.Lock()
db, err := m.db.BeginTx(ctx)
txlocked := &lockedConsole{&sync.Mutex{}, db}
return &lockedTx{m, txlocked, db, sync.Once{}}, err
}
// lockedTx extends Database with transaction scope
type lockedTx struct {
parent *lockedConsole
*lockedConsole
tx console.DBTx
once sync.Once
}
// Commit is a method for committing and closing transaction
func (db *lockedTx) Commit() error {
err := db.tx.Commit()
db.once.Do(db.parent.Unlock)
return err
}
// Rollback is a method for rollback and closing transaction
func (db *lockedTx) Rollback() error {
err := db.tx.Rollback()
db.once.Do(db.parent.Unlock)
return err
}