You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Transaction exception thrown while processing multiple connection objects (enlist=true). #1208
While processing multiple connection objects within a distributed transaction the following exception is thrown:
“Unhandled Exception: Npgsql.NpgsqlException: ERROR: prepared transaction with identifier ""3fd8cffd-7114-4a1b-a7d3-19e93c0b27d9"" does not exist”.
Upon investigation, I've found that initially this exception is being thrown (and subsequently handled within each connections app domain due to this being a distributed transaction)
"A first chance exception of type 'Npgsql.NpgsqlException' occurred in Npgsql.dll
Additional information: ERROR: DISCARD ALL cannot run inside a transaction block"
A DISCARD ALL is being executed on the database whilst still in the middle of a transaction resulting in the db server returning the above error. The postgres documentation states: DISCARD ALL cannot be executed inside a transaction block.
Following the stack trace for this I can see that a ReleaseWithDiscard() method is being called from NpgsqlConnector.ReleaseResources(), it looks like whilst inside a transaction some objects are attempting to clean up and are inadvertently executing a DISCARD ALL.
This results in the NPGSQL receiving the server error and as part of handling the server error executes a rollback at the db, in the meantime the remainder of the connection objects are processed and subsequently a Transaction.Complete() is called, however the transaction no longer exists at the db resulting in the transaction does not exist error.
There should be an additional check that no ambient transactions exist before a ReleaseWithDiscard() is called.
I think that the NpgsqlConnector.ReleaseResources() method should be changed as follows to stop a DISCARD ALL being executed whilst inside a transaction:
Thanks for reporting this. I know that there are several issues with distributed transactions in Npgsql, and rewriting that is one of the major goals of the next version of Npgsql (3.2). Specifically, the app domains will probably be removed.
It will take me some time before I can attack distributed transactions though.
Thanks for looking into this, it would be great if you could have a quick look at the change I suggested above and confirm that it will not have any functional impact other then to stop a DISCARD ALL being attempted during a transaction.
@warcha unfortunately I don't think things are that simple. DISCARD ALL resets the connection state, and it's important to run it when returning connections to the pool, otherwise things start to leak across connections: imagine you set some PostgreSQL parameter and then close the connection, and another connection randomly sees your change - this could cause chaos.
The problem is more fundamental - I need to investigate exactly what needs to be done. For regular transactions (non-distributed), the transaction is rolled back before closing, which is the right behavior. The issue of closing a connection that has a distributed transaction is very different, and probably requires some special handling at the pool level. There's currently a dirty hack in Npgsql whereby if you close a connection with a distributed connection, it goes into a "fake closed" state, which allows it to be reopened and reused. This is exactly the kind of stuff I want to clean up for 3.2, but I haven't yet had time to research what the proper behavior should be.
Note that there's an unrelated performance optimization in progress for 3.2 which allows prepared statements to be persisted across connection close/open (see #483). As part of this change Npgsql will no longer use DISCARD ALL (since that closes all prepared statements), but will rather selectively reset the connection state. This might be helpful for this problem as well.
@warcha I think you're facing 2 distinct problems here:
“Unhandled Exception: Npgsql.NpgsqlException: ERROR: prepared transaction with identifier ""3fd8cffd-7114-4a1b-a7d3-19e93c0b27d9"" does not exist”.
I've faced this situation the 2.0.X version of the driver, and I think it is still here in 3.X.
You should note that I'm using Entity Framework.
The problem was that an EF context (thus a NpgsqlConnection) enlisted in a distributed transaction was disposed before the first phase of the 2PC completes. If that case, the transaction is prepared on a connection that is not the one that received the BEGIN; and subsequent transactional statements. Because there is no previous transaction activity on this connection, PREPARE executes WITHOUT error but issues a warning. The result is that there is no transaction prepared. The COMMIT/ROLLACK that follows fails.
You could test it right away in a PgAdmin query window:
PREPARE TRANSACTION 'TEST';
Result is:
WARNING: there is no transaction in progress
Query returned successfully with no result in 12 ms.
The solution is to:
Open the connection explicitly when you're using EF. The goal is to control when your connection is enlisted by the resource manager.
Cache your connection in a static dictionary with the distributed transaction ID as the key.
In this event, use the distributed transaction ID to retrieve your connection, explicitly dispose it.
There's also a bug if you accidentally opens the connection more than once. It is then enlisted multiple times.
@roji I strongly suggest that the transaction identifier used by the driver is the distributed transaction identifier followed by the connector server process id. This is what I'm doing in my PR.
I'll be out of the office for 1 week. See you on August 16th :-).
@omatrot thanks for all the valuable feedback on distributed transactions. There's very little chance I'll be able to focus on them before August 16th :)
@omatrot thanks for that feedback, your timing couldn't have been better as I've been looking into this and for the last few days now. The feedback is invaluable and has saved me a lot of time. Thanks again.
@warcha There is another potential issue depending on your context. Because everything is asynchronous, the TrasactionCompleted event may be fired while the transaction is being prepared and not yet finished (it could take a while, trust me). So the code has to be synchronized, or you may dispose the connection before it has finished prepared the transaction.
In a previous patch on the calling code, I've been sleeping 100ms in the TrasactionCompleted event before disposing the EF context. Now The Driver creates an AutoReset Event that has the name of the transaction. This allow the code in the event to wait for it before disposing the context. This is much safer and faster of course.
@omatrot Thanks for the additional information. I've been flat out looking at other issues and haven't had a chance to chase this up. This will be helpful when I get a chance to investigate this further.
Oops, I somehow forgot to look at this during all the recent rewrite of System.Transactions support... (#1380, #122).
During the rewrite, I implemented a new mechanism for handling connections which are closed while their TransactionScope hasn't completed (the previous mechanism, which put the NpgsqlConnection in a "fake closed" mode, was flawed in several ways). Npgsql will now detach the connector (internal connection) from the NpgsqlConnection and place it in a special list in the pool. If another NpgsqlConnection is opened and enlists to the same Transaction, this connector can be reused (this is an optimization reducing the chance escalation to a distributed transaction will occur). Otherwise it will stay dormant in the special list until its transaction completes, at which point it'll be returned to the pool as a regular, idle connector.
In short, I think your problem should go away - you're welcome to try out a CI nuget for 3.2 from the unstable feed, I'm definitely looking for feedback. Will close this in the meantime.
Activity
roji commentedon Jul 22, 2016
Thanks for reporting this. I know that there are several issues with distributed transactions in Npgsql, and rewriting that is one of the major goals of the next version of Npgsql (3.2). Specifically, the app domains will probably be removed.
It will take me some time before I can attack distributed transactions though.
warcha commentedon Jul 25, 2016
Thanks for looking into this, it would be great if you could have a quick look at the change I suggested above and confirm that it will not have any functional impact other then to stop a DISCARD ALL being attempted during a transaction.
roji commentedon Jul 25, 2016
@warcha unfortunately I don't think things are that simple. DISCARD ALL resets the connection state, and it's important to run it when returning connections to the pool, otherwise things start to leak across connections: imagine you set some PostgreSQL parameter and then close the connection, and another connection randomly sees your change - this could cause chaos.
The problem is more fundamental - I need to investigate exactly what needs to be done. For regular transactions (non-distributed), the transaction is rolled back before closing, which is the right behavior. The issue of closing a connection that has a distributed transaction is very different, and probably requires some special handling at the pool level. There's currently a dirty hack in Npgsql whereby if you close a connection with a distributed connection, it goes into a "fake closed" state, which allows it to be reopened and reused. This is exactly the kind of stuff I want to clean up for 3.2, but I haven't yet had time to research what the proper behavior should be.
Note that there's an unrelated performance optimization in progress for 3.2 which allows prepared statements to be persisted across connection close/open (see #483). As part of this change Npgsql will no longer use DISCARD ALL (since that closes all prepared statements), but will rather selectively reset the connection state. This might be helpful for this problem as well.
warcha commentedon Jul 28, 2016
@roji thanks for the feedback.
omatrot commentedon Aug 5, 2016
@warcha I think you're facing 2 distinct problems here:
I've faced this situation the 2.0.X version of the driver, and I think it is still here in 3.X.
You should note that I'm using Entity Framework.
The problem was that an EF context (thus a NpgsqlConnection) enlisted in a distributed transaction was disposed before the first phase of the 2PC completes. If that case, the transaction is prepared on a connection that is not the one that received the BEGIN; and subsequent transactional statements. Because there is no previous transaction activity on this connection, PREPARE executes WITHOUT error but issues a warning. The result is that there is no transaction prepared. The COMMIT/ROLLACK that follows fails.
You could test it right away in a PgAdmin query window:
PREPARE TRANSACTION 'TEST';Result is:
The solution is to:
There's also a bug if you accidentally opens the connection more than once. It is then enlisted multiple times.
@roji I strongly suggest that the transaction identifier used by the driver is the distributed transaction identifier followed by the connector server process id. This is what I'm doing in my PR.
I'll be out of the office for 1 week. See you on August 16th :-).
Hope that helps.
roji commentedon Aug 6, 2016
@omatrot thanks for all the valuable feedback on distributed transactions. There's very little chance I'll be able to focus on them before August 16th :)
warcha commentedon Aug 7, 2016
@omatrot thanks for that feedback, your timing couldn't have been better as I've been looking into this and for the last few days now. The feedback is invaluable and has saved me a lot of time. Thanks again.
omatrot commentedon Aug 16, 2016
It is August 16th :)
@roji No problem
@warcha There is another potential issue depending on your context. Because everything is asynchronous, the TrasactionCompleted event may be fired while the transaction is being prepared and not yet finished (it could take a while, trust me). So the code has to be synchronized, or you may dispose the connection before it has finished prepared the transaction.
In a previous patch on the calling code, I've been sleeping 100ms in the TrasactionCompleted event before disposing the EF context. Now The Driver creates an AutoReset Event that has the name of the transaction. This allow the code in the event to wait for it before disposing the context. This is much safer and faster of course.
warcha commentedon Aug 30, 2016
@omatrot Thanks for the additional information. I've been flat out looking at other issues and haven't had a chance to chase this up. This will be helpful when I get a chance to investigate this further.
roji commentedon Dec 12, 2016
Oops, I somehow forgot to look at this during all the recent rewrite of System.Transactions support... (#1380, #122).
During the rewrite, I implemented a new mechanism for handling connections which are closed while their TransactionScope hasn't completed (the previous mechanism, which put the NpgsqlConnection in a "fake closed" mode, was flawed in several ways). Npgsql will now detach the connector (internal connection) from the NpgsqlConnection and place it in a special list in the pool. If another NpgsqlConnection is opened and enlists to the same Transaction, this connector can be reused (this is an optimization reducing the chance escalation to a distributed transaction will occur). Otherwise it will stay dormant in the special list until its transaction completes, at which point it'll be returned to the pool as a regular, idle connector.
In short, I think your problem should go away - you're welcome to try out a CI nuget for 3.2 from the unstable feed, I'm definitely looking for feedback. Will close this in the meantime.