-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction.go
74 lines (63 loc) · 1.96 KB
/
transaction.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
// Copyright 2014 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.
package rdb
import (
"context"
"errors"
)
// Although nested transactions are unsupported, savepoints are supported.
// A transaction should end with either a Commit() or Rollback() call.
type Transaction struct {
cp *ConnPool
conn DriverConn
done bool
level IsolationLevel
}
var transactionClosed = errors.New("Transaction already closed.")
func (tran *Transaction) Query(ctx context.Context, cmd *Command, params ...Param) (*Result, error) {
if tran.done {
return nil, transactionClosed
}
return tran.cp.query(ctx, true, tran.conn, cmd, nil, params...)
}
// Commit commits a one or more queries. If no queries have been run this
// just returns the connection without any action being taken.
func (tran *Transaction) Commit() error {
if tran.done {
return transactionClosed
}
tran.done = true
err := tran.conn.Commit()
tran.cp.releaseConn(tran.conn, tran.conn.Status() != StatusReady)
return err
}
// Rollback rolls back one or more queries. If no queries have been run this
// just returns the connection without any action being taken.
func (tran *Transaction) Rollback() error {
return tran.RollbackTo("")
}
// Rollback to an existing savepoint. Commit or Rollback should still
// be called after calling RollbackTo.
func (tran *Transaction) RollbackTo(savepoint string) error {
if tran.done {
return transactionClosed
}
err := tran.conn.Rollback(savepoint)
if len(savepoint) == 0 {
tran.done = true
tran.cp.releaseConn(tran.conn, tran.conn.Status() != StatusReady)
}
return err
}
// Create a save point in the transaction.
func (tran *Transaction) SavePoint(name string) error {
if tran.done {
return transactionClosed
}
return tran.conn.SavePoint(name)
}
// Return true if the transaction has not been either commited or entirely rolled back.
func (tran *Transaction) Active() bool {
return !tran.done
}