Exception: Cannot add query to cache

Exception: Cannot add query to cache

Postby matt ryan » Wed 30 Jan 2013 22:49

I am receive an invalid operation exception when using linq connect in multi threaded code. Any thoughts on what the solution may be?

Code Example
Code: Select all
public void Test()
{
  var tasks = new List<Task>();
  for (var i = 0; i < numberOfTasks; i++)
  {
    var serverId = i;
    var task = Task.Factory.StartNew(() => Foo(serverId));
    tasks.Add(task);
  }
  Task.WaitAll(tasks.ToArray());
}

private void Foo(string serverId)
{
  using (var context = new DataContext(connectionString))
  {
    (from server in context.Server
     where server.Id == serverId
     select server.Name).First();
  }
}


Exception Details
Code: Select all
System.InvalidOperationException
Cannot add query to cache. An item with the same key already exists.
   at Devart.Data.Linq.Engine.LinkedCache`2.a(TKey A_0, TItem A_1)
   at Devart.Data.Linq.Engine.b3.a(Int32 A_0, cy A_1)
   at Devart.Data.Linq.Engine.bj.a(ci A_0, SqlExpression A_1)
   at Devart.Data.Linq.Engine.ah.a(bq A_0)
   at Devart.Data.Linq.Engine.ah.e(Expression A_0)
   at Devart.Data.Linq.DataProvider.a(Expression A_0)
   at Devart.Data.Linq.Engine.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression)
   at System.Linq.Queryable.First(IQueryable`1 source)
matt ryan
 
Posts: 15
Joined: Wed 30 Jan 2013 22:26

Re: Exception: Cannot add query to cache

Postby MariiaI » Thu 31 Jan 2013 11:49

We couldn't reproduce this issue with your code on our environment. Please try disabling compiled query cache, for example remove (or comment) the next line in the DataContext.Designer.cs file:
Code: Select all
public static CompiledQueryCache compiledQueryCache = CompiledQueryCache.RegisterDataContext(typeof([YourDataContextType]));

For more information about query cache please refer to http://www.devart.com/linqconnect/docs/QueryCache.html

We are sending you a sample project to the e-mail address you provided in your forum profile, please check that the letter is not blocked by your mail filter. Please make changes to it so that the issue could be reproduced and send it back to us or send us your project.
Also, please specify the version of LinqConnect you are using.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Exception: Cannot add query to cache

Postby matt ryan » Thu 31 Jan 2013 16:04

The version being used is 4.1.82.0. I commented out the suggested line but still receive the error. The sample project has not been received, I do not see it in my blocked items either.
matt ryan
 
Posts: 15
Joined: Wed 30 Jan 2013 22:26

Re: Exception: Cannot add query to cache

Postby MariiaI » Fri 01 Feb 2013 09:16

We've sent you the sample again.
If there are any problems with this, please specify some other mail address where we can send the sample (you can contact us at 'support at devart dot com' for this).
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Exception: Cannot add query to cache

Postby matt ryan » Fri 01 Feb 2013 16:57

The project was not received, I have placed a request to suppport with another email address.
matt ryan
 
Posts: 15
Joined: Wed 30 Jan 2013 22:26

Re: Exception: Cannot add query to cache

Postby MariiaI » Mon 04 Feb 2013 09:20

We have contacted you via the email address that you specify as request to our support. Please confirm whether you received a test project or not.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Exception: Cannot add query to cache

Postby matt ryan » Mon 04 Feb 2013 21:02

I have isolated our problem using the test solution. The issue seems to be using a child class of the auto generated DataContext. If the auto generated class is used directly then the exception does not occur. I will update the test solution to reproduce the issue and send it shortly.

Code: Select all
class DataContextImpl : TestDataContext
{
    public DataContextImpl(string connection) : base(connection)
    {
    }

    public DataContextImpl(IDbConnection connection) : base(connection)
    {
    }

    public DataContextImpl(string connection, MappingSource mappingSource) : base(connection, mappingSource)
    {
    }

    public DataContextImpl(IDbConnection connection, MappingSource mappingSource) : base(connection, mappingSource)
    {
    }
}
matt ryan
 
Posts: 15
Joined: Wed 30 Jan 2013 22:26

Re: Exception: Cannot add query to cache

Postby MariiaI » Tue 05 Feb 2013 13:26

Thank you for the additional information. We have reproduced this issue. We will investigate it and inform you about the results as soon as possible.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Exception: Cannot add query to cache

Postby MariiaI » Thu 07 Feb 2013 13:49

The bug related to the "Cannot add query to cache. An item with the same key already exists" exception in multi-threading applications is fixed. The fix is available in the new build of LinqConnect 4.1.184!
It can be downloaded from http://www.devart.com/linqconnect/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=31&t=25834.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Exception: Cannot add query to cache

Postby matt ryan » Thu 07 Feb 2013 17:54

Thank you for the quick turn around on this issue. I have tested it and so far it seems to have resolved the issue.
matt ryan
 
Posts: 15
Joined: Wed 30 Jan 2013 22:26

Re: Exception: Cannot add query to cache

Postby MariiaI » Fri 08 Feb 2013 07:59

If you have any further questions, feel free to contact us.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17


Return to LinqConnect (LINQ to SQL support)