|
| 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 |
0 commit comments