Repository navigation
Rewrite of System.Transactions support - #1380
Conversation
* General rewrite/refactor, everything is much simpler now. * Although support for enlisting as durable/PSPE is present, it is currently commented out since it makes no sense without recovery (npgsql#1378). * Durable/PSPE support has been rewritten to use .NET 4.5.2's PromoteAndEnlistDurable to promote, this removes the need for the separate AppDomain which was complicating things. * Considerably simplified the scenario where a connection is closed before its transaction completes. Before we put the connection in a "fake closed" mode, we now properly close the connection and return the connector to a special list on the pool. The list is checked when opening a new connection with the same transaction. Based on work and discussions with @omatrot, @amirtuval, @skikiker Closes npgsql#122
|
@roji
I strongly suggest that the transaction name used in PREPARE and COMMIT/ROLLBACK PREPARED is composed of a GUID and the connector backend process id. This allows multiple connections to be enlisted a the same distributed transaction. This is one of my use case. The code is indeed much more simple than before. I will try your code tomorrow at work on my applications to see what its like. |
Many thanks for spending time on this and sending your comments!
Can you provide more information on this scenario? As I understand it, it's possible that the system will decide to do single phase commit instead of two-phase commit, but in that case SinglePhaseCommit is supposed to be called instead of Commit... This is also why the resource manager implements ISinglePhaseNotification rather than IEnlistmentNotification. Any info on when your scenario occurs, maybe even a test would help understand...
But when multiple connections enlist in the same distributed transaction, each (currently) generates their own GUID - the transaction name is connection-specific and not shared across connections... Do you mean that you use the distributed transaction GUID (i.e. |
From a random GUID to the distributed transaction's GUID + backend process id.
|
FYI pushed a commit to change the transaction name. |
|
About this weird commit without bein prepared, I'm still in the dark right now. The transaction involves other resource managers. It is distributed because I have a valid distributed Guid. This is a single phase commit in the 2 phase code. Strange. The single phase code is never called. Other resources managers are volatile... May be this is why. I need to further investigate,and see the behavior with your code. Yes, I'm using the Distributed Transaction Guid and the connector pid as the PostgreSQL transaction name. This helps track back an orphaned prepared connection in the application logs where this Guid is also present. It already allowed me to pinpoint an issue on a specific client in my n tier architecture. My customer was quickly reassured. |
OK... let me know what you find out about that. I know that in some cases the systems opts for a single-phase commit, but then the appropriate method should be called. The documentation for Who knows, maybe this is some bug in the current (pre-rewrite) implementation that caused the wrong method to be called? With all the extra codepaths it could be that...
That makes a lot of sense. I made that change and pushed it to this PR. |
Mono is missing this method, which was added to Transaction in .NET 4.5.2. Have submitted a PR to add it. In any case we're not calling it until recovery is implemented.
|
@roji I've recompile the provider from your sys-tx-rewrite branch, changed the version number and dropped it in place of my current 3.1.9 customized version in one of my application. The behavior right now is that there seems to be no enlistment at all. This is difficult for me to say until I have have more logs available. |
|
FYI I just pushed a cleanup that leaves only volatile enlistment for now. |
PSPE/Durable will be implemented in the future with recovery.
|
This is much better ! Following is the Npgsql log produced within my application. 2016-12-12 11:07:35,207 [28] [(null)] Npgsql.NpgsqlConnection DEBUG Opening connnection I'll let this running as is for the day and report any problem. |
|
Whoopsy doopsy there is a connection leak... 2016-12-12 11:20:17,194 [132] [(null)] WebAPI.Utils.Log4NetExceptionLogger ERROR Npgsql.NpgsqlException (0x80004005): The connection pool has been exhausted, either raise MaxPoolSize (currently 10) or Timeout (currently 15 seconds) |
Uhh, can you share a code sample? |
|
I would say that the connector is not returned to the available pool when the 2PC ends. I'll try to prepare a sample to reproduce the problem,but not until the end of my work day now. In the meantime, I think you could raise the log level with the connector pool. |
|
@omatrot I found the bug and fixed it, and added a test for a leak in that particular situation. Thanks for letting me know. Would be great if you could test again with the latest version and let me know. |
|
No more leak with the latest version. |
|
OK great! I think I'll merge this PR soon to be able to work on other things - we can always add more fixes later. Anything else you'd like to test/play around with/suggest before I do? |
|
I'll have to modify my EF code to try not to wait for the transaction to complete and dispose the context as soon as I have voted for transaction completion. But as you say, you probably can go with the merge. |
I don't understand that one... Does the new implementation trigger some sort of behavior change that impacts you? Can you provide more details?
That doesn't seem like something directly related to System.Transactions etc., but more to your EF (or high level) usage, right? Let me know if there's anything that concerns Npgsql directly.
Are you referring to
OK, I'll do that now. Feel free to continue commenting here or opening new issues. |
I am referring to this comment on issue #122 and this other comment on #1208.
Again, as I understand the new implementation, there is a built-in mechanism in Npgsql that automatically reuse of a connection already enlisted in a distributed transaction if the tx distributed id is the same. I could test how it behaves by removing the similar code I have in the consumer. Unless I'm totaly wrong on that one.
Yes exactly. |
I think I understand. Yes, it should now be totally safe to close a connection even though its transaction hasn't been committed/rolled back yet. Npgsql will even reuse that connection if you happen to open a connection within the same transaction - so as long as you don't open two connections at the same time you'll be reusing the same connector behind the scenes, and won't escalate to a distributed transaction. Of course I'd be very happy to get some real-world confirmation that the new implementation works well :)
Have just pushed this commit, which will log a message on all internal commands, which includes commit/rollbacks. You have to have level trace for it though. |
I am now detached from the TransactionCompletedEvent with the new driver and I can confirm that this is working well. I have a few scenarios where multipe connections are enlisted in the same distributed transaction though. So I think this will be unsupported right now. |
OK, good to hear and thanks for testing. But I didn't understand that last comment - why should multiple connections enlisting in the same distributed transaction not be supported? |
|
This was about the following statement:
I have such a use case where 2 connections to the same database are opened during the same distributed transaction, hence the transaction name using the Tx Dist. Id and the connector Id. |
OK, although there's nothing unsupported about it... As long as you don't have the two connections open simultaneously, Npgsql will reuse the same connector behind the scenes. If you do have them open at the same time, at the moment it won't escalate either, since we're using volatile enlistments (I think escalation only happens for two durable enlistments). |
Based on work and discussions with @omatrot, @amirtuval, @skikiker
Closes #122