.NET开发中的事务处理大比拼 之 企业级服务COM+事务 (转)
转自李天平:
http://www.cnblogs.com/ltp/archive/2009/06/17/1505318.html
COM+事务有手动处理和自动处理两种方式,自动处理就是在所需要自动处理的方法前加上[AutoComplete],根据方法的正常或抛出异常决定提交或回滚。手动处理就是调用ContextUtil类中的EnableCommit、SetComplete和SetAbort方法。
实现步骤如下。
1.给程序添加强名
1)创建一对密钥
用来创建密钥的工具是称为sn.exe的共享工具。通常通过命令提示运行它,该工具可执行各种任务以生成并提取密钥。我们需要用以下方式来运行sn.exe。 sn –k c:\key.snk
其中key.snk
代表将保存密钥的文件的名称。它的名称可以是任意的,不过习惯上带有.snk后缀名。
2)签名
这个文件必须在AssemblyKeyFile属性中引用,签名通常是在编译时进行的。签名时,用户可利用C#属性通知编译器应该使用正确的密钥文件对DLL进行签名。要做到这一点用户需要打开工程中的AssemblyInfo.cs文件并进行修改。
[assembly:AssemblyKeyFile(“..\\..\\key.snk”)]
注 意 | key.snk文件和项目文件在同一个文件夹内。 |
2.手动事务处理
创建一个项目用以实现事务处理的业务类ClassTran。
代码示例: |
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;
userid=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;
userid=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();
}
}
}
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;
userid=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;
userid=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.自动事务处理
代码示例: |
(示例位置:光盘\code\ch05\04\ClassTran\OrderData2)
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;
}
}
}
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;
}
}
}
-------此处无银三百两------