-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.go
More file actions
139 lines (126 loc) · 3.06 KB
/
tx.go
File metadata and controls
139 lines (126 loc) · 3.06 KB
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
// 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 (
"container/list"
"fmt"
)
// 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 {
id uint64
ses *Ses
elem *list.Element
}
// checkIsOpen validates that the session is open.
func (tx *Tx) checkIsOpen() error {
if tx == nil || tx.ses == nil {
return er("Tx is closed.")
}
return tx.ses.checkClosed()
}
func (tx *Tx) close() {
if tx.ses != nil {
tx.ses.openTxs.Remove(tx.elem)
tx.ses = nil
tx.elem = nil
_drv.txPool.Put(tx)
}
}
// 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.close()
r := C.OCITransCommit(
tx.ses.srv.ocisvcctx, //OCISvcCtx *svchp,
tx.ses.srv.env.ocierr, //OCIError *errhp,
C.OCI_DEFAULT) //ub4 flags );
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
}
if tx.ses == nil || tx.ses.srv == nil {
return nil
}
defer tx.close()
r := C.OCITransRollback(
tx.ses.srv.ocisvcctx, //OCISvcCtx *svchp,
tx.ses.srv.env.ocierr, //OCIError *errhp,
C.OCI_DEFAULT) //ub4 flags );
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...))
}
}
}