File tree Expand file tree Collapse file tree 3 files changed +18
-11
lines changed Expand file tree Collapse file tree 3 files changed +18
-11
lines changed Original file line number Diff line number Diff line change @@ -104,9 +104,9 @@ func (this *Session) GetContext() context.Context {
104
104
}
105
105
106
106
//开启事务执行语句
107
- //返回true则提交,返回false回滚
107
+ //返回nil则提交,返回error回滚
108
108
//抛出异常错误触发回滚
109
- func (this * Session ) Tx (txFunc func (session * Session ) bool ) {
109
+ func (this * Session ) Tx (txFunc func (session * Session ) error ) {
110
110
this .session .Begin ()
111
111
defer func () {
112
112
if r := recover (); r != nil {
@@ -115,7 +115,7 @@ func (this *Session) Tx(txFunc func(session *Session) bool) {
115
115
}
116
116
}()
117
117
118
- if txFunc (this ) != true {
118
+ if txFunc (this ) != nil {
119
119
this .session .Rollback ()
120
120
} else {
121
121
this .session .Commit ()
Original file line number Diff line number Diff line change @@ -18,9 +18,9 @@ func New(proxyMrg *gobatis.SessionManager) *TestTableCallProxy {
18
18
return (* TestTableCallProxy )(proxyMrg .NewSession ())
19
19
}
20
20
21
- func (proxy * TestTableCallProxy ) Tx (txFunc func (s * TestTableCallProxy ) bool ) {
21
+ func (proxy * TestTableCallProxy ) Tx (txFunc func (s * TestTableCallProxy ) error ) {
22
22
sess := (* gobatis .Session )(proxy )
23
- sess .Tx (func (session * gobatis.Session ) bool {
23
+ sess .Tx (func (session * gobatis.Session ) error {
24
24
return txFunc (proxy )
25
25
})
26
26
}
Original file line number Diff line number Diff line change @@ -230,16 +230,19 @@ func TestTx1(t *testing.T) {
230
230
231
231
var testList []TestTable
232
232
233
- mgr .NewSession ().Tx (func (session * gobatis.Session ) bool {
233
+ mgr .NewSession ().Tx (func (session * gobatis.Session ) error {
234
234
ret := 0
235
- session .Insert ("insert_id" ).Param (testV ).Result (& ret )
235
+ err := session .Insert ("insert_id" ).Param (testV ).Result (& ret )
236
+ if err != nil {
237
+ return err
238
+ }
236
239
t .Logf ("ret %d\n " , ret )
237
240
session .Select ("select_id" ).Param ().Result (& testList )
238
241
for _ , v := range testList {
239
242
t .Logf ("data: %v" , v )
240
243
}
241
244
//commit
242
- return true
245
+ return nil
243
246
})
244
247
}
245
248
@@ -269,9 +272,13 @@ func TestTx2(t *testing.T) {
269
272
270
273
var testList []TestTable
271
274
272
- mgr .NewSession ().Tx (func (session * gobatis.Session ) bool {
275
+ mgr .NewSession ().Tx (func (session * gobatis.Session ) error {
273
276
ret := 0
274
- session .Insert ("insert_id" ).Param (testV ).Result (& ret )
277
+ err := session .Insert ("insert_id" ).Param (testV ).Result (& ret )
278
+ if err != nil {
279
+ t .Logf ("error %v\n " , err )
280
+ return err
281
+ }
275
282
t .Logf ("ret %d\n " , ret )
276
283
session .Select ("select_id" ).Param ().Result (& testList )
277
284
for _ , v := range testList {
@@ -281,6 +288,6 @@ func TestTx2(t *testing.T) {
281
288
panic ("ROLLBACK panic!!" )
282
289
283
290
//rollback
284
- return false
291
+ return nil
285
292
})
286
293
}
You can’t perform that action at this time.
0 commit comments