0

Recently I came across kinda funny issue. Generally I am working on .NET web app hosted on IIS and using MS SQL server.

Having said that I wrote a simple piece of code to update specific record in db. The record should already exist and no further operations should occur on it (or actually even the entire table) for considerable time (no concurrent operation should appear to overwrite update).

var record = db.InputDataRecords.Where(i => i.Id == inputDataId).Single();
record.InputData = updatedInputData;
db.SaveChangesAsync();

So I wrote the above code, tested it on my local machine, everything worked perfectly fine so I just left it as it was.

Recently the code finally got to the test server (pretty slow sucker) and to my surprise I got info from the test crew that the records do not update accordingly. What is even worse no errors were logged although they should if something went wrong plus I am pretty sure the above code did not throw any errors as, for the sake of peace, I forced it to log message with some details after it is executed (I cannot debug on that machine unfortunately so I couldn't get any better feedback).

I struggled with this issue for couple of hours doing some small adjustments here and there, but nothing was really helping. Finally switching to synchronous SaveChanges() did the trick.

Did anyone of you come across similar issue? I am pretty curious why it could occur.

  • 3
    If you use `SaveChangesAsync` you need to `await` it or it may never actually run. – DavidG Mar 02 '17 at 13:16
  • Possible duplicate of http://stackoverflow.com/questions/30388844/savechangesasync-not-updating-value-in-database-table. I am not marking it as exact duplicate until you confirm this was the issue. – Wiktor Zychla Mar 02 '17 at 13:21
  • @DavidG - you need to `await` it to discover that it's *completed* or to *discover the errors*, but the mere act of calling it does cause it to *start* to run. – Damien_The_Unbeliever Mar 02 '17 at 13:22
  • @Damien_The_Unbeliever Yeah, I meant it may not *complete* – DavidG Mar 02 '17 at 13:23
  • The case is more - why on my local machine `SaveChangesAsync` works always (let's say 50 out of 50 times) without `await`, while on that server it does not work at all? Seems kinda counterintuitive. – Piotrek B. Mar 02 '17 at 13:55
  • @PiotrekB. - until you `await` the `Task`, or arrange to `ContinueWith` on it, and actually discover if it's throwing exceptions, we're as much in the dark on the actual cause as you are. At the moment, this is "guess why this code is (probably) throwing an exception" when you can't even give us the type of the exception or any related message or stack trace. – Damien_The_Unbeliever Mar 02 '17 at 14:16
  • Is your database also on your local machine? It could be that SaveChangesAsync runs so fast that it manages to complete before the request ends. – Crowcoder Mar 02 '17 at 14:25
  • @Crowcoder - in both cases IIS and SQL server are on the same machine, that is I have IIS and MS SQL locally for my local work and IIS and MS SQL on remote machine. – Piotrek B. Mar 02 '17 at 14:30
  • That doesn't mean the execution will run exactly the same, in the same amount of time. Unless you offload the database work to a distributed process (queue, for instance) then you must await or you will always have failures because the code keeps running which means the web request is torn down and when it is, anything running asynchronously is too. – Crowcoder Mar 02 '17 at 14:42

0 Answers0