handling database connection issues
I have some code that needs to retry whenever it cannot make a database connection, for example 20 times for each login. The website has dozens of users, what would be the best way to handle this? I was thinking of using a counter but as multiple users will be logged on at the same time perhaps some threading would be required, which I am unfamiliar with.
EDIT :
I get the error 'method name expected' on the line where I am instantiating the Thread object. Here is my code :
private static List<Field.Info> FromDatabase(this Int32 _campId)
{
List<Field.Info&开发者_如何学JAVAgt; lstFields = new List<Field.Info>();
Field.List.Response response = new Field.List.Ticket
{
campId = _campId
}.Commit();
if (response.status == Field.List.Status.success)
{
lstFields = response.fields;
lock (campIdLock)
{
loadedCampIds.Add(_campId);
}
}
if (response.status == Field.List.Status.retry)
{
Thread th1 = new Thread(new ThreadStart(FromDatabase(_campId)));
FromDatabase(_campId);
}
return lstFields;
}
My approach would be an event, which increments the counter and calls an asynchronous methode if counter <= 20. Then i would block the current thread and wait for the connection result.
To thread-safe increment your counter use
Interlocked.Increment(ref int counter);
精彩评论