Page 1 of 1

OptimisticConcurrencyException

Posted: Thu 05 Jun 2014 14:35
by fuji
Hi,

We are using dotConnect for Oracle v8.3.146.0 in our project.
We having problem with the OptimisticConcurrencyException that is thrown in case of concurrency - the StateEntries collection that contains the items that have the problem is empty.
I did a research in Internet but found nothing about the reason that could cause that, so I looked in the code and I saw that you are using the constructor of OptimisticConcurrencyException that takes just a string as parameter:

Code: Select all

namespace Devart.Common.Entity
{
 internal abstract class dq : cl
 {
  protected Exception a(int A_0, int A_1)
  {
   return (Exception) new OptimisticConcurrencyException(string.Format("Store update, insert, or delete statement affected an unexpected number of rows    ({0} of the expected {1}). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.", (object) A_0,    (object) A_1));
  }
 }
}

Is there any special reason to do that?
Is it possible to change it with the constructor

Code: Select all

public OptimisticConcurrencyException(string message, Exception innerException, IEnumerable<ObjectStateEntry> stateEntries)
so we can get the StateEntries collection and resolve our problem?

Here is the StackTrace:

Devart.Data.Oracle.Entity.dll!Devart.Data.Oracle.Entity.o.a(System.Data.Common.DbCommand A_0, int A_1) + 0x33b bytes
Devart.Data.Oracle.Entity.dll!Devart.Common.Entity.dq.m() + 0x1ee bytes
Devart.Data.Oracle.Entity.dll!Devart.Common.Entity.dq.j() + 0x191 bytes
Devart.Data.Oracle.Entity.dll!Devart.Common.Entity.cr.b() + 0x24a bytes
Devart.Data.Oracle.Entity.dll!Devart.Common.Entity.bu.h() + 0x18d bytes
Devart.Data.Oracle.Entity.dll!Devart.Common.Entity.bu.c() + 0x184 bytes
Devart.Data.Oracle.Entity.dll!Devart.Common.Entity.bu.a(object A_0, Devart.Common.TransactionStateChangeEventArgs A_1) + 0x1d2 bytes
Devart.Data.Oracle.dll!Devart.Common.DbConnectionBase.a(Devart.Common.TransactionAction A_0) + 0x24e bytes
Devart.Data.Oracle.dll!Devart.Common.DbConnectionBase.a(object A_0, Devart.Common.TransactionStateChangeEventArgs A_1) + 0x1df bytes
Devart.Data.dll!Devart.Common.DbTransactionBase.OnStateChanging(Devart.Common.TransactionAction action, System.Data.Common.DbConnection connection) + 0x26a bytes
Devart.Data.Oracle.dll!Devart.Data.Oracle.OracleTransaction.Commit() + 0x1be bytes
> System.Data.Entity.dll!System.Data.EntityClient.EntityTransaction.Commit() Line 92 + 0x150 bytes C#

Thank you

Re: OptimisticConcurrencyException

Posted: Fri 06 Jun 2014 14:11
by Shalex
When concurrency check is enabled, the StateEntries property of the generated OptimisticConcurrencyException if any is not initialized because batch updates are managed by provider at ADO.NET level where provider does not have the Entity Framework level information about states of entities in the context's collection.

Here is a workaround for single-threaded application:

Code: Select all

                try
                {
                  context.SaveChanges();
                }
                catch (Exception ex)
                {
                    OptimisticConcurrencyException concurrencyException = ex as OptimisticConcurrencyException;
                    if (concurrencyException == null && ex.InnerException != null)
                        concurrencyException = ex.InnerException as OptimisticConcurrencyException;
                    if (concurrencyException != null)
                    {
                        config.DmlOptions.BatchUpdates.Enabled = false;
                        try
                        {
                            context.SaveChanges();
                        }
                        catch (OptimisticConcurrencyException exceptionWithFullInfo)
                        {
                            var entries = exceptionWithFullInfo.StateEntries;
                            // ...
                        }
                    }
                    else
                        throw;
                }

Re: OptimisticConcurrencyException

Posted: Fri 06 Jun 2014 19:12
by fuji
Thank you for your answer!

I suppose that by config you mean OracleEntityProviderConfig.

I tried to use the following code to switch the BatchUpdate option but I got a System.TypeInitializationException with the following message "The type initializer for 'Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig' threw an exception.". The inner exception is System.IO.FileNotFoundException "Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies."

Code: Select all

OracleEntityProviderConfig config = OracleEntityProviderConfig.Instance;
config.DmlOptions.BatchUpdates.Enabled = false;
Why it is trying to find EF 6.0 assembly when I'm using EF 4.0?

Re: OptimisticConcurrencyException

Posted: Mon 09 Jun 2014 08:19
by Shalex
fuji wrote:"Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies."

Why it is trying to find EF 6.0 assembly when I'm using EF 4.0?
The dotConnect for Oracle installation includes several versions of the Devart.Data.Oracle.Entity.dll assembly:
\Program Files (x86)\Devart\dotConnect\Oracle\Entity\EF1\Devart.Data.Oracle.Entity.dll
\Program Files (x86)\Devart\dotConnect\Oracle\Entity\EF4\Devart.Data.Oracle.Entity.dll
\Program Files (x86)\Devart\dotConnect\Oracle\Entity\EF5\Devart.Data.Oracle.Entity.dll
\Program Files (x86)\Devart\dotConnect\Oracle\Entity\EF6\Devart.Data.Oracle.Entity.dll

Please run your project in the debug mode, set a breakpoint, navigate to the Debug > Windows > Modules menu and make sure that Devart.Data.Oracle.Entity.dll from the \Program Files (x86)\Devart\dotConnect\Oracle\Entity\EF4\ folder is loaded in the process of your application.

Re: OptimisticConcurrencyException

Posted: Mon 09 Jun 2014 16:33
by fuji
As you said I had a bad reference added to my project - it was pointing to the dll for EF6.

Thank you!