Skip to content

Commit eb8041e

Browse files
committed
增加Tx返回值--error:当事务中产生error,则回滚,并将error通过Tx返回。调用者根据error或panic判断事务的执行情况
1 parent acacba5 commit eb8041e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

sqlrunner.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (this *Session) SetParserFactory(fac ParserFactory) {
122122
//开启事务执行语句
123123
//返回nil则提交,返回error回滚
124124
//抛出异常错误触发回滚
125-
func (this *Session) Tx(txFunc func(session *Session) error) {
125+
func (this *Session) Tx(txFunc func(session *Session) error) error {
126126
this.session.Begin()
127127
defer func() {
128128
if r := recover(); r != nil {
@@ -131,10 +131,12 @@ func (this *Session) Tx(txFunc func(session *Session) error) {
131131
}
132132
}()
133133

134-
if txFunc(this) != nil {
134+
if err := txFunc(this); err != nil {
135135
this.session.Rollback()
136+
return err
136137
} else {
137138
this.session.Commit()
139+
return nil
138140
}
139141
}
140142

0 commit comments

Comments
 (0)