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

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

问题描述

虽然我的假设似乎是主观的,在一些研究之后,我发现找到喜欢虚拟 Try / Catch 的开发者而不是使用< a href =http://msdn.microsoft.com/en-us/library/htd05whh.aspx =noreferrer> 使用语句 IDbConnection / IDbTransaction 处理(关闭/提交/回滚)。

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).

这对于一些最经验丰富的开发人员和一些新的开发人员也是如此。我故意不参考任何关于StackOverflow或论坛链接的问题作为例子,所以人们不会被冒犯。从我发现的使用语句是安全使用(不需要双关)。

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

假设 {CREATE_CONNECTION } 是创建连接的 Sub 的占位符,取决于数据库供应商,根据所有可能的最佳做法编写,不需要更多

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

很显然,版本#2对它的功能更直观。但也许我在这里缺少一些重要的东西?例如数据访问库的供应商特定的实现,不调用 Transaction.Commit 和/或 Connection.Close 在内部处理?这种方法是否在不久的将来退役,或在现代编程模式/最佳实践中被视为不够清楚?单元/移动应用开发工具缺少对使用关键字的调试支持?

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?

我正在寻找任何类型的答案支持或否认点。最好是原始文档的引号,如不要使用使用IDbTransaction时...

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.

推荐答案

我完全与你在连接上;应该使用,并且不需要显式 Close()。交易有点棘手;显示的代码肯定是过时的,但它并没有完全定义 Dispose()应该做一个回滚。实际上,在我看过的每个实现中往往会发生,但是有点令人不安的是,即使 DbTransaction (大多数提供者使用)实际上没有这样做。与对比 c> TransactionScope ,其中明确定义不带提交的 Dispose()因此,我倾向于使用(原因是C#):

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

在复杂性方面介于两者之间。它不是搞乱 null -checks,至少。

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天全站免登陆