Skip to content

Commit 979d79f

Browse files
committed
Tx func return error instead bool
1 parent 782e164 commit 979d79f

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

sqlrunner.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ func (this *Session) GetContext() context.Context {
104104
}
105105

106106
//开启事务执行语句
107-
//返回true则提交,返回false回滚
107+
//返回nil则提交,返回error回滚
108108
//抛出异常错误触发回滚
109-
func (this *Session) Tx(txFunc func(session *Session) bool) {
109+
func (this *Session) Tx(txFunc func(session *Session) error) {
110110
this.session.Begin()
111111
defer func() {
112112
if r := recover(); r != nil {
@@ -115,7 +115,7 @@ func (this *Session) Tx(txFunc func(session *Session) bool) {
115115
}
116116
}()
117117

118-
if txFunc(this) != true {
118+
if txFunc(this) != nil {
119119
this.session.Rollback()
120120
} else {
121121
this.session.Commit()

test/cmd/test_table_proxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ func New(proxyMrg *gobatis.SessionManager) *TestTableCallProxy {
1818
return (*TestTableCallProxy)(proxyMrg.NewSession())
1919
}
2020

21-
func (proxy *TestTableCallProxy) Tx(txFunc func(s *TestTableCallProxy) bool) {
21+
func (proxy *TestTableCallProxy) Tx(txFunc func(s *TestTableCallProxy) error) {
2222
sess := (*gobatis.Session)(proxy)
23-
sess.Tx(func(session *gobatis.Session) bool {
23+
sess.Tx(func(session *gobatis.Session) error {
2424
return txFunc(proxy)
2525
})
2626
}

test/runner_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,19 @@ func TestTx1(t *testing.T) {
230230

231231
var testList []TestTable
232232

233-
mgr.NewSession().Tx(func(session *gobatis.Session) bool {
233+
mgr.NewSession().Tx(func(session *gobatis.Session) error {
234234
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+
}
236239
t.Logf("ret %d\n", ret)
237240
session.Select("select_id").Param().Result(&testList)
238241
for _, v := range testList {
239242
t.Logf("data: %v", v)
240243
}
241244
//commit
242-
return true
245+
return nil
243246
})
244247
}
245248

@@ -269,9 +272,13 @@ func TestTx2(t *testing.T) {
269272

270273
var testList []TestTable
271274

272-
mgr.NewSession().Tx(func(session *gobatis.Session) bool {
275+
mgr.NewSession().Tx(func(session *gobatis.Session) error {
273276
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+
}
275282
t.Logf("ret %d\n", ret)
276283
session.Select("select_id").Param().Result(&testList)
277284
for _, v := range testList {
@@ -281,6 +288,6 @@ func TestTx2(t *testing.T) {
281288
panic("ROLLBACK panic!!")
282289

283290
//rollback
284-
return false
291+
return nil
285292
})
286293
}

0 commit comments

Comments
 (0)