使用的IDbConnection / IDbTransaction安全使用? [英] Using for IDbConnection/IDbTransaction safe to use?

查看:2268
本文介绍了使用的IDbConnection / IDbTransaction安全使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我的假设似乎听起来主观的,经过一番研究,我发现它的情况并不少见找到谁赞成一个虚拟的的try / catch开发商而不是使用< A HREF =http://msdn.microsoft.com/en-us/library/htd05whh.aspx>使用声明为的IDbConnection / IDbTransaction 处理(关闭/提交/回滚)。



这适用于即使是最有经验的开发人员和一些新的一些真实的。我故意不去引用任何在计算器上或论坛链接作为例子的问题,所以人们不要得罪。从我发现了什么使用语句的使用安全(没有双关语意)。



这有什么错用它?考虑下面的代码:



<预类=朗动prettyprint-覆盖> 公用Sub提交()
昏暗的CN作为的IDbConnection = {} CREATE_CONNECTION
昏暗TRAN作为IDbTransaction =什么

cn.Open()
尝试
TRAN = cn.BeginTransaction
'$这里运行一些查询b $ b tran.Commit()
抓住EX作为例外
如果不移植是没有那么tran.Rollback()

最后
cn.Close()
端尝试
端功能

假设 {CREATE_CONNECTION } 是根据所有可能的最佳实践为占位符,创建一个连接,这取决于数据库供应商,书面和不需要更多。改进



有为什么上面的代码不能被重写作为这种原因:



<预先类=朗VB prettyprint-覆盖> 使用CN作为的IDbConnection = {} CREATE_CONNECTION
cn.Open()
使用TRAN作为IDbTransaction = cn.BeginTransaction
'在这里运行一些查询
tran.Commit()
端使用
端使用



显然,版本号2更直观,它在做什么。但是,也许我错过这里一些重要的事情?事情是这样的数据访问库的供应商特定的实现,那不叫器transaction.commit 和/或的Connection.close 的Dispose 内部?正在退役不久的将来这种方式,或者不作为的够清楚的现代编程模式/最佳做法?单声道/移动应用dev的缺乏调试支持使用关键字工具?



我要寻找任何一种答案来支持或拒绝的地步。最好是一个与引号原始文件,像不要使用IDbTransaction时... 使用。 。链接的博客或个人的经验都还好过


解决方案

我和你完全是在连接上;这应该是使用,也没有必要在明确的关闭()。本次交易是有点麻烦;显示的代码是目前肯定是矫枉过正,但它不完全定义的的Dispose()应该做回滚。其实,这的东西往往在每一个我看过的实施情况发生,但它稍微伤脑筋,即使 DbTransaction (其中大部分供应商使用)实际上并没有做到这一点。对比的TransactionScope 它明确定义了一个的Dispose()没有犯被视为一个回滚。正因为如此,我的往往的使用(原谅C#):使用(VAR康恩

  = GetOpenConnection())使用
(VAR TRAN = conn.BeginTransaction()){
尝试{
// TODO:做的工作
tran.Commit();
} {赶上
tran.Rollback();
抛出;
}
}



这是在复杂性方面的某处两者之间。它不与 - 检查乱搞,至少。


While my assumption may seem to sound subjective, after some research, I found that it's not uncommon to find developers who favour a dummy Try/Catch instead of using the Using statement for IDbConnection/IDbTransaction processing (Close/Commit/Rollback).

This holds true for even some of the most seasoned developers and some new ones. I am intentionally not going to reference any of the question on StackOverflow or forum links as an example, so people don't get offended. From what I found, Using statement is safe to use (no pun intended).

Is there anything wrong with it? Consider the following code:

Public Sub Commit()
  Dim cn As IDbConnection = {CREATE_CONNECTION}
  Dim tran As IDbTransaction = Nothing

  cn.Open()
  Try
    tran = cn.BeginTransaction
    'run some queries here
    tran.Commit()
  Catch ex As Exception
    If Not tran Is Nothing Then tran.Rollback()
    Throw
  Finally
    cn.Close()
  End Try
End Function

Assume {CREATE_CONNECTION} is place holder for a Sub that creates a connection, depending on the database vendor, written according to all possible best practices and does not need more improvement.

Is there a reason why the above code cannot be rewritten as such:

Using cn As IDbConnection = {CREATE_CONNECTION}
  cn.Open()
  Using tran As IDbTransaction = cn.BeginTransaction
    'run some queries here
    tran.Commit()
  End Using
End Using

?

Clearly, version #2 is more intuitive to what it's doing. But perhaps I am missing something important here? Things like vendor-specific implementations of data access libraries, that do not call Transaction.Commit and/or Connection.Close on Dispose internally? Is this approach being decommissioned in the near future, or not regarded as clear enough in modern programming pattern/best practices? Mono/mobile apps dev tools lacking debug support for Using keyword?

I am looking for any kind of answer to support or deny the point. Preferably the one with quotes to original documentation, something like Do not use Using with IDbTransaction when .... Links to blogs or personal experience are okay too.

解决方案

I'm entirely with you on the connection; that should be using, and there is no need for the explicit Close(). The transaction is a little bit trickier; the code shown is certainly overkill at the moment, but it is not entirely defined that Dispose() should do a rollback. Actually, that is what tends to happen in every implementation I've looked at, but it is slightly vexing that even DbTransaction (which most providers use) doesn't actually do this. Contrast to TransactionScope where it is explicitly defined that a Dispose() without a commit counts as a rollback. Because of that, I tend to use (excuse the C#):

using(var conn = GetOpenConnection())
using(var tran = conn.BeginTransaction()) {
    try {
        // TODO: do work
        tran.Commit();
    } catch {
        tran.Rollback();
        throw;
    }
}

which is somewhere between the two in terms of complexity. It isn't messing around with null-checks, at least.

这篇关于使用的IDbConnection / IDbTransaction安全使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆