Page 1 of 1

Exception: Cannot add query to cache

Posted: Wed 30 Jan 2013 22:49
by matt ryan
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)

Re: Exception: Cannot add query to cache

Posted: Thu 31 Jan 2013 11:49
by MariiaI
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.

Re: Exception: Cannot add query to cache

Posted: Thu 31 Jan 2013 16:04
by matt ryan
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.

Re: Exception: Cannot add query to cache

Posted: Fri 01 Feb 2013 09:16
by MariiaI
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).

Re: Exception: Cannot add query to cache

Posted: Fri 01 Feb 2013 16:57
by matt ryan
The project was not received, I have placed a request to suppport with another email address.

Re: Exception: Cannot add query to cache

Posted: Mon 04 Feb 2013 09:20
by MariiaI
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.

Re: Exception: Cannot add query to cache

Posted: Mon 04 Feb 2013 21:02
by matt ryan
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)
    {
    }
}

Re: Exception: Cannot add query to cache

Posted: Tue 05 Feb 2013 13:26
by MariiaI
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.

Re: Exception: Cannot add query to cache

Posted: Thu 07 Feb 2013 13:49
by MariiaI
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.

Re: Exception: Cannot add query to cache

Posted: Thu 07 Feb 2013 17:54
by matt ryan
Thank you for the quick turn around on this issue. I have tested it and so far it seems to have resolved the issue.

Re: Exception: Cannot add query to cache

Posted: Fri 08 Feb 2013 07:59
by MariiaI
If you have any further questions, feel free to contact us.