Skip to content

Commit 70d7b98

Browse files
committed
Fix session leak with user variables. Fixes #305
Moving 'CreateQueryPayload' earlier in the function keeps the internal state as 'Connected' (rather than 'Querying') so the session is correctly returned to the pool after the exception is handled. The 'Querying' state is now only used when an outgoing connection to the MySQL server is initiated.
1 parent 5b762cd commit 70d7b98

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/MySqlConnector/MySqlClient/CommandExecutors/TextCommandExecutor.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,11 @@ public virtual async Task<DbDataReader> ExecuteReaderAsync(string commandText, M
5151
CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken)
5252
{
5353
cancellationToken.ThrowIfCancellationRequested();
54+
var payload = CreateQueryPayload(commandText, parameterCollection);
5455
using (m_command.RegisterCancel(cancellationToken))
5556
{
5657
m_command.Connection.Session.StartQuerying(m_command);
5758
m_command.LastInsertedId = -1;
58-
var statementPreparerOptions = StatementPreparerOptions.None;
59-
if (m_command.Connection.AllowUserVariables || m_command.CommandType == CommandType.StoredProcedure)
60-
statementPreparerOptions |= StatementPreparerOptions.AllowUserVariables;
61-
if (m_command.Connection.OldGuids)
62-
statementPreparerOptions |= StatementPreparerOptions.OldGuids;
63-
if (m_command.CommandType == CommandType.StoredProcedure)
64-
statementPreparerOptions |= StatementPreparerOptions.AllowOutputParameters;
65-
var preparer = new MySqlStatementPreparer(commandText, parameterCollection, statementPreparerOptions);
66-
var payload = new PayloadData(preparer.ParseAndBindParameters());
6759
try
6860
{
6961
await m_command.Connection.Session.SendAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);
@@ -83,6 +75,19 @@ public virtual async Task<DbDataReader> ExecuteReaderAsync(string commandText, M
8375
}
8476
}
8577

78+
private PayloadData CreateQueryPayload(string commandText, MySqlParameterCollection parameterCollection)
79+
{
80+
var statementPreparerOptions = StatementPreparerOptions.None;
81+
if (m_command.Connection.AllowUserVariables || m_command.CommandType == CommandType.StoredProcedure)
82+
statementPreparerOptions |= StatementPreparerOptions.AllowUserVariables;
83+
if (m_command.Connection.OldGuids)
84+
statementPreparerOptions |= StatementPreparerOptions.OldGuids;
85+
if (m_command.CommandType == CommandType.StoredProcedure)
86+
statementPreparerOptions |= StatementPreparerOptions.AllowOutputParameters;
87+
var preparer = new MySqlStatementPreparer(commandText, parameterCollection, statementPreparerOptions);
88+
return new PayloadData(preparer.ParseAndBindParameters());
89+
}
90+
8691
readonly MySqlCommand m_command;
8792
}
8893
}

0 commit comments

Comments
 (0)