private static void DeleteMatch(MatchRow matchRow) { for (int deadlockRetry = 0; deadlockRetry < deadlockRetiesMax; deadlockRetry++) { try { using (TransactionScope transactionScope = new TransactionScope()) { // This provides a context for any transactions. DataModelTransaction dataModelTransaction = DataModelTransaction.Current; long rowVersion; Guid matchId; matchRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout); try { if (matchRow.RowState == System.Data.DataRowState.Detached || matchRow.RowState == System.Data.DataRowState.Deleted) { continue; } rowVersion = matchRow.RowVersion; matchId = matchRow.MatchId; } finally { matchRow.ReleaseReaderLock(dataModelTransaction.TransactionId); } DataModel dataModel = new DataModel(); dataModel.DestroyMatch(new object[] { matchId }, rowVersion); transactionScope.Complete(); return; } //end using } catch (System.Data.SqlClient.SqlException sqlEx) { if (FluidTrade.Core.Utilities.SqlErrorHelper.IsDeadlockException(sqlEx)) { if (deadlockRetry == deadlockRetiesMax - 1) { throw; } FluidTrade.Core.EventLog.Warning("Deadlock exception\r\n{0}: {1}\r\n{2}", sqlEx.Message, sqlEx.ToString(), sqlEx.StackTrace); } else { throw; } } } //end deadlock retry }
/// <summary> /// Delete a match /// </summary> /// <returns>True for sucess</returns> public override ErrorCode Delete(Records.Match record) { if (record.RowId == null || DataModel.Match.MatchKey.Find(record.RowId) == null) { return(ErrorCode.RecordNotFound); } DataModel dataModel = new DataModel(); MatchRow matchRow = DataModel.Match.MatchKey.Find(record.RowId); DataModelTransaction dataModelTransaction = DataModelTransaction.Current; matchRow.AcquireReaderLock(dataModelTransaction); if (!TradingSupport.HasAccess(dataModelTransaction, matchRow.BlotterId, AccessRight.FullControl)) { return(ErrorCode.AccessDenied); } MatchRow contraMatchRow = DataModel.Match.MatchKey.Find(matchRow.ContraMatchId); matchRow.ReleaseReaderLock(dataModelTransaction.TransactionId); dataModel.DestroyMatch( new object[] { record.RowId }, record.RowVersion); matchRow.ReleaseWriterLock(dataModelTransaction.TransactionId); // Delete the contra second, in case the record.RowVersion is off. if (contraMatchRow != null) { contraMatchRow.AcquireWriterLock(dataModelTransaction); dataModel.DestroyMatch( new object[] { contraMatchRow.MatchId }, contraMatchRow.RowVersion); contraMatchRow.ReleaseWriterLock(dataModelTransaction.TransactionId); } return(ErrorCode.Success); }