diff --git a/session/session.go b/session/session.go index 637c971b03609..25a3f05441352 100644 --- a/session/session.go +++ b/session/session.go @@ -3160,13 +3160,16 @@ func (s *session) PrepareTxnCtx(ctx context.Context) error { } txnMode := ast.Optimistic - if !s.sessionVars.IsAutocommit() || s.sessionVars.RetryInfo.Retrying || - config.GetGlobalConfig().PessimisticTxn.PessimisticAutoCommit.Load() { + if !s.sessionVars.IsAutocommit() || config.GetGlobalConfig().PessimisticTxn.PessimisticAutoCommit.Load() { if s.sessionVars.TxnMode == ast.Pessimistic { txnMode = ast.Pessimistic } } + if s.sessionVars.RetryInfo.Retrying { + txnMode = ast.Pessimistic + } + return sessiontxn.GetTxnManager(s).EnterNewTxn(ctx, &sessiontxn.EnterNewTxnRequest{ Type: sessiontxn.EnterNewTxnBeforeStmt, TxnMode: txnMode,