Skip to content

Commit 0aac9b5

Browse files
committed
Throw InvalidOperationException for open reader.
This is a breaking change from Connector/NET but is more consistent with other ADO.NET Providers. Since it indicates misuse of the library, the specific exception type should not be relied on my clients.
1 parent 835ec7b commit 0aac9b5

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

docs/content/tutorials/migrating-from-connector-net.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ Connector/NET allows a command to be executed even when `MySqlCommand.Transactio
7676
disposed `MySqlTransaction`. MySqlConnector will throw an `InvalidOperationException` if the `MySqlCommand.Transaction`
7777
property doesn't reference the active transaction. See [#333](https://github.com/mysql-net/MySqlConnector/issues/333) for more details.
7878

79+
### Exceptions
80+
81+
For consistency with other ADO.NET providers, MySqlConnector will throw `InvalidOperationException` (instead of `MySqlException`)
82+
for various precondition checks that indicate misuse of the API (and not a problem related to MySQL Server).
83+
7984
### Bugs present in Connector/NET that are fixed in MySqlConnector
8085

8186
* [#37283](https://bugs.mysql.com/bug.php?id=37283), [#70587](https://bugs.mysql.com/bug.php?id=70587): Distributed transactions are not supported

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void StartQuerying(MySqlCommand command)
115115
lock (m_lock)
116116
{
117117
if (m_state == State.Querying || m_state == State.CancelingQuery)
118-
throw new MySqlException("There is already an open DataReader associated with this Connection which must be closed first.");
118+
throw new InvalidOperationException("There is already an open DataReader associated with this Connection which must be closed first.");
119119

120120
VerifyState(State.Connected);
121121
m_state = State.Querying;

tests/SideBySide/QueryTests.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ public async Task InvalidSql()
198198
[Fact]
199199
public async Task MultipleReaders()
200200
{
201+
#if BASELINE
202+
var exceptionType = typeof(MySqlException);
203+
#else
204+
var exceptionType = typeof(InvalidOperationException);
205+
#endif
201206
using (var cmd = m_database.Connection.CreateCommand())
202207
{
203208
cmd.CommandText = @"drop table if exists query_multiple_readers;
@@ -214,21 +219,21 @@ public async Task MultipleReaders()
214219

215220
using (var reader1 = await cmd1.ExecuteReaderAsync())
216221
{
217-
Assert.Throws<MySqlException>(() => cmd2.ExecuteReader());
218-
Assert.Throws<MySqlException>(() => cmd2.ExecuteScalar());
222+
Assert.Throws(exceptionType, () => cmd2.ExecuteReader());
223+
Assert.Throws(exceptionType, () => cmd2.ExecuteScalar());
219224
do
220225
{
221226
while (await reader1.ReadAsync())
222227
{
223-
Assert.Throws<MySqlException>(() => cmd2.ExecuteReader());
224-
Assert.Throws<MySqlException>(() => cmd2.ExecuteScalar());
228+
Assert.Throws(exceptionType, () => cmd2.ExecuteReader());
229+
Assert.Throws(exceptionType, () => cmd2.ExecuteScalar());
225230
}
226-
Assert.Throws<MySqlException>(() => cmd2.ExecuteReader());
227-
Assert.Throws<MySqlException>(() => cmd2.ExecuteScalar());
231+
Assert.Throws(exceptionType, () => cmd2.ExecuteReader());
232+
Assert.Throws(exceptionType, () => cmd2.ExecuteScalar());
228233
} while (await reader1.NextResultAsync());
229234

230-
Assert.Throws<MySqlException>(() => cmd2.ExecuteReader());
231-
Assert.Throws<MySqlException>(() => cmd2.ExecuteScalar());
235+
Assert.Throws(exceptionType, () => cmd2.ExecuteReader());
236+
Assert.Throws(exceptionType, () => cmd2.ExecuteScalar());
232237

233238
reader1.Dispose();
234239
using (cmd2.ExecuteReader())

0 commit comments

Comments
 (0)