Skip to content

Concurrency-Exception-Handling fails #11

Description

@fgieseke

Situation:
Pseudo-Code for handling Concurrency-Exception
using(testScope = ...)
{
try
{
repository.Update(projekt);
testScope.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
foreach (var entry in ex.Entries)
{
entry.OriginalValues.SetValues(entry.GetDatabaseValues());
}
testScope.SaveChanges(); //Retry to update Fails, because DbContextCollection says You can't call Commit() or Rollback() more than once on a DbContextCollection.
}

What happens in DbContextCollection.cs "Commit"-method:

115 foreach (var dbContext in _initializedDbContexts.Values)
116 {
117 try
118 {
119 if (!_readOnly)
120 {
121 c += dbContext.SaveChanges();
122 }
123
124 // If we've started an explicit database transaction, time to commit it now.
125 var tran = GetValueOrDefault(_transactions, dbContext);
126 if (tran != null)
127 {
128 tran.Commit();
129 tran.Dispose();
130 }
131 }
132 catch (Exception e)
133 {
134 lastError = ExceptionDispatchInfo.Capture(e);
135 }
136 }
137

138 transactions.Clear();
139 completed = true; // *!!! in case of an exception this ist wrong. This Statement should be placed in line 130. *

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions