pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

SqlTransaction 是否需要調(diào)用 Dispose?

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

問題描述

我需要在 SqlTransaction 的 finally 塊中調(diào)用 dispose 嗎?假設(shè)開發(fā)者沒有在任何地方使用 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?

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

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

但通常垃圾收集器處理未引用的對象(這并不意味著 GC 調(diào)用了 dispose,這 不正確),因此您只需要非托管資源.但是因?yàn)槲乙膊幌朐谒衅渌兞可险{(diào)用 dispose 或使用 using-statement 無處不在,研究類的 Dispose 方法的實(shí)際實(shí)現(xiàn)總是值得的.

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);
}
        

在不了解這里發(fā)生的所有(或任何事情)的情況下,我可以說這不僅僅是一個簡單的 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.

但是因?yàn)?SqlConnection.BeginTransaction 創(chuàng)建了事務(wù),所以 反映這一點(diǎn):

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 還將在創(chuàng)建事務(wù)時保持連接處于活動狀態(tài).它也不持有對交易的引用,因?yàn)樗环祷厮?因此,即使連接已被處理,它也可能不會被處理.處理交易的另一個論據(jù).

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.

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

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

相關(guān)文檔推薦

SQL Server 2005 Transaction Level and Stored Procedures(SQL Server 2005 事務(wù)級和存儲過程)
Yield return from a try/catch block(try/catch 塊的收益回報)
Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務(wù)的 SqlCommand 時,我應(yīng)該調(diào)用 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 和實(shí)體框架一起使用?(獲取多個同時連接...目前不
what does a using statement without variable do when disposing?(處理時不帶變量的 using 語句有什么作用?)
主站蜘蛛池模板: 乐之康护 - 专业护工服务平台,提供医院陪护-居家照护-居家康复 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 西门子代理商_西门子变频器总代理-翰粤百科| 100国际学校招生 - 专业国际学校择校升学规划 | 大行程影像测量仪-探针型影像测量仪-增强型影像测量仪|首丰百科 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | Win10系统下载_32位/64位系统/专业版/纯净版下载 | 棉服定制/厂家/公司_棉袄订做/价格/费用-北京圣达信棉服 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 挤塑板-XPS挤塑板-挤塑板设备厂家[襄阳欧格] | 选宝石船-陆地水上开采「精选」色选机械设备-青州冠诚重工机械有限公司 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 智慧水务|智慧供排水利信息化|水厂软硬件系统-上海敢创 | 成都软件开发_OA|ERP|CRM|管理系统定制开发_成都码邻蜀科技 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 挨踢网-大家的导航! | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 强效碱性清洗剂-实验室中性清洗剂-食品级高纯氮气发生器-上海润榕科学器材有限公司 | 水冷式工业冷水机组_风冷式工业冷水机_水冷螺杆冷冻机组-深圳市普威机械设备有限公司 | TPE_TPE热塑性弹性体_TPE原料价格_TPE材料厂家-惠州市中塑王塑胶制品公司- 中塑王塑胶制品有限公司 | 新型锤式破碎机_新型圆锥式_新型颚式破碎机_反击式打沙机_锤式制砂机_青州建源机械 | 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | QQ房产导航-免费收录优秀房地产网站_房地产信息网 | 青岛美佳乐清洁工程有限公司|青岛油烟管道清洗|酒店|企事业单位|学校工厂厨房|青岛油烟管道清洗 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 物流之家新闻网-最新物流新闻|物流资讯|物流政策|物流网-匡匡奈斯物流科技 | 同步带轮_同步带_同步轮_iHF合发齿轮厂家-深圳市合发齿轮机械有限公司 | uv机-uv灯-uvled光固化机-生产厂家-蓝盾机电 | 硫化罐-电加热蒸汽硫化罐生产厂家-山东鑫泰鑫智能装备有限公司 | 特种电缆厂家-硅橡胶耐高温电缆-耐低温补偿导线-安徽万邦特种电缆有限公司 | 安驭邦官网-双向万能直角铣头,加工中心侧铣头,角度头[厂家直销] 闸阀_截止阀_止回阀「生产厂家」-上海卡比阀门有限公司 | 国产液相色谱仪-超高效液相色谱仪厂家-上海伍丰科学仪器有限公司 | 营养师网,营养师考试时间,报名入口—网站首页 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 临沂招聘网_人才市场_招聘信息_求职招聘找工作请认准【马头商标】 | 步进驱动器「一体化」步进电机品牌厂家-一体式步进驱动 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 山西3A认证|太原AAA信用认证|投标AAA信用证书-山西AAA企业信用评级网 | 【德信自动化】点胶机_全自动点胶机_自动点胶机厂家_塑料热压机_自动螺丝机-深圳市德信自动化设备有限公司 | 并网柜,汇流箱,电控设备,中高低压开关柜,电气电力成套设备,PLC控制设备订制厂家,江苏昌伟业新能源科技有限公司 |