diff --git a/conn.go b/conn.go index 2b1f0ca..e27f4f1 100644 --- a/conn.go +++ b/conn.go @@ -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 { diff --git a/module_test/tx_test.go b/module_test/tx_test.go index 989c7ed..3f2f49b 100644 --- a/module_test/tx_test.go +++ b/module_test/tx_test.go @@ -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)