C#事务技术

事务技术

.NET目前提供了三种事务机制。

1.方法1:在SQL中使用事务

在存储过程中使用 BEGIN TRANS, COMMIT TRANS, ROLLBACK TRANS 实现。

begin trans
declare @orderDetailsError int,@procuntError int
delete from [order details] where productid=42
select @orderDetailsError =@@error
delete from products where productid=42
select @procuntError=@@error
if(@orderDetailsError =0 and @procuntError=0)
COMMIT TRANS
else
ROLLBACK TRANS

2.方法2:ADO.NET事务

使用ADO.NET 里Transaction对象来实现。

SqlConnection 和OleDbConnection  对象有一个BeginTransaction方法,它可以返回 SqlTransaction 或者OleDbTransaction 对象。而且这个对象有Commit和Rollback方法来管理事务。

SqlConnection sqlConnection = new SqlConnection(
"workstation id=WEIXIAOPING;packet size=4096;user id=sa;
initial catalog=Northwind;persist security info= False");
sqlConnection.Open();
SqlTransaction  myTrans = sqlConnection.BeginTransaction();
SqlCommand sqlInsertCommand = new SqlCommand();
sqlInsertCommand.Connection = sqlConnection
sqlInsertCommand.Transaction=myTrans;
try{
sqlInsertCommand.CommandText="insert into
tbTree(Context,ParentID) values('北京',1)";
sqlInsertCommand.ExecuteNonQuery();
sqlInsertCommand.CommandText="insert into
tbTree(Context,ParentID) values('上海',1)";
sqlInsertCommand.ExecuteNonQuery();
myTrans.Commit();
}catch(Exception ex)
{
myTrans.Rollback();
}
finally
{
sqlConnection.Close();
}

其优点:

·简单性。

·和数据库事务差不多快。

·独立于数据库,不同数据库的专有代码被隐藏了。

其缺点:

·事务不能跨越多个数据库连接。

·事务执行在数据库连接层上,所以需要在事务过程中维护一个数据库连接。

·ADO.NET分布事务也可以跨越多个数据库,但是其中一个SQL SERVER 数据库的话,通过用SQL SERVER连接服务器连接到别的数据库,但是如果是在DB2和Oracle之间就不可以。

3.方法3:COM+事务(分布式事务),此种方式主要用于自动事务控制情况下

.NET Framework依靠MTS/COM+ 服务来支持自动事务。COM+ 使用 Microsoft Distributed Transaction Coordinator (DTC) 作为事务管理器和事务协调器在分布式环境中运行事务。

这样可使 .NET 应用程序运行跨多个资源结合不同操作(例如,将订单插入 SQL Server 数据库,将消息写入 Microsoft 消息队列 (MSMQ) 队列,以及从 Oracle 数据库检索数据)的事务。

COM+事务处理的类必须继承System.EnterpriseServices.ServicedComponent,其实Web Service就是继承System.EnterpriseServices.ServicedComponent的,所以Web Service支持COM+事务。

定义一个COM+事务处理的类:

[Transaction(TransactionOption.Required)]
public class DataAccess:System.EnterpriseServices.ServicedComponent
{ }

ransactionOption枚举类型支持5类COM+值:

·Disabled      忽略当前上下文中的任何事务。

·NotSupported  使用非受控事务在上下文中创建组件。

·Required      如果事务存在则共享事务,并且如有必要则创建新事务。

·RequiresNew   使用新事务创建组件,而与当前上下文的状态无关。

·Supported     如果事务存在,则共享该事务。

一般来说COM+中的组件需要Required 或Supported。当组件用于记录或查账时RequiresNew 很有用,因为组件应该与活动中其他事务处理的提交或回滚隔离开来。

派生类可以重载基类的任意属性,如DataAccess选用Required,派生类仍然可以重载并指定RequiresNew或其他值。

COM+事务有手动处理和自动处理,自动处理就是在所需要自动处理的方法前加上[AutoComplete],根据方法的正常或抛出异常决定提交或回滚。

手动处理就是调用ContextUtil类中EnableCommit,SetComplete,SetAbort方法。

在需要事务跨 MSMQ 和其他可识别事务的资源(例如,SQL Server 数据库)运行的系统中,只能使用 DTC 或 COM+ 事务,除此之外没有其他选择。DTC 协调参与分布式事务的所有资源管理器,也管理与事务相关的操作。

这种做法的缺点是,由于存在 DTC 和 COM 互操作性开销,导致性能降低,并且COM+事务处理的类必须强命名。

3.6.3 使用System.Transaction命名空间

2008-03-17 20:12 罗江华 朱永光 电子工业出版社 我要评论(0) 字号:T | T
一键收藏,随时查看,分享好友!

《.NET Web高级开发》可以使读者通过阅读进一步的了解和掌握.NET,本文介绍了如何使用System.Transaction命名空间。

AD:

3.6.3  使用System.Transaction命名空间

在.NET 2.0中新添加了一个名为System.Transaction的命名空间,其提供了一个“轻量级”的、易于使用的事务框架,通过这个框架可以大大简化事务的操作。

这个框架提供了如下优点:

(1)在简单(不涉及分布式)事务中也可以使用声明式的事务处理方法,而不必使用Com+容器和目录注册。

(2)用户根本不需要考虑是简单事务还是分布式事务。它实现一种所谓自动提升事务机制(Promotable Transaction),会自动根据事务中涉及的对象资源判断使用何种事务管理器。简而言之,对于任何的事务用户只要使用同一种方法进行处理。

下面是一个使用System.Transaction的简单例子:

using(TransactionScope scope = new TransactionScope())
{
/**//* Perform transactional work here */
//No errors - commit transaction
scope.Complete();
}

对于嵌套事务,也可以很好地支持:

using(TransactionScope scope1 = new TransactionScope())
//Default is Required
{
using(TransactionScope scope2 = new
TransactionScope(TransactionScopeOption.Required))
{}
using(TransactionScope scope3 = new
TransactionScope(TransactionScopeOption.RequiresNew))
{}
using(TransactionScope scope4 = new
TransactionScope(TransactionScopeOption.Suppress))
{}
}

下面是一个对多个数据库进行事务管理的例子:

using (TransactionScope transScope = new TransactionScope())
{
using (SqlConnection connection1 = new
SqlConnection(connectString1))
{
// Opening connection1 automatically enlists it in the
// TransactionScope as a lightweight transaction.
connection1.Open();
// Do work in the first connection.
// Assumes conditional logic in place where the second
// connection will only be opened as needed.
using (SqlConnection connection2 = new
SqlConnection(connectString2))
{
// Open the second connection, which enlists the
// second connection and promotes the transaction to
// a full distributed transaction.
connection2.Open();
// Do work in the second connection.
}
}
//  The Complete method commits the transaction.
transScope.Complete();
}


 

原文地址:https://www.cnblogs.com/kingwangzhen/p/2128683.html