Ejemplo n.º 1
0
        private static bool IsDeadlockException(System.Data.Common.DbException e)
        {
            // Avoid [SqlException (0x80131904): Transaction (Process ID ??) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
            // CAUTION: Using e.ErrorCode and testing for HRESULT 0x80131904 will not work! you should use e.Number not e.ErrorCode
            var sqlEx = e as System.Data.SqlClient.SqlException;

            if (sqlEx == null)
            {
                return(false);
            }
            var sqlExNumber    = sqlEx.Number;
            var sqlExErrorCode = sqlEx.ErrorCode;
            var isDeadLock     = sqlExNumber == 1205;
            // assert
            var messageParts = new[]
            {
                "was deadlocked on lock",
                "resources with another process and has been chosen as the deadlock victim. rerun the transaction"
            };
            var currentMessage    = e.Message.ToLower();
            var isMessageDeadlock = !messageParts.Where(msgPart => !currentMessage.Contains(msgPart)).Any();

            if (sqlEx != null && isMessageDeadlock != isDeadLock)
            {
                throw new Exception(String.Concat("Incorrect deadlock analysis",
                                                  ". Number: ", sqlExNumber,
                                                  ". ErrorCode: ", sqlExErrorCode,
                                                  ". Errors.Count: ", sqlEx.Errors.Count,
                                                  ". Original message: ", e.Message), e);
            }
            return(isDeadLock);
        }
Ejemplo n.º 2
0
        public static ExceptionMessageBoxDialogResult ShowExceptionMessageBox(IWin32Window owner, Exception ex)
        {
            //try
            //{

            ExceptionMessageBoxDialogResult exceptionMessageBoxDialogResult;

            CustomException customEx = ex as CustomException;
            if (customEx != null)
            {
                CustomExceptionMessageBox customExceptionMessageBox = new CustomExceptionMessageBox(customEx);
                exceptionMessageBoxDialogResult = (ExceptionMessageBoxDialogResult)customExceptionMessageBox.ShowDialog(owner);
            }
            else
            {
                System.Data.Common.DbException dbException = ex as System.Data.Common.DbException;
                if (dbException != null && dbException.ErrorCode == -2146232060)
                    ex = new Exception("Không thể lưu do trùng dữ liệu. Vui lòng kiểm tra lại.");


                ExceptionMessageBox exceptionMessageBox = new ExceptionMessageBox(ex); //exceptionMessageBox.ShowToolBar = false;
                exceptionMessageBoxDialogResult = (ExceptionMessageBoxDialogResult)exceptionMessageBox.Show(owner);
            }
            return exceptionMessageBoxDialogResult;
            //}
            //catch {}// Environment.Exit(0); return ExceptionMessageBoxDialogResult.None; }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Writes error to error log file.
        /// </summary>
        /// <param name="virtualServer">Virtual server name.</param>
        /// <param name="x"></param>
        /// <param name="stackTrace"></param>
        public static void DumpError(string virtualServer, Exception x, StackTrace stackTrace)
        {
            try
            {
                string source = stackTrace.GetFrame(0).GetMethod().DeclaringType.FullName + "." + stackTrace.GetFrame(0).GetMethod().Name + "()";

                string errorText = x.Message + "\r\n";
                errorText += "//------------- function: " + source + "  " + DateTime.Now.ToString() + "------------//\r\n";
                errorText += new StackTrace().ToString() + "\r\n";
                errorText += "//--- Excetption info: -------------------------------------------------\r\n";
                errorText += x.ToString() + "\r\n";

                if (x is System.Data.Common.DbException)
                {
                    System.Data.Common.DbException sX = (System.Data.Common.DbException)x;
                    errorText += "\r\n\r\nSql errors:\r\n";

                    errorText += "\n";
                    errorText += "Procedure: '" + "UnknonwnProcedure" + "'  line: " + "666" + "  error: " + sX.ErrorCode.ToString() + "\r\n";
                    errorText += "Message: " + sX.Message + "\r\n";

                    // foreach (System.Data.SqlClient.SqlError sErr in sX.Errors){
                    //	 errorText += "\n";
                    //	 errorText += "Procedure: '" + sErr.Procedure + "'  line: " + sErr.LineNumber.ToString() + "  error: " + sErr.Number.ToString() + "\r\n";
                    //	 errorText += "Message: " + sErr.Message + "\r\n";
                    // }
                }

                if (x.InnerException != null)
                {
                    errorText += "\r\n\r\n//------------- Innner Exception ----------\r\n" + x.Message + "\r\n";
                    errorText += x.InnerException.ToString();
                }

                DumpError(virtualServer, errorText);
            }
            catch
            { }
        }