Skip to content

Commit

Permalink
Throw exception for misuse. Fixes #308
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Oct 22, 2017
1 parent 9d74878 commit d528baa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/MySqlConnector/MySqlClient/MySqlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,19 @@ public override void Prepare()
}

public override string CommandText { get; set; }
public new MySqlConnection Connection { get; set; }
public new MySqlTransaction Transaction { get; set; }

public new MySqlConnection Connection
{
get => m_connection;
set
{
if (m_connection?.HasActiveReader ?? false)
throw new MySqlException("Cannot set MySqlCommand.Connection when there is an open DataReader for this command; it must be closed first.");
m_connection = value;
}
}

public override int CommandTimeout
{
get => Math.Min(m_commandTimeout ?? Connection?.DefaultCommandTimeout ?? 0, int.MaxValue / 1000);
Expand Down Expand Up @@ -244,6 +254,7 @@ private bool IsValid(out Exception exception)

static int s_commandId = 1;

MySqlConnection m_connection;
MySqlParameterCollection m_parameterCollection;
int? m_commandTimeout;
CommandType m_commandType;
Expand Down
2 changes: 2 additions & 0 deletions src/MySqlConnector/MySqlClient/MySqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ internal async Task<CachedProcedure> GetCachedProcedure(IOBehavior ioBehavior, s

internal MySqlSslMode SslMode => m_connectionSettings.SslMode;

internal bool HasActiveReader => m_activeReader != null;

internal void SetActiveReader(MySqlDataReader dataReader)
{
if (dataReader == null)
Expand Down

0 comments on commit d528baa

Please sign in to comment.