I am using EF Core 2.2.4. All SQL tables have timestamp column. I have method below that tries to get next WorkOrder for given user. If there are multiple users competing at the same time we may get DbUpdateConcurrencyException which i handle using re-try logic.
Note that DBCntext is injected into service's constructor as Scoped instance
public async Task<WorkOrder> GetNextOrder(int userID)
{
var maxTry = 3;
WorkOrder order = null;
for (int i = 0; i < maxTry; i++)
{
order = await _dbContext.WorkOrder
.Where(o => o.UserID == null)
.OrderBy(o => o.CreatedDateTime)
.FirstOrDefaultAsync();
if (order == null)
{
break; //exit out of the loop
}
order.StatusID = (int)Statuses.InProgress;
order.UserID = userID;
order.AssignedDateTime = DateTime.UtcNow;
try
{
await _dbContext.SaveChangesAsync();
i = maxTry;
}
catch (DbUpdateConcurrencyException ex)
{
// since mutiple users are compting for orders. We may get concurrency issue
// in such case retry to get next order
await _dbContext.Entry(order).ReloadAsync();
order = null;
}
}
return order;
}
This is been working, but now number of users are increased and when they competing for WorkOrders most of the time method above exhaust retry and returns null for WorkOrder. Of-course i can increase the counter to 10 or 20 and that might work but i cannot keep increasing the counter as users are increasing.
I wanted to know if there is a way to lock the selected record so other execution does not read the same record or waits till the first one complete?
I would defiantly do not want to lock the whole table
I tried transaction but it did not work
public async Task<WorkOrder> GetNextOrder(int userID)
{
var maxTry = 3;
WorkOrder order = null;
using (var transaction = await _dbContext.Database.BeginTransactionAsync())
{
for (int i = 0; i < maxTry; i++)
{
order = await _dbContext.WorkOrder
.Where(o => o.UserID == null)
.OrderBy(o => o.CreatedDateTime)
.FirstOrDefaultAsync();
if (order == null)
{
break; //exit out of the loop
}
order.StatusID = (int)Statuses.InProgress;
order.UserID = userID;
order.AssignedDateTime = DateTime.UtcNow;
try
{
await _dbContext.SaveChangesAsync();
i = maxTry;
}
catch (DbUpdateConcurrencyException ex)
{
// since mutiple users are compting for orders. We may get concurrency issue
// in such case retry to get next order
await _dbContext.Entry(order).ReloadAsync();
order = null;
}
}
transaction.Commit();
}
return order;
}
Update 1
So EF Core does not support Pessimistic Concurrency (Locking) out of the box, It supports only Optimistic Concurrency. That means you allow concurrency conflict to happen and then react accordingly.
So next solution i was thinking instead of taking FirstOrDefault() is it possible to take random record?
order = await _dbContext.WorkOrder
.Where(o => o.UserID == null)
.OrderBy(o => o.CreatedDateTime)
.TakeRandonRecord() ??
Is there any way to select random record without loading entire recordset into memory?
Related
I'm having real difficulty with EF Core with a Web API project I'm working... to me EF Core is not intuitive at all. I'm in a disconnected environment and I'm trying to update Sudoku games. EF Core is spending more time deleting connections between users and their apps and roles than in updating the game. How do I disable delete statements in an update? There is no reason for deletes, I don't need them. How do I stop them?
The method is as follows, the game is loaded as a graph and my understanding is this code should change everything tracked to modified or added. To me it seems like EF Core is going out of it's way to delete things... this makes no sense. I never instructed it to delete anything:
async public Task<IRepositoryResponse> Update(TEntity entity)
{
var result = new RepositoryResponse();
try
{
dbSet.Update(entity);
context.ChangeTracker.TrackGraph(entity,
e => {
var dbEntry = (IEntityBase)e.Entry.Entity;
if (dbEntry.Id != 0)
{
e.Entry.State = EntityState.Modified;
}
else
{
e.Entry.State = EntityState.Added;
}
});
await context.SaveChangesAsync();
result.Success = true;
result.Object = entity;
return result;
}
catch (Exception exp)
{
result.Success = false;
result.Exception = exp;
return result;
}
}
Well, I found a work around but it is the equivalent to the fixing your bike with bubblegum and tape. It's ugly... but it works. Before I save the game I create a list of all associated apps and roles and then recreate and resave the values after await context.SaveChangesAsync();. The code is listed below:
async public Task<IRepositoryResponse> Update(TEntity entity)
{
var result = new RepositoryResponse();
try
{
entity.DateUpdated = DateTime.UtcNow;
context.Games.Update(entity);
context.ChangeTracker.TrackGraph(entity,
e => {
var dbEntry = (IEntityBase)e.Entry.Entity;
if (dbEntry.Id != 0)
{
e.Entry.State = EntityState.Modified;
}
else
{
e.Entry.State = EntityState.Added;
}
});
var apps = new List<App>();
var roles = new List<Role>();
foreach (var userApp in entity.User.Apps)
{
apps.Add(userApp.App);
}
foreach (var userRole in entity.User.Roles)
{
roles.Add(userRole.Role);
}
await context.SaveChangesAsync();
foreach (var app in apps)
{
userAppDbSet.Add(new UserApp(entity.UserId, app.Id));
}
foreach (var role in roles)
{
userRoleDbSet.Add(new UserRole(entity.UserId, role.Id));
}
await context.SaveChangesAsync();
result.Success = true;
result.Object = entity;
return result;
}
catch (Exception exp)
{
result.Success = false;
result.Exception = exp;
return result;
}
}
There has to be a better way of doing this? The full app can be found here, can someone tell me a better way of setting this up:
https://github.com/Joseph-Anthony-King/SudokuCollective
I have the following code, which stores information in two different tables in the same method
public static async Task<Response> AddStockTransaction(StockTransactionsHeader header, List<StockTransactionsDetails> details)
{
using (DataContext dbContext = new DataContext())
{
try
{
dbContext.StockTransactionsHeader.Add(header);
await dbContext.SaveChangesAsync();
int hearderID = header.TransactionHeaderID;
foreach (var item in details)
{
item.TransactionHeaderID = hearderID;
}
dbContext.StockTransactionsDetails.AddRange(details);
await dbContext.SaveChangesAsync();
return new Response
{
IsSuccess = true
};
}
catch (Exception ex)
{
return new Response
{
IsSuccess = false,
Message = ex.Message
};
}
}
}
How can I do, in case there is an exception in the second SaveChanges () to revert the first one?
Once SaveChanges has been called, your datat is stored on your database. You should not call SaveChanges more than once in a call, unless you are willingly to persist the intermediate steps.
You can use a transaction scope to create managed transactions :
using (TransactionScope scope = CreateTransactionScope())
{
DoSomthing(context);
scope.Complete();
}
however, if the failure of the second part involves rolling back the first one, this means that both parts belong to the same transaction, therefore simply omitting the first SaveChanges would turn your code into a single transaction.
From my another awnser: You could use DbTransaction class.
private void TestTransaction()
{
var context = new MyContext(connectionString);
using (var transaction = context.Database.BeginTransaction())
{
try
{
// do your stuff
// commit changes
transaction.Commit();
}
catch
{
// 'undo' all changes
transaction.Rollback();
}
}
}
I have two similar plugins in Dynamics CRM 2016 ONPREMISE to merge into one.
They're
registered to the same entity,
both triggered by update message,
one plugin check value of 3 fields, the other check value of 4 fields. If all equal to a specified value, then go on. If not, return.
4.set, map or calculate the value from old record to new record. two plugins handle two sets of fields.
create a new record.
What I can think is "if else-if " structure. But it looks so naive. Anybody have any advice?
The other plugin checks 3 other fields and execute similar action creating new record with some other fields set or mapped.
Thanks,
protected void ExecuteApplication(LocalPluginContext localContext)
{
IPluginExecutionContext context = null;
IOrganizationService service = null;
ITracingService tracer = null;
context = localContext.PluginExecutionContext;
service = localContext.OrganizationService;
tracer = localContext.TracingService;
try
{
// ensure we have an application and update message
Entity application = new Entity(applicationEntityName);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
application = (Entity)context.InputParameters["Target"];
if (!application.LogicalName.ToLower().Equals(this.applicationEntityName))
{
return;
}
}
else
{
return;
}
if (context.MessageName.ToLower() != "update")
{
return;
}
// Fetch data from PreImage
Entity postImageApplication = context.PostEntityImages["PostImage"];
//check three fields are not null
if (application.GetAttributeValue<OptionSetValue>("statuscode") == null ||
postImageApplication.GetAttributeValue<EntityReference>("new_service").Name == null ||
postImageApplication.GetAttributeValue<EntityReference>("new_source").Name == null)
{
return;
}
if (
application.GetAttributeValue<OptionSetValue>("statuscode").Value == 881780003 &&
postImageApplication.GetAttributeValue<EntityReference>("new_service").Name == "CIC"
)
// process if the update meets the criteria
{
Entity newApplication = new Entity("new_application");
// set
newApplication.Attributes.Add("new_effectiveapplication", true);
newApplication.Attributes.Add("new_confirmdate", DateTime.Now);
newApplication.Attributes.Add("new_signdate", DateTime.Now);
//mapped
if (postImageApplication.Attributes.Contains("new_client"))
{
newApplication.Attributes.Add("new_client", postImageApplication["new_client"]);
}
if (postImageApplication.Attributes.Contains("new_servicecentre"))
{
newApplication.Attributes.Add("new_servicecentre", postImageApplication["new_servicecentre"]);
}
service.Create(newApplication);
}
else
{
return;
}
I like to abstract unwieldy predicates into their own method.
How about something like this:
private bool allFieldsHaveValues()
{
return application.GetAttributeValue<OptionSetValue>("statuscode") != null
&& postImageApplication.GetAttributeValue<EntityReference>("new_service").Name != null
&& postImageApplication.GetAttributeValue<EntityReference>("new_source").Name != null;
}
private bool valuesAreValid()
{
return application.GetAttributeValue<OptionSetValue>("statuscode").Value == 881780003
&& postImageApplication.GetAttributeValue<EntityReference>("new_service").Name == "CIC";
}
if (allFieldsHaveValues() && valuesAreValid())
{
Entity newApplication = new Entity("new_application");
I want to get followers Id's for a particular userId using java program. where I want to implement the cursor concept with rate limit set ... Can any one post me the code.
Use the following code snippet to get follower id. After getting the ids you use show user to get other details. Remember to use this code in background thread like in asynctask.
long[] tempids = null;
ConfigurationBuilder config =
new ConfigurationBuilder()
.setOAuthConsumerKey(custkey)
.setOAuthConsumerSecret(custsecret)
.setOAuthAccessToken(accesstoken)
.setOAuthAccessTokenSecret(accesssecret);
twitter1 = new TwitterFactory(config.build()).getInstance();
while(cursor != 0) {
try {
IDs temp = twitter1.friendsFollowers().getFollowersIDs("username", cursor);
cursor = temp.getNextCursor();
tempids = temp.getIDs();
} catch (twitter4j.TwitterException e) {
System.out.println("twitter: failed");
e.printStackTrace();
return null;
}
if(tempids != null) {
for (long id : tempids) {
ids.add(id);
System.out.println("followerID: " + id);
}
}
}
I have an app that reads a lot of data into memory and processes it in a batches.
What I want is for entity framework to ignore DbUpdateConcurrencyException when deleting an entity that has already been deleted.
The reason is that by the time an entity has been processed and marked for deletion, it may already have been deleted from the DB.
Obliviously deleting a row that has already been deleted isn't a problem and shouldn't cause an error, I just need a way to tell entity framework that :)
Example
Db.Entry(itemToRemove).State = EntityState.Deleted;
Db.SaveChanges();
Causes an error if itemToRemove has already been deleted.
Note: Db.Configuration.ValidateOnSaveEnabled = false; doesn't fix this as another thread suggested.
How about?
Db.Entry(itemToRemove).State = EntityState.Deleted;
bool saveFailed;
do
{
saveFailed = false;
try
{
Db.SaveChanges();
}
catch(DbUpdateConcurrencyException ex)
{
saveFailed = true;
var entry = ex.Entries.Single();
//The MSDN examples use Single so I think there will be only one
//but if you prefer - do it for all entries
//foreach(var entry in ex.Entries)
//{
if(entry.State == EntityState.Deleted)
//When EF deletes an item its state is set to Detached
//http://msdn.microsoft.com/en-us/data/jj592676.aspx
entry.State = EntityState.Detached;
else
entry.OriginalValues.SetValues(entry.GetDatabaseValues());
//throw; //You may prefer not to resolve when updating
//}
}
} while (saveFailed);
More here:
Resolving optimistic concurrency exceptions
I posted this question a long time ago but it has recently had some attention so I though I would add the solution I actually use.
//retry up to 5 times
for (var retries = 0; retries < 5; retries++)
{
try
{
Db.SaveChanges();
break;
}
catch (DbUpdateConcurrencyException ex)
{
foreach (var entity in ex.Entries)
{
entity.State = EntityState.Detached;
}
}
}
Things I considered - I did NOT want to use ReloadAsync() or ObjectContext.Refresh as I wanted to ignore items deleted in another process WITHOUT any additional database overhead.
I added in the for loop as a simple protection against infinite loops - not something that should be able to happen, but I'm a belt and braces approach man and not a fan of while(true) if it can be avoided.
No need to a local variable like isDone or saveFailed - simply break if we saved successfully.
No need to cast ex.Entries to a list in order to enumerate it - just because you can write something on one line doesn't make it better.
You could handle the DbUpdateConcurrencyException and then call Refresh(RefreshMode,IEnumerable) with RefreshMode.StoreWins and your deleted entities as parameter.
try{
Db.Entry(itemToRemove).State = EntityState.Deleted;
Db.SaveChanges();
}
catch(DbUpdateConcurrencyException)
{
IObjectContextAdapter adapter = Db;
adapter.ObjectContext.Refresh(RefreshMode.StoreWins, context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted));
Db.SaveChanges();
}
Based on the code from https://msdn.microsoft.com/en-US/data/jj592904 but where I added an infite loop counter (just in case, you never know, right?) and looping through all the entries in the exception's list.
var maxTriesCounter = 20;
bool saveFailed;
do
{
saveFailed = false;
maxTriesCounter--;
try
{
context.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
foreach (var entry in ex.Entries)
{
entry.Reload();
}
}
} while (saveFailed && maxTriesCounter > 0);
Here is what I use. Detach all problem records after the save.
Db.Entry(itemToRemove).State = EntityState.Deleted;
while(true)
try {
Db.SaveChanges();
break;
} catch (DbUpdateConcurrencyException ex) {
ex.Entries.ToList().ForEach(x=>x.State=EntityState.Detached);
}
Or you could add a custom SaveChanges function to your DbContext class and use it instead whenever you need to ignore those errors.
public int SaveChanges_IgnoreConcurrencyExceptions () {
while(true)
try {
return this.SaveChanges();
} catch (DbUpdateConcurrencyException ex) {
ex.Entries.ToList().ForEach(x => x.State=EntityState.Detached);
}
}
This is my approach:
public async Task DeleteItem(int id)
{
bool isDone = false;
while (!isDone)
{
var item= await dbContext.Items.AsNoTracking().SingleOrDefaultAsync(x=> x.id== id);
if (item== null)
return;
dbContext.Items.Delete(item);
try
{
await dbContext.CommitAsync();
return;
}
catch (DbUpdateConcurrencyException ex)
{
}
}
}
This is another approach:
context.Delete(item);
bool saveFailed;
do
{
saveFailed = false;
try
{
await context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
var entity = ex.Entries.Single();
await entity.Single().ReloadAsync();
if (entity.State == EntityState.Unchanged)// entity is already updated
context.Delete(item);;
else if (entity.State == EntityState.Detached) // entity is already deleted
saveFailed =false;
}
} while (saveFailed);
ReloadAsync() method as of Microsoft docs :
Reloads the entity from the database overwriting any property values
with values from the database.
The entity will be in the Unchanged state after calling this method,
unless the entity does not exist in the database, in which case the
entity will be Detached. Finally, calling Reload on an Added entity
that does not exist in the database is a no-op. Note, however, that an
Added entity may not yet have had its permanent key value created.