Repository navigation
Description
This is a little follow up to #1571.
Most connection implementations support enlisting a null transaction, provided there is no current one or that the current one is flagged as completed.
Some connections requires it (SqlConnection in NHibernate tests) in some edge circumstances, for leaving "earlier" an ended transaction, as if their own un-enlistment logic was sometimes a bit delayed. Again this occurs only in our distributed transactions tests.
There Npgsql is a special case for NHibernate, since it throws an ArgumentNullException. (It is not alone, SQLite does the same, SqlCe throws null ref.)
That is not hard to handle for NHibernate (I already have put a setting in our provider interface for this, still in a PR), but maybe this could be an additional issue later on, if such delayed un-enlistment occurs with Npgsql.
Repro: just call EnlistTransaction(null) on a NpgsqlConnection.
Hum, and should I open another issue about following test in its code?
if (EnlistedTransaction != null)
throw new InvalidOperationException($"Already enlisted to transaction (localid={EnlistedTransaction.TransactionInformation.LocalIdentifier})");On Microsoft connection side, they do it so (from ILSpy on SqlConnection):
if (enlistedTransaction != null) { if (enlistedTransaction.Equals(transaction)) { return; } if (enlistedTransaction.TransactionInformation.Status == TransactionStatus.Active) { throw ADP.TransactionPresent(); } }
Meaning they support enlisting with a new transaction even if there is already one, provided it is not yet completed. Here too, I suspect they have done so for avoiding some concurrency issues, when the connection has not already un-enlisted by itself.
Activity
roji commentedon May 24, 2017
Regarding un-enlistment, I'm not sure I understand what the expected behavior actually is... what happens if you've un-enlisted after already sending some statements? How can a distributed transaction be "demoted"??
Regarding reenlistment, the Microsoft code you posted seems to allow reenlisting only if it's on the same transaction (in which case it's a no-op), or if the transaction has already completed. Npgsql also has a check for whether we're reenlisting on the same transaction, although I notice Npgsql does == equality whereas your Microsoft code uses Equals. Do you want to submit a PR fixing these two issues (Equals instead of ==, allowing reenlisting if previous transaction isn't active)?
fredericDelaporte commentedon May 24, 2017
Npgsql should still throw if trying to enlist while another active transaction is already enlisted.
I just hint at maybe the Npgsql test should be relaxed for not throwing when the already enlisted transaction is no more active. Even better if it starts supporting un-enlisting from completed transaction when we call it with
null(and no-op if the connection was not enlisted with anything).Equalsinstead of==does not change anything in the case of system transactions : they override==for callingEquals. But that is an implementation detail on which it is better to not rely: usually==is reference comparison. So it would be still better to useEqualsinstead, even if currently that does the same thing. IMO this==override is here for considering equal a cloned transaction, even for those not aware that reference equality (default behavior of==) would miss that.roji commentedon May 24, 2017
OK. So if I understand you correctly, calling
Enlist(null)while there's an active transaction would throw InvalidOperationException? If so everything you say makes sense to me, and I'd accept a PR doing it.fredericDelaporte commentedon May 25, 2017
That is it. The point is if the transaction is completed, it should un-enlist the transaction from the connection. I currently have not checked what should be done for that.
roji commentedon May 25, 2017
OK, I think we're on the same page. Would you like to take a look at submitting a PR on this for 3.3 (dev branch)?
Better check for active enlisting transaction
Better check for active enlisting transaction