Page 1 of 1

How can i use Parallel Selects?

Posted: Mon 06 May 2013 13:13
by Samircj
I've tryed this Singleton

Code: Select all

partial class VenezaEntities
    {
        static VenezaEntities instance = null;
        static readonly object padlock = new object();
        public const string DefaultConnectionString = "Data Source=ms-appdata:///local/veneza.db";

        public static VenezaEntities Instance
        {
            get
            {
                lock(padlock)
                    if(instance == null)
                    {
                        instance = new VenezaEntities(DefaultConnectionString);
                    }
                return instance;
            }
        }
}
And always got this error:

Code: Select all

{"Error on opening DbConnection."}
{"The connection was not closed."}

StackTrace Devart.Data.Linq.LinqCommandExecutionException.CanThrowLinqCommandExecutionException(String  , Exception  )
   at Devart.Data.Linq.Engine.ConnectionManager.GetOpenConnection()
   at Devart.Data.Linq.Engine.ConnectionManager.cva5p653c7dmud6wp92shjm8z8e47zrn    (IConnectionUser  )
   at Devart.Data.Linq.DataProvider. (ICompiledQuery  , Object[]  )
   at Devart.Data.Linq.DataProvider.7axdkwt3gfx52xqzykge2d4jpayt4hd5    (Expression  )
   at Devart.Data.Linq.Table`1.vfdyx8eadthyzp7z2f27edhsaq4phr44    [TResult](Expression  )
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at VenezaRT.App.<DeployDatabase>d__0.MoveNext() in d:\...\App.xaml.cs:line 137
How can i implement a single connection to use Parallel Selects?
I was implemented with "using (var context = VenezaEntities.Create())", but got problem with parallel threads.

Re: How can i use Parallel Selects?

Posted: Wed 08 May 2013 11:01
by MariiaI
The issue with the "Connection not closed" exception in Windows Store apps is a common one. This is an expected behavior and it is related to the situation when one DataContext object is accessed from different threads simultaneously. DataContext objects are not thread-safe, at least because a DataContext instance uses a single connection, which is not thread-safe itself.

Due to the fact that likelihood of trying to access DataContext on several different threads is high, we don't recommend using a static instance of DataContext. DataContext is a lightweight object, and it is recommended to use a new DataContext for each unit of work instead of keeping it for a long time.
But if you want to use one instance of DataContext globally, you should maintain thread synchronization yourself.

If this doesn't help, please send us your sample, so that we could find a solution for you.