SqlTransaction has completed

前端 未结 9 1489
后悔当初
后悔当初 2020-12-03 10:05

I have an application which potentially does thousands of inserts to a SQL Server 2005 database. If an insert fails for any reason (foreign key constraint, field length, etc

9条回答
  •  囚心锁ツ
    2020-12-03 11:01

    According to this post: http://blogs.msdn.com/b/dataaccesstechnologies/archive/2010/08/24/zombie-check-on-transaction-error-this-sqltransaction-has-completed-it-is-no-longer-usable.aspx

    A temporary solution could be to try catching the rollback or commit operation. So this code will be enough for stopping the bug to be throwing:

        public static void TryRollback(this System.Data.IDbTransaction t)
        {
            try
            {
                t.Rollback();
            }
            catch (Exception ex)
            {
                // log error in my case
            }
        }
    
        public static void TryCommit(this System.Data.IDbTransaction t)
        {
            try
            {
                t.Commit();
            }
            catch (Exception ex)
            {
                // log error in my case
            }
        }
    

    Check this example from msdn website: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.aspx

提交回复
热议问题