From b74567be104e4d2cd4b594bb00076d28b1c6414d Mon Sep 17 00:00:00 2001 From: Florent Biville Date: Wed, 17 May 2023 15:32:41 +0200 Subject: [PATCH] Fix rollback --- neo4j/transaction_with_context.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/neo4j/transaction_with_context.go b/neo4j/transaction_with_context.go index beb424b0..49371944 100644 --- a/neo4j/transaction_with_context.go +++ b/neo4j/transaction_with_context.go @@ -80,8 +80,12 @@ func (tx *explicitTransaction) Run(ctx context.Context, cypher string, params ma } func (tx *explicitTransaction) Commit(ctx context.Context) error { - if err := tx.checkCompleted(); err != nil { - return err + if tx.runFailed { + tx.runFailed = false + return tx.err + } + if tx.conn == nil { + return transactionAlreadyCompletedError() } tx.err = tx.conn.TxCommit(ctx, tx.txHandle) tx.onClosed(tx) @@ -97,8 +101,12 @@ func (tx *explicitTransaction) Close(ctx context.Context) error { } func (tx *explicitTransaction) Rollback(ctx context.Context) error { - if err := tx.checkCompleted(); err != nil { - return err + if tx.runFailed { + tx.runFailed = false + return nil + } + if tx.conn == nil { + return transactionAlreadyCompletedError() } if !tx.conn.IsAlive() || tx.conn.HasFailed() { // tx implicitly rolled back by having failed @@ -110,17 +118,6 @@ func (tx *explicitTransaction) Rollback(ctx context.Context) error { return errorutil.WrapError(tx.err) } -func (tx *explicitTransaction) checkCompleted() error { - if tx.runFailed { - tx.runFailed = false - return tx.err - } - if tx.conn == nil { - return transactionAlreadyCompletedError() - } - return nil -} - func (tx *explicitTransaction) legacy() Transaction { return &transaction{ delegate: tx,