WCF 4 Step By Step Chapter 9 Note (Transaction)

The commonly accepted standard  mechanism for handling distributed transactions is the two-phase commit protocol.
什么是两阶段提交协议 two-phase commit
实现分布式事务的关键就是两阶段提交协议。在此协议中,一个或多个资源管理器的活动均由一个称为事务协调器的单独软件组件来控制。
1) 应用程序调用事务协调器中的提交方法
2) 事务协调器将联络事务中涉及的每个资源管理器,并通知它们准备提交事务(这是第一阶段的开始)
3) 为 了以肯定的方式响应准备阶段,资源管理器必须将自己置于以下状态:确保能在被要求提交事务时提交事务,或在被要求回滚事务时回滚事务。大多数资源管理器会 将包含其计划更改的日记文件(或等效文件)写入持久存储区中。如果资源管理器无法准备事务,它会以一个否定响应来回应事务协调器。
4) 事务协调器收集来自资源管理器的所有响应
5) 在 第二阶段,事务协调器将事务的结果通知给每个资源管理器。如果任一资源管理器做出否定响应,则事务协调器会将一个回滚命令发送给事务中涉及的所有资源管理 器。如果资源管理器都做出肯定响应,则事务协调器会指示所有的资源管理器提交事务。一旦通知资源管理器提交,此后的事务就不能失败了。通过以肯定的方式响 应第一阶段,每个资源管理器均已确保,如果以后通知它提交事务,则事务不会失败

The  OASIS organization has proposed the Web Services Atomic Transaction (WS-AtomicTransaction)  specification, which describes a standard mechanism for handling transactions in a Web services  infrastructure.The WS-AtomicTransaction specification is primarily useful when building Web services. However,
WCF is not just concerned with Web services; you can use WCF to build applications  based on many other technologies, such as COM, MSMQ, and .NET Framework Remoting.  Microsoft has provided its own transaction management features, which are built in to the current family of Microsoft Windows operating systems under the name Distributed Transaction  Coordinator, or DTC. DTC uses its own optimized transaction protocol. Transactions based  on the DTC transaction protocol are referred to as OLE transactions (OLE was the name of a  technology that was the forerunner of COM). OLE transactions are ideal if you are building  solutions based on Microsoft technologies.

Service:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,
TransactionIsolationLevel=IsolationLevel.RepeatableRead)]
public class ShoppingCartServiceImpl : IShoppingCartService
{
...
}


[OperationBehavior(TransactionScopeRequired=true, TransactionAutoComplete=false)]
public bool AddItemToCart(string productNumber)
{
...
}
**The  TransactionAutoComplete property specifies what happens to the transaction
when the operation finishes.  If you set this property to true, the transaction automatically
commits and makes all its changes permanent.  Setting this property to false keeps
the transaction active; the changes are not committed yet. The default value for this
property is true


Specifying a parameter of  TransactionFlowOption.Mandatory indicates that the client application  must create a transaction before calling this operation and must send the details of  this transaction as part of the SOAP message header when invoking the operation.
TransactionFlowOption.Allowed, which will use a transaction  created by the client if one exists (the WCF runtime will create a new transaction
if not)
TransactionFlowOption.NotAllowed, which causes the WCF runtime to disregard any client transaction and always create a new one

public interface IShoppingCartService
    {
        [OperationContract(Name = "AddItemToCart", IsInitiating = true)]
        [TransactionFlow(TransactionFlowOption.Mandatory)]
        bool AddItemToCart(string productNumber);


2)Configuration Service to Flow Transactions from Client Applications



3)Create a Transaction in the Client Application
TransactionOptions tOpts = new TransactionOptions();
tOpts.IsolationLevel = IsolationLevel.RepeatableRead;
tOpts.Timeout = new TimeSpan(0, 1, 0);
using (TransactionScope tx =
new TransactionScope(TransactionScopeOption.RequiresNew, tOpts))
{
...
tx.Complete();
}

The TransactionScopeOption parameter of the TransactionScope constructor determines
how the WCF runtime utilizes an existing transaction. If this parameter is set to Transaction
ScopeOption.RequiresNew, the WCF runtime will always create a new transaction.


4)Configure the Client Application to Flow Transactions to the ShoppingCartService
Service


5)
[OperationBehavior(TransactionScopeRequired = true,
TransactionAutoComplete = false)]
public bool Checkout()
{
// Not currently implemented
// - just indicate that the transaction completed successfully
// and return true
OperationContext.Current.SetTransactionComplete();
return true;
}


The static Current property of the OperationContext class provides access to the execution
context of the operation.  The SetTransactionComplete method indicates that the  current transaction has completed successfully and can be committed when the client  application calls the Complete method of the TransactionScope object containing this  transaction. If you need to abort the transaction, just exit the method without calling
the SetTransactionComplete method,

Additionally, you can call the SetTransactionComplete method only once in a transaction

Implementing the WS-AtomicTransaction Protocol

The NetTcpBinding binding uses OLE transactions and Microsoft’s own protocol for communicating  through DTC to other Microsoft-specific services such as SQL Server. In a heterogeneous  environment involving services based on non-Microsoft technologies, you may not be
able to employ OLE transactions. Instead, you should apply a more standardized mechanism,such as the WS-AtomicTransaction protocol.

**Important The BasicHttpBinding binding does not support transactions (OLE or WS-Atomic Transaction).such as the WS-AtomicTransaction protocol.

The choice of transaction protocol should be transparent to your services and client applications.  The code that you write to initiate, control, and support transactions based on the WSAtomicTransaction  protocol is the same as that for manipulating OLE transactions, so the same  service can execute using OLE transactions or WS-AtomicTransaction transactions, depending  on how you configure the service

If you wish to exploit the implementation of the WS-AtomicTransaction protocol provided by  the .NET Framework 4.0 with the HTTP bindings, you must configure support for the  WS-AtomicTransaction protocol in DTC.

Designing a WCF Service to Support Transactions
Transactions, Sessions, and Service Instance Context Modes

An important point to bear in mind from the points just raised is that transactions cannot span multiple sessions.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值