Ich habe die folgende Situation, eine Web-App, unter IIS auf Azure App Service läuft. Diese App, eine Verbindung zu einer Datenbank auf Azure Sql, um Datensätze auf einer Tabelle zu aktualisieren. Mein Problem ist, dass zwei Anrufe, die wirklich, wirklich nahe gemacht werden, INSERT auf der Tabelle zweimal mit dem gleichen Schlüssel aufrufen, und beide erfolgreich sind.Http Handler/Azure SQL Nebenläufigkeit Problem
Nun sind die Details:
SQL-Schema: Mitteilung Id => uniqueidentifier (unique key) Status => int
Meine Web-App ist regelmäßig IHttpHandler
und der Code ist unten.
<!-- language: c# -->
public class NotificationStatus : IHttpHandler
{
internal class Notification
{
public Guid Id { get; set; }
public int Status { get; set; }
SqlConnection Connection; /* Not mapped for obvious reasons */
}
public void ProcessRequest(HttpContext context)
{
Guid id = GetFromContext(context, "id");
int status = GetFromContext(context, "status");
Notification not = Db.RetrieveRow(id); /* Create connection from pool */
try
{
if (not == null)
{
not = new DbRecord
{
Id = id,
Status = status,
};
Db.InsertRow(not); /* Create connection from pool */
}
else
{
if (status > not.Status) /* Prevent setting a minor status */
{
not.Status = status
Db.UpdateRecord(not); /* Use connection attached to the object */
}
}
context.Response.Write("ok");
}
catch (Exception ex)
{
context.Response.Write(ex.Message);
}
}
}
Die Db
ist ein einfacher Wrapper um Dapper
Bibliothek.
Also, die Anfrage waren:
http://myapp.net/notificationstatus.ashx?id=a58441ae-28ac-48f5-ba5a-730060863f17&status=3
http://myapp.net/notificationstatus.ashx?id=a58441ae-28ac-48f5-ba5a-730060863f17&status=1
Am Ende der Aufzeichnung auf der db-Show Status = 1
, und beide Anfrage zurück ok Was nicht passieren sollte. An diesem Punkt habe ich keine Ahnung, was los ist, soweit ich weiß, IIS läuft jede Anfrage auf Thread, dort ist nichts geteilt. SqlConnections
werden aus einem Pool abgerufen, also bezweifle ich, dass dort geteilt wird.
Weiß jemand, was hier passieren könnte.
Sorry, aber ich kann deine Antwort nicht verstehen. – erick2red
Versuchen Sie [remote debuggen] (https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-troubleshoot-visual-studio/#remotedebug) den Code, um die Änderungen von zu verfolgen ** Status ** und was ist wirklich in '' void ProcessRequest (HttpContext Kontext) '' Methode? –
Das wird nicht funktionieren. Es passiert nur ab und zu. Es ist sehr schwierig zu debuggen, da dies zwei verschiedene Threads sind, die beim Debuggen kollidieren müssen. – erick2red