Skip to content

Commit 5880e9e

Browse files
committed
Move troubleshooting instructions to documentation site.
1 parent 257047a commit 5880e9e

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

docs/config.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ pluralizeListTitles = false
2929
identifier = "api"
3030
pre = "<i class='fa fa-file-text'></i>"
3131
weight = 50
32+
[[menu.main]]
33+
name = "Troubleshooting"
34+
identifier = "troubleshooting"
35+
pre = "<i class='fa fa-lightbulb-o'></i>"
36+
weight = 60
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
lastmod: 2018-09-29
3+
date: 2018-09-29
4+
title: Transaction Usage
5+
weight: 20
6+
menu:
7+
main:
8+
parent: troubleshooting
9+
---
10+
11+
# Transaction Usage
12+
13+
By default, MySqlConnector requires `MySqlCommand.Transaction` to be set to the connection's active transaction in order for the command to be executed successfully. This strictness is intended to catch programming bugs related to using the wrong transaction, a disposed transaction, or forgetting to set the transaction (and using the default value `null`).
14+
15+
However, this strictness can make migrating from Connector/NET more difficult, as it may require significant code changes to pass the current transaction through to all command objects. It can also be challenging when using a library like Dapper that creates the `MySqlCommand` objects itself.
16+
17+
## Workaround: Use IgnoreCommandTransaction=true
18+
19+
To easily migrate code from Connector/NET, use the `IgnoreCommandTransaction=true` connection string setting to emulate Connector/NET's behaviour and not validate the value of `MySqlCommand.Transaction`. By doing this, you will not need the code fixes prescribed below.
20+
21+
## Code Fix: Set MySqlCommand.Transaction
22+
23+
### ADO.NET example
24+
25+
```csharp
26+
using (var connection = new MySqlConnection(...))
27+
{
28+
connection.Open();
29+
using (var transaction = connection.BeginTransaction())
30+
using (var command = connection.CreateCommand())
31+
{
32+
command.CommandText = "SELECT ...";
33+
34+
// *** ADD THIS LINE ***
35+
command.Transaction = transaction;
36+
37+
// otherwise, this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction.
38+
command.ExecuteScalar();
39+
}
40+
}
41+
```
42+
43+
### Dapper Example
44+
45+
```csharp
46+
using (var connection = new MySqlConnection(...))
47+
{
48+
connection.Open();
49+
using (var transaction = connection.BeginTransaction())
50+
{
51+
// this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction.
52+
connection.Query("SELECT ...");
53+
54+
// use this instead:
55+
connection.Query("SELECT ...", transaction: transaction);
56+
}
57+
}
58+
```
59+
60+
## Further Reading
61+
62+
* [MySQL bug 88611](https://bugs.mysql.com/bug.php?id=88611) reporting Connector/NET's behaviour as a bug
63+
* [Issue #333](https://github.com/mysql-net/MySqlConnector/issues/333) for the addition of MySqlConnector's strict behaviour
64+
* Issues [#405](https://github.com/mysql-net/MySqlConnector/issues/405), [#452](https://github.com/mysql-net/MySqlConnector/issues/452), [#457](https://github.com/mysql-net/MySqlConnector/issues/457) for users encountering this as a breaking change

src/MySqlConnector/MySql.Data.MySqlClient/MySqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ private bool IsValid(out Exception exception)
419419
else if (Connection.State != ConnectionState.Open && Connection.State != ConnectionState.Connecting)
420420
exception = new InvalidOperationException("Connection must be Open; current state is {0}".FormatInvariant(Connection.State));
421421
else if (!Connection.IgnoreCommandTransaction && Transaction != Connection.CurrentTransaction)
422-
exception = new InvalidOperationException("The transaction associated with this command is not the connection's active transaction; see https://github.com/mysql-net/MySqlConnector/issues/474");
422+
exception = new InvalidOperationException("The transaction associated with this command is not the connection's active transaction; see https://fl.vu/mysql-trans");
423423
else if (string.IsNullOrWhiteSpace(CommandText))
424424
exception = new InvalidOperationException("CommandText must be specified");
425425
return exception == null;

0 commit comments

Comments
 (0)