void mDatabaseThread_DoWork(object sender, DoWorkEventArgs e) { while (!mCancellationTokenSource.IsCancellationRequested) { IDbRequest request = null; try { Utility.Log.LogString("Database thread waiting for request"); request = mDatabaseQueue.Take(mCancellationTokenSource.Token); Utility.Log.LogFormat("Database thread dequeued request of type {0}", request.GetType().Name); DbOpVerifySchema schema_request = new DbOpVerifySchema(FFRKProxy.Instance.MinimumRequiredSchema); ProcessDbRequestOnThisThread(schema_request); if (schema_request.Result != DbOpVerifySchema.VerificationResult.OK) { Utility.Log.LogString("Schema verification failed. Disabling database connectivity."); mConnectionState = ConnectionState.Disabled; if (OnSchemaError != null) { OnSchemaError(TranslateSchemaVerificationResult(schema_request.Result)); } if (OnConnectionStateChanged != null) { OnConnectionStateChanged(mConnectionState); } Shutdown(); } else { ProcessDbRequestOnThisThread(request); } } catch (OperationCanceledException) { Utility.Log.LogString("Database worker thread shutting down because cancellation was requested."); } catch (Exception ex) { Utility.Log.LogFormat("Database worker thread encountered an unknown exception. {0}\n{1}", ex.Message, ex.StackTrace); } } Utility.Log.LogString("Database worker thread exiting."); }
public void BeginExecuteRequest(IDbRequest Request) { if (mDatabaseDisabled) { Utility.Log.LogFormat("Ignoring request {0} because database connectivity is disabled.", Request.GetType().Name); return; } try { mDatabaseQueue.Add(Request, mCancellationTokenSource.Token); } catch (Exception ex) { Utility.Log.LogFormat("An error occurred initiating request {0}. Message = {1}.\n{2}", Request.GetType().Name, ex.Message, ex.StackTrace); System.Diagnostics.Debugger.Break(); } }
void ProcessDbRequestOnThisThread(IDbRequest Request) { try { Utility.Log.LogFormat("Database exceuting operation {0}", Request.GetType().Name); EnsureConnected(); if (Request.RequiresTransaction) { using (MySqlTransaction transaction = mConnection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted)) { try { Request.Execute(mConnection, transaction); transaction.Commit(); Request.Respond(); } catch (Exception ex) { Utility.Log.LogFormat("An error occurred executing the operation in a transaction. Rolling back. {0}", ex.Message); Utility.Log.LogFormat(ex.StackTrace); transaction.Rollback(); } } } else { Request.Execute(mConnection, null); Request.Respond(); } } catch (Exception ex) { Utility.Log.LogFormat("An error occurred executing request {0}. Message = {1}.\n{2}", Request.GetType().Name, ex.Message, ex.StackTrace); } }