Skip to content

Commit

Permalink
Fix: empty transaction should be committed successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
btnguyen2k committed Jan 4, 2024
1 parent 0ccd395 commit fe6abcd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func (c *Conn) commit() error {
c.txMode = txNone
c.txStmtList = nil
}()
if len(c.txStmtList) == 0 {
//empty transaction should be successful
return nil
}
c.txMode = txCommitting
txStmts := make([]types.ParameterizedStatement, len(c.txStmtList))
for i, txStmt := range c.txStmtList {
Expand Down
16 changes: 16 additions & 0 deletions module_test/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ import (
"testing"
)

func TestTx_Empty(t *testing.T) {
testName := "TestTx_Empty"
db := _openDb(t, testName)
defer func() { _ = db.Close() }()
_initTest(db)

tx, err := db.Begin()
if err != nil {
t.Fatalf("%s failed: %s", testName+"/Begin", err)
}
err = tx.Commit()
if err != nil {
t.Fatalf("%s failed: %s", testName+"/Commit", err)
}
}

func TestTx_Rollback(t *testing.T) {
testName := "TestTx_Rollback"
db := _openDb(t, testName)
Expand Down

0 comments on commit fe6abcd

Please sign in to comment.