Skip to content

Support enlistment with null #1580

Closed
Closed

Description

@fredericDelaporte

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

  1. roji commented on May 24, 2017

    @roji
    Member

    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)?

  2. fredericDelaporte commented on May 24, 2017

    @fredericDelaporte
    ContributorAuthor

    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).

    Equals instead of == does not change anything in the case of system transactions : they override == for calling Equals. 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 use Equals instead, 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.

  3. roji commented on May 24, 2017

    @roji
    Member

    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.

  4. fredericDelaporte commented on May 25, 2017

    @fredericDelaporte
    ContributorAuthor

    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.

  5. roji commented on May 25, 2017

    @roji
    Member

    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)?

  6. added this to the milestone on May 25, 2017
  7. reopened this on Jun 19, 2017
  8. added a commit that references this issue on Jun 19, 2017
    11e5e8f
  9. added a commit that references this issue on Jun 19, 2017
    c0d627b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions