Transaction does not match the DataContext.Connection

Transaction does not match the DataContext.Connection

Postby bmarotta » Tue 18 Jan 2011 17:08

It is not the first time I have this error but this time I managed to reproduce it with DbMonitor on.

My application is multithreaded. For some of my contexts I open a connection that is committed on the end. The pattern is always:

Code: Select all
context.Connection.Open();
context.Transaction = Connection.BeginTransaction();
try
{
  // do something
  context.SubmitChanges();
  // do another thing
  context.SubmitChanges();
  context.Transaction.Commit();
}
catch
{
  context.Transaction.Rollback();
}


Sometimes I get the error "Transaction does not match the DataContext.Connection." I could not reproduce the error on my development machine. Only on the server were the application runs under the IIS.

Here follows some lines of the log where the words TRANSACTION or COMMIT appear. The [] is the name of the threads. Please let me know if you want the full logs sent to you.

In the first example, only one thread was starting a transaction. On the second example, two threads at the same time were opening and committing a transaction

2011-01-18 17:33:41,005 INFO [p526918=>CreateProduct(Puma.Transfer.CreateChangeProduct.ProductCreationInitialData)] IssuanceEngine.Data.PumaDataContext Prepare: Description: Prepare: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info:
2011-01-18 17:33:41,005 INFO [p526918=>CreateProduct(Puma.Transfer.CreateChangeProduct.ProductCreationInitialData)] IssuanceEngine.Data.PumaDataContext Execute: Description: Execute: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info: -1
2011-01-18 17:33:41,084 INFO [p526918=>CreateProduct(Puma.Transfer.CreateChangeProduct.ProductCreationInitialData)] IssuanceEngine.Data.PumaDataContext Commit: Description: Commit - Extra info:
2011-01-18 17:33:42,318 INFO [p526918=>CreateProduct(Puma.Transfer.CreateChangeProduct.ProductCreationInitialData)] IssuanceEngine.Data.PumaDataContext Prepare: Description: Prepare: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info:
2011-01-18 17:33:42,318 INFO [p526918=>CreateProduct(Puma.Transfer.CreateChangeProduct.ProductCreationInitialData)] IssuanceEngine.Data.PumaDataContext Execute: Description: Execute: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info: -1
2011-01-18 17:33:42,599 ERROR [p526918=>CreateProduct(Puma.Transfer.CreateChangeProduct.ProductCreationInitialData)] IssuanceEngine.Data.PumaDataContext Fail to get the DataContext error:Transaction does not match the DataContext.Connection.
2011-01-18 17:33:42,599 INFO [p526918=>CreateProduct(Puma.Transfer.CreateChangeProduct.ProductCreationInitialData)] IssuanceEngine.Data.PumaDataContext Rollback: Description: Rollback - Extra info:


2011-01-18 17:37:43,847 INFO [p526918=>ProcessTaskChanged()] IssuanceEngine.Data.PumaDataContext Prepare: Description: Prepare: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info:
2011-01-18 17:37:43,863 INFO [p526918=>ProcessTaskChanged()] IssuanceEngine.Data.PumaDataContext Execute: Description: Execute: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info: -1
2011-01-18 17:37:43,863 INFO [p526918=>ProcessTaskChanged()] IssuanceEngine.Data.PumaDataContext Commit: Description: Commit - Extra info:
2011-01-18 17:38:23,060 INFO [p526918=>AcceptTaskAndSetStatus(5026900,202005)] IssuanceEngine.Data.PumaDataContext Prepare: Description: Prepare: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info:
pum2011-01-18 17:38:23,060 INFO [p526918=>AcceptTaskAndSetStatus(5026900,202005)] IssuanceEngine.Data.PumaDataContext Execute: Description: Execute: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info: -1
2011-01-18 17:38:23,107 INFO [p526918=>ProcessTaskChanged()] IssuanceEngine.Data.PumaDataContext Prepare: Description: Prepare: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info:
2011-01-18 17:38:23,107 INFO [p526918=>ProcessTaskChanged()] IssuanceEngine.Data.PumaDataContext Execute: Description: Execute: SET TRANSACTION ISOLATION LEVEL READ COMMITTED - Extra info: -1
2011-01-18 17:38:23,107 INFO [p526918=>ProcessTaskChanged()] IssuanceEngine.Data.PumaDataContext Commit: Description: Commit - Extra info:
2011-01-18 17:38:23,123 ERROR [p526918=>AcceptTaskAndSetStatus(5026900,202005)] IssuanceEngine.Data.PumaDataContext Fail to get the DataContext error:Transaction does not match the DataContext.Connection.
bmarotta
 
Posts: 34
Joined: Wed 29 Sep 2010 11:27

Postby StanislavK » Thu 20 Jan 2011 14:14

In the line
Code: Select all
context.Transaction = Connection.BeginTransaction();

did you mean
Code: Select all
context.Transaction = context.Connection.BeginTransaction();

instead? If no, please specify where and how is the Connection object defined.

We couldn't reproduce the problem in our environment (provided that the transaction is started for the context connection). Could you please describe how the pattern you've specified is usually executed (in particular, where the context is defined, when it is disposed, and how you are handling multithreading)?
StanislavK
Devart Team
 
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Postby bmarotta » Thu 20 Jan 2011 14:37

Hi Stanislav,

yes I meant:

Code: Select all
context.Transaction = context.Connection.BeginTransaction();


There is nothing special about our context handling. We have only one context per thread (this for sure). It is always started with an using clause

Code: Select all
using (var context = new DataContext())


By multithreading I just meant that our application, being a service provider, launch one query per thread. And I guess it is your connection pooling which is messing with things.

Also, the "do something" and "do another thing" can be really multiple calls to multiple statements doing a lot of things and maybe doing more than two SubmitChanges

This error does not happen all the time. It happens from time to time.
bmarotta
 
Posts: 34
Joined: Wed 29 Sep 2010 11:27

Postby StanislavK » Mon 24 Jan 2011 13:08

Could you please specify the connection string (especially the parameters configuring pooling) you are using?

Also, did you try disabling pooling at all to check whether the issue is related to it? Can the problem be reproduced in this case?
StanislavK
Devart Team
 
Posts: 1710
Joined: Thu 03 Dec 2009 10:48


Return to dotConnect for Oracle