From c412be236502ca71315dd61dbd324f3706010f0c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 23 Jul 2017 09:15:44 +0800 Subject: [PATCH] support reuse session to transaction (#650) --- session_tx.go | 2 ++ session_tx_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/session_tx.go b/session_tx.go index 302bc104..c8f0777c 100644 --- a/session_tx.go +++ b/session_tx.go @@ -24,6 +24,7 @@ func (session *Session) Rollback() error { if !session.IsAutoCommit && !session.IsCommitedOrRollbacked { session.saveLastSQL(session.Engine.dialect.RollBackStr()) session.IsCommitedOrRollbacked = true + session.IsAutoCommit = true return session.Tx.Rollback() } return nil @@ -34,6 +35,7 @@ func (session *Session) Commit() error { if !session.IsAutoCommit && !session.IsCommitedOrRollbacked { session.saveLastSQL("COMMIT") session.IsCommitedOrRollbacked = true + session.IsAutoCommit = true var err error if err = session.Tx.Commit(); err == nil { // handle processors after tx committed diff --git a/session_tx_test.go b/session_tx_test.go index 3e71bb40..adb11c66 100644 --- a/session_tx_test.go +++ b/session_tx_test.go @@ -190,3 +190,34 @@ func TestCombineTransactionSameMapper(t *testing.T) { panic(err) } } + +func TestReuseTransaction(t *testing.T) { + assert.NoError(t, prepareEngine()) + sess := testEngine.NewSession() + defer sess.Close() + + type ReuseTx struct { + Id int64 + Name string + } + + assertSync(t, new(ReuseTx)) + + records := []ReuseTx{ + { + Name: "1", + }, + { + Name: "3", + }, + { + Name: "2", + }, + } + for _, r := range records { + assert.NoError(t, sess.Begin()) + _, err := sess.Insert(&r) + assert.NoError(t, err) + assert.NoError(t, sess.Commit()) + } +}