1

I use MySQL (it does not support MARS), and I try to run in parallel multiple SELECTs. The connection string is the same, however for each SELECT I create another db context.

It looks like this:

using (var db = DataContextCreator.Instance.Create())
{
  return db.Customers
           .Where(it => it.customer_Id > 10)
           .Detach(db.Customers);
}

A word about Detach -- it is helper method which takes a record or bunch of records. In the second case it makes the list of them (to make them concrete data), and detaches the records from db context (so GC could free the db context) returning back the detached data.

For now it gives me an error about dreadful "There is already an open DataReader associated with this Connection which must be closed first". Since I don't explicitly use DataReader I would like be 100% sure of the reason.

Is it because I use single connection string to create all those db contexts? Or put in other words -- is MARS required for above scenario?

I am asking because I would like not to reinvent the wheel, and because all my queries are rather small and quick I am thinking about stupid workaround -- using lock in db context creator. This way each block querying database would have guarantee that is not executed in parallel with another one.

greenoldman
  • 16,895
  • 26
  • 119
  • 185

2 Answers2

4

MARS is only a factor when you are running multiple queries at the same time on a single SqlConnection object.

Generally, every DbContext object will have its own DbConnection object, so that is probably not the issue here.

I think the issue here is that, by calling the Detach method, you are probably calling the query while it is still executing.

I would suggest using the AsNoTracking extension methods to achieve your goals.

That is, I would write the function return as:

return db.Customers.Where(it => it.customer_Id > 10).AsNoTracking().ToList();
Jean Hominal
  • 16,518
  • 5
  • 56
  • 90
  • Million thanks you for longer explanation, because you revealed the problem. I use multiple db contexts but I pass the same `EntityConnection` over and over. I changed it now to passing just string (connection string) and create entity connection with db context at the same time. So I have both multiplied. And after that change -- it worked! – greenoldman Jan 19 '13 at 21:21
  • Ok, I thought you were already doing that. Yes, creating the `EntityConnection` object every time is the way to go in your case. ^^; – Jean Hominal Jan 19 '13 at 21:25
2

Is it because I use single connection string to create all those db contexts?

No it can't be result of single connection string, i assume your DataContextCreator.Instance.Create() returns contexts based on same connection. Can you provide DataContextCreator code? Particularly the property Instance.

Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
  • Thank you, Jean was a bit faster, so I already solved the problem. Indeed it was inside the db context creator -- it created multiple db contexts but using one entity connection. – greenoldman Jan 19 '13 at 21:24