forked from dataux/dataux
/
conn_tx.go
72 lines (57 loc) · 1.3 KB
/
conn_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
package proxy
import (
"github.com/dataux/dataux/vendored/mixer/client"
"github.com/dataux/dataux/vendored/mixer/mysql"
)
func (c *Conn) isInTransaction() bool {
return c.Status&mysql.SERVER_STATUS_IN_TRANS > 0
}
func (c *Conn) isAutoCommit() bool {
return c.Status&mysql.SERVER_STATUS_AUTOCOMMIT > 0
}
func (c *Conn) handleBegin() error {
c.Status |= mysql.SERVER_STATUS_IN_TRANS
return c.WriteOK(nil)
}
func (c *Conn) handleCommit() (err error) {
if err := c.commit(); err != nil {
return err
} else {
return c.WriteOK(nil)
}
}
func (c *Conn) handleRollback() (err error) {
if err := c.rollback(); err != nil {
return err
} else {
return c.WriteOK(nil)
}
}
func (c *Conn) commit() (err error) {
c.Status &= ^mysql.SERVER_STATUS_IN_TRANS
for _, co := range c.txConns {
if e := co.Commit(); e != nil {
err = e
}
co.Close()
}
c.txConns = map[*Node]*client.SqlConn{}
return
}
func (c *Conn) rollback() (err error) {
c.Status &= ^mysql.SERVER_STATUS_IN_TRANS
for _, co := range c.txConns {
if e := co.Rollback(); e != nil {
err = e
}
co.Close()
}
c.txConns = map[*Node]*client.SqlConn{}
return
}
//if status is in_trans, need
//else if status is not autocommit, need
//else no need
func (c *Conn) needBeginTx() bool {
return c.isInTransaction() || !c.isAutoCommit()
}