Using distributed transactions in .Net 1.x without deriving from ServicedComponent
来源:互联网 发布:java 数组长度可变吗 编辑:程序博客网 时间:2024/05/17 00:57
引用 : http://blogs.msdn.com/florinlazar/archive/2004/07/24/194199.aspx
The most used feature of System.EnterpriseServices or COM+ is the distributed transaction support. And the automatic transaction programming model in ES using attributes ([Transaction] and [AutoComplete]) is great and nice but (it is always a but!)... you need to inherit from ServicedComponent and the Transaction attribute is only available at class level, and you need to register your component in the COM+ repository and the list can continue.
If doing this seems overkill to you, because all you need is a distributed transaction to protect your code/actions and you don't care of any of the others ES features (which are great ones nevertheless) then there is a solution for you: System.EnterpriseServices.ServiceDomain. Here is some sample code:
using System;
using System.EnterpriseServices;
namespace SDSample
{
class Class1
{
[MTAThread]
static void
{
ServiceConfig config = new ServiceConfig();
config.Transaction = TransactionOption.Required;
ServiceDomain.Enter(config);
try
{
MyTxCode();
}
catch(Exception e)
{
// we got an exception
Console.WriteLine(e.Message);
// so, we should abort the transaction
ContextUtil.SetAbort();
}
finally
{
ServiceDomain.Leave();
}
}
// The code that I want to be transactional
static void MyTxCode()
{
Console.WriteLine(ContextUtil.TransactionId);
// Open connection to database 1
// Execute update in database 1
// Open connection to database 2
// Execute update in database 2
}
}
}
Of course, you can go further and create a helper class, let’s call it ESTransactionScope (similar to System.Transactions.TransactionScope that will arrive in Whidbey) that will be very easy to use:
using System;
using System.EnterpriseServices;
namespace SDSample2
{
class Class1
{
[MTAThread]
static void
{
using( ESTransactionScope ts = new ESTransactionScope())
{
MyTxCode();
// Everything went well, no exception thrown
// so let’s vote for Commit
ts.Complete();
}
}
static void MyTxCode()
{
Console.WriteLine(ContextUtil.TransactionId);
// Open connection to database 1
// Execute update in database 1
// Open connection to database 2
// Execute update in database 2
}
}
// Used to create transactional code blocks
class ESTransactionScope : IDisposable
{
// Dispose must be called to exit the transactional block
public void Dispose()
{
if(!this.Consistent)
{
ContextUtil.SetAbort();
}
ServiceDomain.Leave();
}
// by calling this method, you mark the scope as being consistent
// and ready to for commit
// if the method is never called, upon dispose, the scope will abort the transaction
public ESTransactionScope()
{
EnterTxContext(TransactionOption.Required);
}
public ESTransactionScope(TransactionOption txOption)
{
EnterTxContext(txOption);
}
private void EnterTxContext(TransactionOption txOption)
{
ServiceConfig config = new ServiceConfig();
config.Transaction = txOption;
ServiceDomain.Enter(config);
}
// By default, the scope is inconsistent;
// To Commit the transaction on exit, the Consistent flag
// must be set to true before Dispose is called
private bool Consistent = false;
}
}
If you need your app to work with .Net 1.0 or on Windows 2000 or XP pre-SP2, you can use the trick that Don Box posted at http://www.gotdotnet.com/team/dbox/default.aspx?key=2004-07-12T08:40:44Z It uses exactly one transactional ServicedComponent based class and a DoCallback method to which you pass the delegate to your MyTxCode function that needs to execute in a transaction.
- Using distributed transactions in .Net 1.x without deriving from ServicedComponent
- Distributed transactions in Spring, with and without XA
- Running Distributed Transactions in RAC without DTP service
- Distributed transactions in Spring, with and without XA
- .net 2.0 的 Distributed Transactions(使用TransactionScope )
- Distributed Transactions
- Large-scale Incremental Processing Using Distributed Transactions and Notifications
- Distributed Application in .Net
- Distributed Transactions and Timeouts
- Distributed Transactions and Timeouts
- distributed-transactions-microservices-icebergs
- Creating a XML Document from scratch without using a file in C#
- Distributed .NET Programming in C#
- 经典论文翻译导读之《Large-scale Incremental Processing Using Distributed Transactions and Notifications》
- 经典论文翻译导读之《Large-scale Incremental Processing Using Distributed Transactions and Notifications》 转载
- Creating a multileader in AutoCAD using a jig from .NET
- BDNtv: Deriving a model from an existing database with ECO II in Delphi 2005
- Adding dynamic nodes to ASP.NET site maps at runtime by deriving from StaticSiteMapProvider
- 计算机指针
- 牙利命名法
- 关于敏捷思维的一个连接&敏捷宣言理解
- MYSQL+JDBC,主键对可写记录集的必要性
- Java HttpServer
- Using distributed transactions in .Net 1.x without deriving from ServicedComponent
- 保留号码的方案
- 夸张和浮躁
- 一个经典的ADO.NET入门例子
- 关于自动化和流水线监控的WEB平台开发
- 柳传志——父亲教我正直做人
- 转贴:Jsp中生成验证码图片
- 网站首页head区代码规范
- B/S开发常用JavaScript技术