您的位置:首页 > 其它

企业级服务 COM+事务

2009-11-17 11:31 169 查看
.NET Framework 依靠 MTS/COM+ 服务来支持自动事务处理。COM+ 使用 Microsoft
Distributed Transaction Coordinator(DTC)作为事务管理器和事务协调器在分布式环境中运行事务。这样可使 .NET 应用程序运行跨多个资源结合不同操作(例如将定单插入 SQL
Server 数据库、将消息写入 Microsoft 消息队列(MSMQ)队列,以及从 Oracle 数据库检
索数据)的事务。
要实现COM+事务处理的类则必须继承System.EnterpriseServices.ServicedComponent, 这
些类需要是公共的,并且需要提供一个公共的默认的构造器。其实 Web Service 就是继承
ServicedComponent,所以 Web Service 也支持 COM+事务。要在类定义之前加属性
[Transaction(TransactionOption.Required)]。类里面的每个方法都会运行在一个事务中。
定义一个COM+事务处理的类: 首先引用:using System.EnterpriseServices;。
然后,继承ServicedComponent。
[Transaction(TransactionOption.Required)]
public class OrderData : ServicedComponent
{
} TransactionOption枚举类型支持5个值: Disabled、 NotSupported、 Required、 RequiresNew
和Supported,如表5-3 所示。 表5-3 TransactionOption枚举类型支持5个值
值 说 明
Disabled 忽略当前上下文中的任何事务
NotSupported 使用非受控事务在上下文中创建组件
Required 如果事务存在则共享事务,并且如有必要则创建新事务
RequiresNew 使用新事务创建组件,而与当前上下文的状态无关
Supported 如果事务存在,则共享该事务 一般来说 COM+中的组件需要 Required 或 Supported。当组件用于记录或查账时
RequiresNew 很有用,因为组件应该与活动中其他事务处理的提交或回滚隔离开来。
派生类可以重载基类的任意属性。如OrderData 选用 Required,派生类仍然可以重载并
指定RequiresNew或其他值。
COM+事务有手动处理和自动处理两种方式,自动处理就是在所需要自动处理的方法前
加上[AutoComplete],根据方法的正常或抛出异常决定提交或回滚。手动处理就是调用
ContextUtil类中的EnableCommit、SetComplete和 SetAbort方法。
实现步骤如下。 实现步骤如下。
1.给程序添加强名
1)创建一对密钥
用来创建密钥的工具是称为sn.exe的共享工具。通常通过命令提示运行它,该工具可执
行各种任务以生成并提取密钥。我们需要用以下方式来运行 sn.exe。
sn –k c:/key.snk 5
其中 key.snk 代表将保存密钥的文件的名称。它的名称可以是任意的,不过习惯上带
有.snk后缀名。
2)签名
这个文件必须在 AssemblyKeyFile 属性中引用,签名通常是在编译时进行的。签名时
用户可利用 C#属性通知编译器应该使用正确的密钥文件对 DLL 进行签名。要做到这一点
用户需要打开工程中的 AssemblyInfo.cs文件并进行修改。
[assembly:AssemblyKeyFile(“..//..//key.snk”)] 2.手动事务处理
创建一个项目用以实现事务处理的业务类ClassTran。
(示例位置:光盘/code/ch05/04/ClassTran/OrderData1)
using System;
using System.Data.SqlClient;
using System.EnterpriseServices; //企业级服务COM+事务
namespace ClassTran
{
[Transaction(TransactionOption.Required)]
public class OrderData1 : ServicedComponent
代码示例: {
//手动事务
public string WorkTran()
{
try
{
ContextUtil.EnableCommit();
Work1();
Work2();
ContextUtil.SetComplete();
return "成功!";
}
catch (Exception ex)
{
ContextUtil.SetAbort();
return "失败!";
}
}
private void Work1()
{
string conString = "data source=127.0.0.1;database=codematic;

user id=sa;password="; SqlConnection myConnection = new SqlConnection(conString);
string strSql = "Insert Into P_Category(CategoryId,Name)
values('1','test1')";
SqlCommand myCommand = new SqlCommand(strSql, myConnection);
myConnection.Open();
int rows = myCommand.ExecuteNonQuery();
myConnection.Close();
} private void Work2()
{
string conString = "data source=127.0.0.1;database=codematic;
user id=sa;password=";
SqlConnection myConnection = new SqlConnection(conString);
string strSql = "Insert Into P_Category(CategoryId,Name)
values('2','test2')";
SqlCommand myCommand = new SqlCommand(strSql, myConnection);
myConnection.Open();
int rows = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}

3.自动事务处理
在方法之前增加属性[AutoComplete(true)],这样如果方法执行时没有异常就默认提交,
如果有异常则这个方法就会回滚。 using System;
using System.Data.SqlClient;
using System.EnterpriseServices;//企业级服务COM+事务
namespace ClassTran
{
[Transaction(TransactionOption.Required)]
public class OrderData2 : ServicedComponent
{
//自动事务
[AutoComplete(true)]
public string WorkTran()
{
string msg = "";
string conString = "data source=127.0.0.1;database=codematic;
user id=sa;password=";
SqlConnection myConnection = new SqlConnection(conString);
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection; try
{
myCommand.CommandText = "update P_Product set Name='电脑2'
where Id=52";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "update P_Product set Name='电脑3'
where Id=53";
myCommand.ExecuteNonQuery();
msg ="成功!";
}
catch (Exception ex)
{
msg = "失败:"+ex.Message;
}
finally
{
myConnection.Close();
}
return msg;
}
}
}
4.事务方法调用 protected void Button1_Click(object sender, EventArgs e)
{
ClassTran.OrderData1 od1 = new ClassTran.OrderData1();
od1.WorkTran();
}
protected void Button2_Click(object sender, EventArgs e)
{
ClassTran.OrderData2 od2 = new ClassTran.OrderData2();
od2.WorkTran();
}
在需要事务跨 MSMQ 和其他可识别事务的资源(例如 SQL Server 数据库)运行的系
,只能使用 DTC 或 COM+ 事务,除此之外没有其他选择。DTC 协调参与分布式事
所有资源管理器,也管理与事务相关的操作。
企业级服务COM+事务的前提及优缺点如下。
前提:
l 需要强名字。
l 使用事务的对象需要继承ServicedComponent。
优势: l 执行分布式事务,多个对象可以轻松地运行在同一个事务处理中,事务处理还可以
自动登记。
l 获得COM+服务,诸如对象构建和对象池等。
缺点:
l 由于存在 DTC 和 COM 互操作性开销,导致性能降低。
l COM+ 1.0要求每个事务的隔离级别都设置为 Serializable。
l 使用 Enterprise Services 的事务总是线程安全的, 也就是说你无法让多个线程参与
到同一个事务中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: