久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SqlTransaction 是否需要調用 Dispose?

Does SqlTransaction need to have Dispose called?(SqlTransaction 是否需要調用 Dispose?)
本文介紹了SqlTransaction 是否需要調用 Dispose?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要在 SqlTransaction 的 finally 塊中調用 dispose 嗎?假設開發者沒有在任何地方使用 USING,只是嘗試/捕獲.

Do I need to call dispose in the finally block for SqlTransaction? Pretend the developer didnt use USING anywhere, and just try/catch.

SqlTransaction sqlTrans = con.BeginTransaction();

try
{
     //Do Work
sqlTrans.Commit()
}
catch (Exception ex)
        {

           sqlTrans.Rollback();
        }

 finally
        {
            sqlTrans.Dispose();
            con.Dispose();
        }

推薦答案

我是否需要使用try-finallyusing-statement 來處理SqlTransaction?

Do I need to use try-finally or the using-statement to dispose the SqlTransaction?

擁有它并沒有什么壞處.對于實現 IDisposable 的每個類都是如此,否則它會不實現這個接口.

It does not hurt to have it. This is true for every class implementing IDisposable, otherwise it would not implement this interface.

但通常垃圾收集器處理未引用的對象(這并不意味著 GC 調用了 dispose,這 不正確),因此您只需要非托管資源.但是因為我也不想在所有其他變量上調用 dispose 或使用 using-statement 無處不在,研究類的 Dispose 方法的實際實現總是值得的.

But normally the garbage collector deals with unreferenced objects(it doesn't mean that the GC calls dispose, which isn't true), so you need it only for unmanaged resources. But because i also don't want to call dispose on every other variable or use the using-statement everywhere, it it's always worth to look into the actual implementation of the class' Dispose method.

SqlTransaction.Dispose:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        SNIHandle target = null;
        RuntimeHelpers.PrepareConstrainedRegions();
        try
        {
            target = SqlInternalConnection.GetBestEffortCleanupTarget(this._connection);
            if (!this.IsZombied && !this.IsYukonPartialZombie)
            {
                this._internalTransaction.Dispose();
            }
        }
        catch (OutOfMemoryException e)
        {
            this._connection.Abort(e);
            throw;
        }
        catch (StackOverflowException e2)
        {
            this._connection.Abort(e2);
            throw;
        }
        catch (ThreadAbortException e3)
        {
            this._connection.Abort(e3);
            SqlInternalConnection.BestEffortCleanup(target);
            throw;
        }
    }
    base.Dispose(disposing);
}
        

在不了解這里發生的所有(或任何事情)的情況下,我可以說這不僅僅是一個簡單的 base.Dispose(disposing).因此,確保 SqlTransaction 被釋放可能是一個好主意.

Without understanding all(or anything) what is happening here i can say that this is more than a simple base.Dispose(disposing). So it might be a good idea to ensure that a SqlTransaction gets disposed.

但是因為 SqlConnection.BeginTransaction 創建了事務,所以 反映這一點:

But because SqlConnection.BeginTransaction creates the transaction it could also be a good idea to reflect this also:

public SqlTransaction BeginTransaction(IsolationLevel iso, string transactionName)
{
    SqlStatistics statistics = null;
    string a = ADP.IsEmpty(transactionName) ? "None" : transactionName;
    IntPtr intPtr;
    Bid.ScopeEnter(out intPtr, "<sc.SqlConnection.BeginTransaction|API> %d#, iso=%d{ds.IsolationLevel}, transactionName='%ls'
", this.ObjectID, (int)iso, a);
    SqlTransaction result;
    try
    {
        statistics = SqlStatistics.StartTimer(this.Statistics);
        SqlTransaction sqlTransaction = this.GetOpenConnection().BeginSqlTransaction(iso, transactionName);
        GC.KeepAlive(this);
        result = sqlTransaction;
    }
    finally
    {
        Bid.ScopeLeave(ref intPtr);
        SqlStatistics.StopTimer(statistics);
    }
    return result;
}

如你所見.GC 還將在創建事務時保持連接處于活動狀態.它也不持有對交易的引用,因為它只返回它.因此,即使連接已被處理,它也可能不會被處理.處理交易的另一個論據.

As you can see. The GC will also keep the Connection alive when a Transaction is created. It also doesn't hold a reference to the transaction since it only returns it. Hence it might not be disposed even when the connection is already disposed. Another argument to dispose the transaction.

您還可以查看TransactionScope 類 比 BeginTransaction 更安全.查看這個問題了解更多信息.

You might also have a look at the TransactionScope class which is more fail-safe than BeginTransaction. Have a look at this question for more informations.

這篇關于SqlTransaction 是否需要調用 Dispose?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

SQL Server 2005 Transaction Level and Stored Procedures(SQL Server 2005 事務級和存儲過程)
Yield return from a try/catch block(try/catch 塊的收益回報)
Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務的 SqlCommand 時,我應該調用 Parameters.Clear 嗎?)
Reason for System.Transactions.TransactionInDoubtException(System.Transactions.TransactionInDoubtException 的原因)
How do I use TransactionScope with MySql and Entity Framework? (getting Multiple simultaneous connections...are not currently supported error)(如何將 TransactionScope 與 MySql 和實體框架一起使用?(獲取多個同時連接...目前不
what does a using statement without variable do when disposing?(處理時不帶變量的 using 語句有什么作用?)
主站蜘蛛池模板: 99re6在线视频精品免费 | 精品欧美一区二区在线观看视频 | 国产成人午夜高潮毛片 | 欧美一区二区三区高清视频 | 欧美国产亚洲一区二区 | 成年人黄色一级毛片 | 亚洲欧美精品国产一级在线 | 国产精品久久久久无码av | 久久精品 | 日韩精品在线视频免费观看 | 青春草国产 | 日韩欧美三级 | 国产欧美日韩综合精品一区二区 | 91免费在线| 亚洲大片 | 国产精品国产成人国产三级 | 一区二区三区精品视频 | 91精品一区二区三区久久久久久 | 成人av在线播放 | 国产精品69毛片高清亚洲 | 一区二区在线看 | 综合网中文字幕 | 久久久99精品免费观看 | 亚洲狠狠丁香婷婷综合久久久 | 欧美日韩在线精品 | av毛片| 粉嫩一区二区三区国产精品 | 97免费在线观看视频 | 日韩av视屏| 欧美寡妇偷汉性猛交 | 欧美日韩精品一区二区三区视频 | 一区二区三区精品视频 | 国产在线二区 | 6996成人影院网在线播放 | 草久久| 日韩中文字幕在线观看视频 | 91日日 | 久久国产精品免费一区二区三区 | 国产中文视频 | www日本在线 | 亚洲九九色 |