Beispiel #1
0
        public MethodResponse <ErrorCode> UpdateUser(User[] users)
        {
            MethodResponse <ErrorCode> returnCodes = new MethodResponse <ErrorCode>();

            foreach (var user in users)
            {
                try
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        UserPersistence.UpdateUser(user);
                        transactionScope.Complete();
                    }
                }
                catch (Exception exception)
                {
                    EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace);
                    returnCodes.AddError(exception, 0);
                }
            }
            returnCodes.Result = (returnCodes.HasErrors()) ? ErrorCode.NoJoy : ErrorCode.Success;
            return(returnCodes);
        }
        /// <summary>
        /// Delete a set of records from the data model. Each record is deleted in order. The method returns when all the records are successfully
        /// deleted, or when an error occurs - whichever comes first.
        /// </summary>
        /// <typeparam name="RecordType">The specific type of the record.</typeparam>
        /// <typeparam name="PersistenceType">The type that handles deleting the record type.</typeparam>
        /// <param name="source">The record set to delete.</param>
        /// <returns>The errors that may have occurred attempted to delete the set.</returns>
        internal static MethodResponse <ErrorCode> Delete <RecordType, PersistenceType>(this IEnumerable <RecordType> source)
            where RecordType : BaseRecord
            where PersistenceType : DataModelPersistence <RecordType>, new()
        {
            MethodResponse <ErrorCode> returnCodes = new MethodResponse <ErrorCode>()
            {
                Result = ErrorCode.Success
            };
            PersistenceType datamodelPersistance = new PersistenceType();
            Int32           bulkIndex            = 0;

            foreach (RecordType record in source)
            {
                try
                {
                    Boolean finished = false;

                    for (int deadlockRetry = 0; !finished && deadlockRetry < deadlockRetiesMax; deadlockRetry++)
                    {
                        try
                        {
                            using (TransactionScope transactionScope = new TransactionScope())
                            {
                                // This provides a context for any transactions.
                                DataModelTransaction dataModelTransaction = DataModelTransaction.Current;

                                ErrorCode result = datamodelPersistance.Delete(record);

                                if (result == ErrorCode.Success)
                                {
                                    transactionScope.Complete();
                                }
                                else if (result != ErrorCode.RecordNotFound)
                                {
                                    returnCodes.AddError(new ErrorInfo()
                                    {
                                        ErrorCode = result, BulkIndex = bulkIndex
                                    });
                                }

                                // Break out of the deadlock loop if the transaction completed expectedly (either failure or success).
                                finished = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            if (FluidTrade.Core.Utilities.SqlErrorHelper.IsDeadlockException(ex))
                            {
                                if (deadlockRetry == deadlockRetiesMax - 1)
                                {
                                    throw;
                                }
                                if (EventLog.IsLoggingEnabledFor(EventLog.ErrorLogLevel.Verbose))
                                {
                                    EventLog.Warning("Deadlock exception\r\n{0}: {1}", ex.Message, ex.StackTrace);
                                }
                                System.Threading.Thread.Sleep(2000 * deadlockRetry + 1);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }
                catch (FaultException <RecordNotFoundFault> exception)
                {
                    EventLog.Information("RecordNotFound ignored by delete operation: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
                }
                catch (Exception exception)
                {
                    EventLog.Error("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
                    returnCodes.AddError(exception, bulkIndex);
                    break;
                }

                if (returnCodes.HasErrors())
                {
                    break;
                }

                bulkIndex += 1;
            }

            if (returnCodes.Errors.Length > 0)
            {
                returnCodes.Result = ErrorCode.NoJoy;
            }

            return(returnCodes);
        }