Closed Gankov closed 1 year ago
If the call stack is accurate, then it seems like the exception is happening here: https://github.com/mysql-net/MySqlConnector/blob/2.2.6/src/MySqlConnector/MySqlConnection.cs#L1098-L1107
I'm not seeing where the exception could come from unless CurrentTransaction
is not null
but m_session
is.
Is it possible that there is a background thread doing work on this MySqlConnection
at the same time that OnCloseButtonClicked
is clicked in the UI? Sharing connections between threads is not permitted: https://mysqlconnector.net/troubleshooting/connection-reuse/.
It's potentially possible, the app is big enough, but I don't see where it would happen. The application basically doesn't use threads directly in the database related code. Unless in an obvious way.
I see in MySqlConnection.cs#L1098-L1107 and can't understand what throw NullReferenceException too.
I see that in MySqlConnector called async methods. Possibly we call Dispose multi times and parallel thread created in MySqlConnector. I will try fix multi times disposing.
Even if the connection was used in unsupported scenarios, throwing NullReferenceException
is not a good practice. In this case is can be easily fixed:
- if (CurrentTransaction is not null && m_session!.IsConnected)
+ if (CurrentTransaction is not null && m_session?.IsConnected is true)
That would only be an inappropriate change if m_session
is expected to be null
; otherwise it's communicating false semantics about the expected preconditions/invariants.
It first needs to be established that that's a valid state to be in (as opposed to misuse of the library, in which case any invariants might be invalidated and all bets are off).
Software versions MySqlConnector version: 2.2.6 Server type (MySQL, MariaDB, Aurora, etc.) and version: MariaDB (versions different 10.x) .NET version: 4.6.1 (Optional) ORM NuGet packages and versions: Nhibernate 5.3 and 5.4
Describe the bug Sometimes when transaction rollback we have NullReferenceException. How i can check before call rollback that i can get NullReferenceException? This code worked with MySQL.Data. Perhaps for MySQLConnector need check something else? it seems like connection still open.
Exception
Code sample