在.NET交易 [英] Transactions in .net

查看:154
本文介绍了在.NET交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是做交易的C#.NET 2.0的最佳实践。什么是应该使用的类?什么是陷阱看出来的等等。所有的提交和回退的东西。我刚开始一个项目,我可能需要做一些事务,同时将数据插入到数据库。任何响应或链接,即使有关交易基本的东西是值得欢迎的。

What are the best practices to do transactions in C# .Net 2.0. What are the classes that should be used? What are the pitfalls to look out for etc. All that commit and rollback stuff. I'm just starting a project where I might need to do some transactions while inserting data into the DB. Any responses or links for even basic stuff about transactions are welcome.

推荐答案

有2种主要的交易;连接的事务和环境事务。一个连接的事务(如的SqlTransaction)直接连接到数据库连接(如SqlConnection的),这意味着你必须保持周围路过的连接 - 确定在某些情况下,但不允许创建/使用/释放用法,并且不允许跨分贝的工作。一个例子(格式为空格):

There are 2 main kinds of transactions; connection transactions and ambient transactions. A connection transaction (such as SqlTransaction) is tied directly to the db connection (such as SqlConnection), which means that you have to keep passing the connection around - OK in some cases, but doesn't allow "create/use/release" usage, and doesn't allow cross-db work. An example (formatted for space):

using (IDbTransaction tran = conn.BeginTransaction()) {
    try {
        // your code
        tran.Commit();
    }  catch {
        tran.Rollback();
        throw;
    }
}

不是太乱,但仅限于我们的连接conn将。如果我们想调出不同的方法,我们现在需要通过conn将左右。

Not too messy, but limited to our connection "conn". If we want to call out to different methods, we now need to pass "conn" around.

另一种选择是一个环境事务;新的.NET 2.0中,<一个href="http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx">TransactionScope对象(System.Transactions.dll)允许使用一定范围的操作(适合的供应商将自动登记在环境事务)。这使得它很容易加装适合到现有(非交易)code,并与多个供应商(虽然DTC将涉足如果你跟不止一个)。

The alternative is an ambient transaction; new in .NET 2.0, the TransactionScope object (System.Transactions.dll) allows use over a range of operations (suitable providers will automatically enlist in the ambient transaction). This makes it easy to retro-fit into existing (non-transactional) code, and to talk to multiple providers (although DTC will get involved if you talk to more than one).

例如:

using(TransactionScope tran = new TransactionScope()) {
    CallAMethodThatDoesSomeWork();
    CallAMethodThatDoesSomeMoreWork();
    tran.Complete();
}

请注意,这里说的两种方法可以处理自己的连接(开/使用/关闭/处置),但他们会悄悄地成为环境事务的一部分,没有我们不必传递任何东西。

Note here that the two methods can handle their own connections (open/use/close/dispose), yet they will silently become part of the ambient transaction without us having to pass anything in.

如果您的code错误,Dispose()方法将不完整的()被调用,所以它会被回滚。预期嵌套等支持,虽然你不能回滚内部事务尚未完成外部事务。如果任何人是不快乐的,该交易被中止

If your code errors, Dispose() will be called without Complete(), so it will be rolled back. The expected nesting etc is supported, although you can't roll-back an inner transaction yet complete the outer transaction: if anybody is unhappy, the transaction is aborted.

的TransactionScope的另一优点是,它不依赖于只是数据库;任何事务感知提供商可以使用它。 WCF,例如。 ( - 虽然我从来没有用过这种方法我自己也许比留念更容易,即.NET类与回退功能)

The other advantage of TransactionScope is that it isn't tied just to databases; any transaction-aware provider can use it. WCF, for example. Or there are even some TransactionScope-compatible object models around (i.e. .NET classes with rollback capability - perhaps easier than a memento, although I've never used this approach myself).

所有的一切,一个非常,非常有用的对象。

All in all, a very, very useful object.

一些注意事项:

  • 在SQL Server 2000中,一个TransactionScope将立即转至DTC;这是固定在SQL Server 2005及以上时,可使用LTM(少得多的开销),直到你跟2源等,当它被提升到DTC。
  • 有一个<一个href="http://stackoverflow.com/questions/195420/transactionscope-bug-in-net-more-information#195427">glitch这意味着你可能需要调整你的连接字符串
  • On SQL Server 2000, a TransactionScope will go to DTC immediately; this is fixed in SQL Server 2005 and above, it can use the LTM (much less overhead) until you talk to 2 sources etc, when it is elevated to DTC.
  • There is a glitch that means you might need to tweak your connection string

这篇关于在.NET交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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