Skip to content

MySqlTransaction.Dispose(Boolean disposing) throws System.InvalidOperationException: Connection must be Open; current state is Closed #383

@jemiller0

Description

@jemiller0

I'm running into a problem where a query times out. Then, Dispose() gets called on the Transaction that is being used. I looked at the MySqlConnector code and it tries to rollback the transaction inside the Dispose() method. But, it appears that the connection is already closed. So, the Command that is trying to rollback the transaction throws an exception. This masks the original exception.

System.InvalidOperationException: Connection must be Open; current state is Closed
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at MySql.Data.MySqlClient.MySqlTransaction.Dispose(Boolean disposing)
   at OleLibrary.OleDapperContext.Dispose() in C:\Users\jemiller\Documents\Visual Studio 2017\Projects\Ole\OleLibrary\OleDapperContext.cs:line 2558

Would it be possible to modify the following code to include a check for whether the connection is open?

		protected override void Dispose(bool disposing)
		{
			try
			{
				if (disposing)
				{
					if (!m_isFinished && m_connection != null && m_connection.CurrentTransaction == this)
					{
						using (var cmd = new MySqlCommand("rollback", m_connection, this))
							cmd.ExecuteNonQuery();
						m_connection.CurrentTransaction = null;
					}
					m_connection = null;
				}
			}
			finally
			{
				base.Dispose(disposing);
			}
		}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions