/
tx.go
171 lines (156 loc) · 3.57 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2014 Rana Ian. All rights reserved.
// Use of this source code is governed by The MIT License
// found in the accompanying LICENSE file.
package ora
/*
#include <oci.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"sync"
)
// LogTxCfg represents Tx logging configuration values.
type LogTxCfg struct {
// Commit determines whether the Tx.Commit method is logged.
//
// The default is true.
Commit bool
// Rollback determines whether the Tx.Rollback method is logged.
//
// The default is true.
Rollback bool
}
// NewLogTxCfg creates a LogTxCfg with default values.
func NewLogTxCfg() LogTxCfg {
c := LogTxCfg{}
c.Commit = true
c.Rollback = true
return c
}
// Tx represents an Oracle transaction associated with a session.
//
// Implements the driver.Tx interface.
type Tx struct {
sync.RWMutex
cmu sync.Mutex
id uint64
ses *Ses
}
// checkIsOpen validates that the session is open.
func (tx *Tx) checkIsOpen() error {
if tx == nil {
return er("Tx is closed.")
}
tx.RLock()
ses := tx.ses
tx.RUnlock()
if ses == nil {
return er("Tx is closed.")
}
return ses.checkClosed()
}
// closeWithRemove releases allocated resources and removes the Tx from the
// Ses.openTxss list.
func (tx *Tx) closeWithRemove() (err error) {
tx.RLock()
tx.ses.openTxs.remove(tx)
tx.RUnlock()
return tx.close()
}
// close releases allocated resources.
func (tx *Tx) close() (err error) {
tx.cmu.Lock()
defer tx.cmu.Unlock()
var ok bool
tx.Lock()
if tx.ses != nil {
tx.ses = nil
ok = true
}
tx.Unlock()
if ok {
_drv.txPool.Put(tx)
}
return nil
}
// Commit commits the transaction.
//
// Commit is a member of the driver.Tx interface.
func (tx *Tx) Commit() (err error) {
if tx == nil {
return nil
}
tx.log(_drv.Cfg().Log.Tx.Commit)
if err = tx.checkIsOpen(); err != nil {
return err
}
defer tx.closeWithRemove()
tx.RLock()
r := C.OCITransCommit(
tx.ses.ocisvcctx, //OCISvcCtx *svchp,
tx.ses.srv.env.ocierr, //OCIError *errhp,
C.OCI_DEFAULT) //ub4 flags );
tx.RUnlock()
if r == C.OCI_ERROR {
return tx.ses.srv.env.ociError()
}
return nil
}
// Rollback rolls back a transaction.
//
// Rollback is a member of the driver.Tx interface.
func (tx *Tx) Rollback() (err error) {
if tx == nil {
return nil
}
tx.log(_drv.Cfg().Log.Tx.Rollback)
if err = tx.checkIsOpen(); err != nil {
return err
}
tx.RLock()
ses := tx.ses
tx.RUnlock()
if ses == nil || ses.srv == nil {
return nil
}
defer tx.closeWithRemove()
tx.RLock()
r := C.OCITransRollback(
tx.ses.ocisvcctx, //OCISvcCtx *svchp,
tx.ses.srv.env.ocierr, //OCIError *errhp,
C.OCI_DEFAULT) //ub4 flags );
tx.RUnlock()
if r == C.OCI_ERROR {
return tx.ses.srv.env.ociError()
}
return nil
}
// sysName returns a string representing the Tx.
func (tx *Tx) sysName() string {
if tx == nil {
return "E_S_S_T_"
}
return tx.ses.sysName() + fmt.Sprintf("T%v", tx.id)
}
// log writes a message with an Tx system name and caller info.
func (tx *Tx) log(enabled bool, v ...interface{}) {
if enabled {
if len(v) == 0 {
_drv.Cfg().Log.Logger.Infof("%v %v", tx.sysName(), callInfo(1))
} else {
_drv.Cfg().Log.Logger.Infof("%v %v %v", tx.sysName(), callInfo(1), fmt.Sprint(v...))
}
}
}
// log writes a formatted message with an Tx system name and caller info.
func (tx *Tx) logF(enabled bool, format string, v ...interface{}) {
if enabled {
if len(v) == 0 {
_drv.Cfg().Log.Logger.Infof("%v %v", tx.sysName(), callInfo(1))
} else {
_drv.Cfg().Log.Logger.Infof("%v %v %v", tx.sysName(), callInfo(1), fmt.Sprintf(format, v...))
}
}
}