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
Transaction does not rollback if scope timeout has elapsed.
Detailed description:
Consider the following sample. We declare the TransactionScope with scopeTimeOut.
When execute command and the time is over as specified in scopeTimeout. The transaction aborts but the command does not rollback.
using(TransactionScopescope=newTransactionScope(TransactionScopeOption.Required,newTimeSpan(0,0,5))){using(conn1){conn.Open();NpgsqlCommandcmd=conn.CreateCommand();cmd.CommandText="update test_ts set c2='sssss' where c1=1";cmd.ExecuteNonQuery();// the update operation is expected to rollbackSystem.Threading.Thread.Sleep(6000);}scope.Complete();}
Following is the error message and stack trace.
[Error Message]
The transaction has aborted.
[StackTrace]
at System.Transactions.TransactionStateAborted.BeginCommit(InternalTransactio
n tx, Boolean asyncCommit, AsyncCallback asyncCallback, Object asyncState)
at System.Transactions.CommittableTransaction.Commit()
at System.Transactions.TransactionScope.InternalDispose()
at System.Transactions.TransactionScope.Dispose()
Following is the patch to fix the above mentioned issue.
Thanks for raising this. We're aware of some issues with TransactionScope support. Unfortunately our current work on the next major version (v3.0) is already too huge to allow us to tackle that as well, so I was planning a serious pass over TransactionScope for v3.1.
After a very quick and incomplete review of your patch:
Your patch appears to make a lot of changes not related to this specific issue. Can you please separate the fix for this issue from any other changes and submit a minimal patch?
Can you please submit it as a standard github PR rather than a gist
If possible, please add a test reproducing the issue for regression testing (if not I'll do it).
@smmansoor, I tried to reproduce your issue and could not. But I may have an idea: does you connection string include the enlist=true parameter? Npgsql's current support requires that you include this parameter for connection opened inside the TransactionScope. If you didn't include this parameter, your connection is completely disconnected from the TransactionScope, and the rollback has no effect on it.
Note that you can also enlist a connection to a TransactionScope by calling Enlist() on it.
If you're still encountering the error, please take a look at the (fb82fca)[test] I just pushed for this and try to help me reproduce.
Reproduced it. Probably it was because of disabled pooling in the connection string, and it's problem on our side due to a lack of the documentation. It says that Enlist means:
Whether to enlist in an ambient TransactionScope.
And nothing about pooling. ADO.NET documentation has nothing about it too and introduces false expectations due to this paragraph:
Automatic enlistment is the default (and preferred) way of integrating ADO.NET connections with System.Transactions. A connection object will automatically enlist in an existing distributed transaction if it determines that a transaction is active, which, in System.Transaction terms, means that Transaction.Current is not null. Automatic transaction enlistment occurs when the connection is opened. It will not happen after that even if a command is executed inside of a transaction scope.
SqlClient documentations states that it's a responsibility of the pooler (see Enlist in docs):
true indicates that the SQL Server connection pooler automatically enlists the connection in the creation thread's current transaction context.
@YohDeadfall in theory we could look at enabling enlist even if the connection isn't pooled. This shouldn't be too hard, I think the main thing to look out for is when the connection is closed before the TransactionScope completes, in which case it cannot actually be closed (since we haven't committed or rolled back yet). Logic to keep the connection in a special state exists for the pooled scenario, but not for unpooled. I don't think anyone's complained because very few people disable pooling.
Of course we can and should add a note in the meantime...
Activity
roji commentedon Feb 20, 2015
Thanks for raising this. We're aware of some issues with TransactionScope support. Unfortunately our current work on the next major version (v3.0) is already too huge to allow us to tackle that as well, so I was planning a serious pass over TransactionScope for v3.1.
After a very quick and incomplete review of your patch:
If possible, please add a test reproducing the issue for regression testing (if not I'll do it).roji commentedon Mar 4, 2015
I'll try to look at this soon and include it in a 2.2 release.
roji commentedon Mar 4, 2015
@smmansoor, I tried to reproduce your issue and could not. But I may have an idea: does you connection string include the enlist=true parameter? Npgsql's current support requires that you include this parameter for connection opened inside the TransactionScope. If you didn't include this parameter, your connection is completely disconnected from the TransactionScope, and the rollback has no effect on it.
Note that you can also enlist a connection to a TransactionScope by calling Enlist() on it.
If you're still encountering the error, please take a look at the (fb82fca)[test] I just pushed for this and try to help me reproduce.
Passing test for rollback on ambient transaction timeout
roji commentedon Mar 11, 2015
Have you had any time to check this @smmansoor?
smmansoor commentedon Mar 12, 2015
Hi,
Sorry for not responding earlier. I moved away on some other stuff. I will
definitely have a go at it and will get back to you.
Thanks,
--Mansoor
On Thu, Mar 12, 2015 at 12:08 AM, Shay Rojansky [email protected]
wrote:
roji commentedon May 8, 2016
Am closing for inactivity, feel free to take this up again.
YohDeadfall commentedon Mar 12, 2019
Reproduced it. Probably it was because of disabled pooling in the connection string, and it's problem on our side due to a lack of the documentation. It says that
Enlistmeans:And nothing about pooling. ADO.NET documentation has nothing about it too and introduces false expectations due to this paragraph:
SqlClient documentations states that it's a responsibility of the pooler (see
Enlistin docs):roji commentedon Mar 15, 2019
@YohDeadfall in theory we could look at enabling enlist even if the connection isn't pooled. This shouldn't be too hard, I think the main thing to look out for is when the connection is closed before the TransactionScope completes, in which case it cannot actually be closed (since we haven't committed or rolled back yet). Logic to keep the connection in a special state exists for the pooled scenario, but not for unpooled. I don't think anyone's complained because very few people disable pooling.
Of course we can and should add a note in the meantime...