diff --git a/session_tx.go b/session_tx.go index 84d2f7f9d..c8d759a31 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 568ed0f12..ab66713ef 100644 --- a/session_tx_test.go +++ b/session_tx_test.go @@ -128,3 +128,66 @@ func TestCombineTransactionSameMapper(t *testing.T) { err = session.Commit() assert.NoError(t, err) } + +func TestMultipleTransaction(t *testing.T) { + assert.NoError(t, prepareEngine()) + + type MultipleTransaction struct { + Id int64 + Name string + } + + assertSync(t, new(MultipleTransaction)) + + session := testEngine.NewSession() + defer session.Close() + + err := session.Begin() + assert.NoError(t, err) + + m1 := MultipleTransaction{Name: "xiaoxiao2"} + _, err = session.Insert(&m1) + assert.NoError(t, err) + + user2 := MultipleTransaction{Name: "zzz"} + _, err = session.Where("id = ?", 0).Update(&user2) + assert.NoError(t, err) + + err = session.Commit() + assert.NoError(t, err) + + var ms []MultipleTransaction + err = session.Find(&ms) + assert.NoError(t, err) + assert.EqualValues(t, 1, len(ms)) + + err = session.Begin() + assert.NoError(t, err) + + _, err = session.Where("id=?", m1.Id).Delete(new(MultipleTransaction)) + assert.NoError(t, err) + + err = session.Commit() + assert.NoError(t, err) + + ms = make([]MultipleTransaction, 0) + err = session.Find(&ms) + assert.NoError(t, err) + assert.EqualValues(t, 0, len(ms)) + + err = session.Begin() + assert.NoError(t, err) + + _, err = session.Insert(&MultipleTransaction{ + Name: "ssss", + }) + assert.NoError(t, err) + + err = session.Rollback() + assert.NoError(t, err) + + ms = make([]MultipleTransaction, 0) + err = session.Find(&ms) + assert.NoError(t, err) + assert.EqualValues(t, 0, len(ms)) +}