private bool ExecuteSQLCommand( AsyncRPGDataContext dbContext, DatabaseCommandDelegate commandDelegate, out string result_code) { System.Data.Common.DbTransaction dbTransaction = null; bool success = false; try { // Open the db connection immediately dbContext.Connection.Open(); // Create a transaction for the request processor. // If anything fails along the way, roll back everything. dbTransaction = dbContext.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); // Tell the context about the transaction so that it doesn't create one of it's own dbContext.Transaction = dbTransaction; // Run the command on the database commandDelegate(dbContext); // Commit the transaction to the DB up success dbTransaction.Commit(); dbTransaction = null; result_code = SuccessMessages.GENERAL_SUCCESS; success = true; } catch (System.Transactions.TransactionAbortedException ex) { // Our attempt to rollback a failed transaction failed. // Possible data corruption :( result_code = ErrorMessages.DB_ERROR + "(Transaction Aborted:" + ex.Message + ")"; success = false; } catch (System.Exception ex) { // An unexpected error occurred // Attempt to rollback any db changes if any were made if (dbTransaction != null) { dbTransaction.Rollback(); dbTransaction = null; } // Don't bother retrying in this case result_code = ex.Message; success = false; } finally { // In all cases, make sure to close the connection to the DB if (dbContext.Connection.State == ConnectionState.Open) { dbContext.Connection.Close(); } dbContext = null; } return success; }
private bool ExecuteSQLCommand( AsyncRPGDataContext dbContext, DatabaseCommandDelegate commandDelegate, out string result_code) { System.Data.Common.DbTransaction dbTransaction = null; bool success = false; try { // Open the db connection immediately dbContext.Connection.Open(); // Create a transaction for the request processor. // If anything fails along the way, roll back everything. dbTransaction = dbContext.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); // Tell the context about the transaction so that it doesn't create one of it's own dbContext.Transaction = dbTransaction; // Run the command on the database commandDelegate(dbContext); // Commit the transaction to the DB up success dbTransaction.Commit(); dbTransaction = null; result_code = SuccessMessages.GENERAL_SUCCESS; success = true; } catch (System.Transactions.TransactionAbortedException ex) { // Our attempt to rollback a failed transaction failed. // Possible data corruption :( result_code = ErrorMessages.DB_ERROR + "(Transaction Aborted:" + ex.Message + ")"; success = false; } catch (System.Exception ex) { // An unexpected error occurred // Attempt to rollback any db changes if any were made if (dbTransaction != null) { dbTransaction.Rollback(); dbTransaction = null; } // Don't bother retrying in this case result_code = ex.Message; success = false; } finally { // In all cases, make sure to close the connection to the DB if (dbContext.Connection.State == ConnectionState.Open) { dbContext.Connection.Close(); } dbContext = null; } return(success); }