This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
/
tx.go
105 lines (89 loc) · 3.13 KB
/
tx.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2016-2018 Stratumn SAS. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package postgresstore
import (
"database/sql"
"github.com/stratumn/go-core/monitoring"
"github.com/stratumn/go-core/monitoring/errorcode"
"github.com/stratumn/go-core/store"
"github.com/stratumn/go-core/types"
)
// TxFactory creates and manages transactions.
type TxFactory interface {
NewTx() (*sql.Tx, error)
CommitTx(tx *sql.Tx) error
RollbackTx(tx *sql.Tx, err error)
}
// StandardTxFactory is just a wrapper around SQL transactions.
type StandardTxFactory struct {
db *sql.DB
}
// NewStandardTxFactory creates a transaction factory using the underlying DB.
func NewStandardTxFactory(db *sql.DB) TxFactory {
return &StandardTxFactory{db: db}
}
// NewTx creates a new DB transaction.
func (f *StandardTxFactory) NewTx() (*sql.Tx, error) {
tx, err := f.db.Begin()
if err != nil {
return nil, types.WrapError(err, errorcode.Unavailable, store.Component, "could not create tx")
}
return tx, nil
}
// CommitTx commits the transaction to the DB.
func (f *StandardTxFactory) CommitTx(tx *sql.Tx) error {
err := tx.Commit()
if err != nil {
return types.WrapError(err, errorcode.Internal, store.Component, "could not commit tx")
}
return nil
}
// RollbackTx rolls back the transaction and logs errors.
func (f *StandardTxFactory) RollbackTx(tx *sql.Tx, _ error) {
err := tx.Rollback()
if err != nil {
monitoring.LogEntry().Warnf("Error during transaction rollback: %s", err.Error())
}
}
// SingletonTxFactory uses a single transaction under the hood.
// It can be used for batches since a transaction cannot contain
// sub-transactions.
type SingletonTxFactory struct {
tx *sql.Tx
rollback bool
err error
}
// NewSingletonTxFactory creates a transaction factory using the underlying
// transaction.
// It makes sure all the work is done in a single transaction instead of many
// (since a transaction cannot contain nested sub-transactions).
func NewSingletonTxFactory(tx *sql.Tx) TxFactory {
return &SingletonTxFactory{tx: tx}
}
// NewTx returns the DB transaction.
// Clients should not directly call tx.Commit() or tx.Rollback().
func (f *SingletonTxFactory) NewTx() (*sql.Tx, error) {
return f.tx, nil
}
// CommitTx does nothing. The owner of the SingletonTxFactory will commit
// directly on the tx object when ready (batch.Write()).
func (f *SingletonTxFactory) CommitTx(tx *sql.Tx) error {
return nil
}
// RollbackTx marks the transaction as rolled back.
// The owner of the SingletonTxFactory should rollback the transaction.
func (f *SingletonTxFactory) RollbackTx(tx *sql.Tx, err error) {
f.rollback = true
f.err = err
}