Skip to content

Commit

Permalink
fix tx
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfujian committed Sep 25, 2023
1 parent 59a0722 commit 76dad37
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
13 changes: 3 additions & 10 deletions generator/client/golang/templates/queryx.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,11 @@ func (c *QXClient) Transaction(f func(t *Tx) error) error {
if err != nil {
return err
}
err = f(tx)
if err != nil {
err = tx.Rollback()
if err != nil {
return err
}
}
err = tx.Commit()
if err != nil {
defer tx.Rollback()
if err = f(tx);err != nil {
return err
}
return nil
return tx.Commit()
}

type Tx struct {
Expand Down
6 changes: 3 additions & 3 deletions internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ func TestManualTransaction(t *testing.T) {
require.Equal(t, "tag1-updated", tag1.Name.Val)
}

func TestAutomaticTransaction(t *testing.T) {
transactionErr := errors.New("sql: transaction has already been committed or rolled back")
func TestTransactionBlock(t *testing.T) {
transactionErr := errors.New("test transaction")
err := c.Transaction(func(tran *db.Tx) error {
_, err := tran.QueryPost().Create(tran.ChangePost().SetTitle("post title .."))
if err != nil {
Expand All @@ -441,7 +441,7 @@ func TestAutomaticTransaction(t *testing.T) {
if err != nil {
return err
}
return errors.New("test transaction")
return transactionErr
})
require.Equal(t, err, transactionErr)

Expand Down

0 comments on commit 76dad37

Please sign in to comment.