-
Notifications
You must be signed in to change notification settings - Fork 72
/
tx.go
96 lines (78 loc) · 2.46 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
package golangNeo4jBoltDriver
import (
"github.com/johnnadratowski/golang-neo4j-bolt-driver/errors"
"github.com/johnnadratowski/golang-neo4j-bolt-driver/log"
"github.com/johnnadratowski/golang-neo4j-bolt-driver/structures/messages"
)
// Tx represents a transaction
type Tx interface {
// Commit commits the transaction
Commit() error
// Rollback rolls back the transaction
Rollback() error
}
type boltTx struct {
conn *boltConn
closed bool
}
func newTx(conn *boltConn) *boltTx {
return &boltTx{
conn: conn,
}
}
// Commit commits and closes the transaction
func (t *boltTx) Commit() error {
if t.closed {
return errors.New("Transaction already closed")
}
if t.conn.statement != nil {
if err := t.conn.statement.Close(); err != nil {
return errors.Wrap(err, "An error occurred closing open rows in transaction Commit")
}
}
successInt, pullInt, err := t.conn.sendRunPullAllConsumeSingle("COMMIT", nil)
if err != nil {
return errors.Wrap(err, "An error occurred committing transaction")
}
success, ok := successInt.(messages.SuccessMessage)
if !ok {
return errors.New("Unrecognized response type committing transaction: %#v", success)
}
log.Infof("Got success message committing transaction: %#v", success)
pull, ok := pullInt.(messages.SuccessMessage)
if !ok {
return errors.New("Unrecognized response type pulling transaction: %#v", pull)
}
log.Infof("Got success message pulling transaction: %#v", pull)
t.conn.transaction = nil
t.closed = true
return err
}
// Rollback rolls back and closes the transaction
func (t *boltTx) Rollback() error {
if t.closed {
return errors.New("Transaction already closed")
}
if t.conn.statement != nil {
if err := t.conn.statement.Close(); err != nil {
return errors.Wrap(err, "An error occurred closing open rows in transaction Rollback")
}
}
successInt, pullInt, err := t.conn.sendRunPullAllConsumeSingle("ROLLBACK", nil)
if err != nil {
return errors.Wrap(err, "An error occurred rolling back transaction")
}
success, ok := successInt.(messages.SuccessMessage)
if !ok {
return errors.New("Unrecognized response type rolling back transaction: %#v", success)
}
log.Infof("Got success message rolling back transaction: %#v", success)
pull, ok := pullInt.(messages.SuccessMessage)
if !ok {
return errors.New("Unrecognized response type pulling transaction: %#v", pull)
}
log.Infof("Got success message pulling transaction: %#v", pull)
t.conn.transaction = nil
t.closed = true
return err
}