multi threading

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
[email protected]
Posts: 1
Joined: Thu 04 Dec 2014 15:51

multi threading

Post by [email protected] » Thu 04 Dec 2014 16:25

Hello,

I am working an a multi threaded win service. the service can trigger up to 15 threads in one time. each of the threads has its own DbContext to work with.

when 2 threads are working on the same table (on a different row) i get following exception

Code: Select all

2014-12-04 17:21:04.3351 Verbeke.WorkEngine.Engine FATAL Devart.Data.PostgreSql.PgSqlException (0x80004005): Unexpected server response.
   at Devart.Data.PostgreSql.PgSqlDataReader.f(Int32 A_0)
   at Devart.Data.PostgreSql.PgSqlCommand.InternalExecute(CommandBehavior behavior, IDisposable stmt, Int32 startRecord, Int32 maxRecords)
   at Devart.Common.DbCommandBase.InternalExecute(CommandBehavior behavior, IDisposable stmt, Int32 startRecord, Int32 maxRecords, Boolean nonQuery)
   at Devart.Common.DbCommandBase.ExecuteDbDataReader(CommandBehavior behavior, Boolean nonQuery)
   at Devart.Common.DbCommandBase.ExecuteDbDataReader(CommandBehavior behavior)
   at Devart.Common.DbCommandBase.ExecuteNonQuery()
   at Devart.Data.PostgreSql.PgSqlConnection.Rollback()
   at Devart.Data.PostgreSql.PgSqlTransaction.Rollback()
   at Devart.Data.PostgreSql.u.p()
   at Devart.Common.DbConnectionBase.Close()
   at Devart.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   at Devart.Data.Linq.DataContext.SubmitChanges()
   at Verbeke.Default.Service.Service.Commit(Int32 userId)
   at Verbeke.Workflow.FileImport.ValidateImportFile.Process()
   at Verbeke.WorkEngine.DllHandler.ExecuteSteps(Lazy`2 step)
any clue on what is going wrong?

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: multi threading

Post by MariiaI » Fri 05 Dec 2014 12:28

dotConnect for PostgreSQL (LinqConnect) is not thread safe, so you should use separate connection for each thread or synchronize the threads yourself.
Please specify the following details:
- the version of PostgreSQL server;
- the version of dotConnect for PotgreSQL;

Please also create a small test project, with which this error could be reproduced, and send it us with the necessary DDL scripts, so that we are able to investigate it and find a solution for you.

jon.ranes
Posts: 6
Joined: Fri 13 Mar 2015 13:36

Re: multi threading

Post by jon.ranes » Fri 13 Mar 2015 13:39

There is definatly a problem here that I wasn't having on 7.3.283 with EF4 which I am now having on 7.3.359.6 with EF6

Have been getting the same as below.

Code: Select all

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> Devart.Data.PostgreSql.PgSqlException: Unexpected server response.
   at Devart.Data.PostgreSql.PgSqlDataReader.f(Int32 A_0)
   at Devart.Data.PostgreSql.PgSqlCommand.InternalExecute(CommandBehavior behavior, IDisposable stmt, Int32 startRecord, Int32 maxRecords)
   at Devart.Common.DbCommandBase.ExecuteDbDataReader(CommandBehavior behavior, Boolean nonQuery)
   at Devart.Data.PostgreSql.Entity.t.a(CommandBehavior A_0)
   at Devart.Common.Entity.ab.b(CommandBehavior A_0)
   at Devart.Data.PostgreSql.Entity.t.b(CommandBehavior A_0)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   --- End of inner exception stack trace ---
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)

jon.ranes
Posts: 6
Joined: Fri 13 Mar 2015 13:36

Re: multi threading

Post by jon.ranes » Fri 13 Mar 2015 13:46

Same as OP also, this happens in multi threaded situations.

And I do have a separate connection for each thread.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: multi threading

Post by MariiaI » Fri 13 Mar 2015 15:08

Please specify the version of PostgreSQL server.
Please also create a small test project, with which this error could be reproduced, and send it us with the necessary DDL scripts, so that we are able to investigate this issue and find a solution for you.

jon.ranes
Posts: 6
Joined: Fri 13 Mar 2015 13:36

Re: multi threading

Post by jon.ranes » Sat 14 Mar 2015 02:28

MariiaI wrote:Please specify the version of PostgreSQL server.
Please also create a small test project, with which this error could be reproduced, and send it us with the necessary DDL scripts, so that we are able to investigate this issue and find a solution for you.
It isn't easy to reproduce, right now it only happens in production sporadically. I am downgrading from ef6 right now to see what combination of ef and dotConnect for PotgreSQL makes it go away, I can tell you that when I know.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: multi threading

Post by MariiaI » Mon 16 Mar 2015 10:48

We are looking forward to your reply with the additional information.

jon.ranes
Posts: 6
Joined: Fri 13 Mar 2015 13:36

Re: multi threading

Post by jon.ranes » Mon 16 Mar 2015 15:45

OK after digging deeper I found out this issue was actually related to unexpected lazy loading. I had jumped from ef4 to ef6 and I found a place where lazy loading was depleting the connection pool.

I had used your template for self-tracking and found that my ObjectContext constructor did not have the lazyloading preference pulled in from my edml.

The line this.ContextOptions.LazyLoadingEnabled = false; was missing from the ObjectContext constructor and I was getting lazy loading when I wasn't expecting it.
Shouldn't the self-tracking template be adding this?

Code: Select all

        public MvcCmsEntities() : 
                base(@"name=MvcCmsEntities", "MvcCmsEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = false;
            Initialize();
        }
Where I did find the setting being used was in ApplyChanges which I am not quite sure why it is there.

jon.ranes
Posts: 6
Joined: Fri 13 Mar 2015 13:36

Re: multi threading

Post by jon.ranes » Mon 16 Mar 2015 16:24

Below is my quick fix to the template

Code: Select all

  private void GenerateContextConstructors(string contextClassName) { 
    string lazyLoadingOptionText = "";
    if (model.LazyLoadingEnabled)
    {
      lazyLoadingOptionText= "this.ContextOptions.LazyLoadingEnabled = true;";
    }
    else
    {
      lazyLoadingOptionText= "this.ContextOptions.LazyLoadingEnabled = false;";
    }      
#>
        #region Constructors

        /// <summary>
        /// Initialize a new <#= contextClassName #> object.
        /// </summary>
        public <#= contextClassName #>() : 
                base(@"<#= model.FormatConnectionString(ModelLanguage.CS) #>", "<#= model.EntityContainerName #>")
        {      
            <#=lazyLoadingOptionText#>      
            Initialize();
        }

jon.ranes
Posts: 6
Joined: Fri 13 Mar 2015 13:36

Re: multi threading

Post by jon.ranes » Tue 17 Mar 2015 09:06

I also seem very much to have more problems with lazyloading in 7.3.359 than I do with 7.3.283. 7.3.283 is the last version that works consistently for me without these weird errors so I am going to sit on that version for a while.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: multi threading

Post by MariiaI » Tue 17 Mar 2015 09:34

Thank you for the additional information. We will fix the Self-Tracking Entity template and inform you when the corresponding build of dotConnect for PostgreSQL is available for download.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: multi threading

Post by MariiaI » Thu 26 Mar 2015 14:13

The bug with missing settings for the LazyLoadingEnabled property in the generated code when working with the Self-Tracking Entity template is fixed.
New build of dotConnect for PostgreSQL 7.3.379 is available for download!
It can be downloaded from http://www.devart.com/dotconnect/postgr ... nload.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=3&t=31496.

Post Reply