Поделиться через


Commerce Foundation Transactions

Transactions are necessary to maintain data consistency and service reliability in critical operations. Commerce Server 2009 allows for the definition of operation sequences and operation sequence components. Transactions define critical sections of the code that must either succeed or fail entirely. In Commerce Server 2009, operation sequences and operation sequence components (for instance, providers) are units of execution that may be designated as transactional. The choices made by Commerce Server 2009 administrators or developers may affect not only data consistency but also overall system performance. Transactional operation sequences and operation sequence components may execute within the scope of a .NET System.Transactions.TransactionScope.

Configuration

The administrator may designate a operation sequence as transactional with the Transactional attribute in the Operation Sequence element.

<MessageHandlers>
  <MessageHandler name="UpdateOperation_Basket"responseType="Microsoft.Commerce.Contracts.Messages.UpdateOperationResponse, Microsoft.Commerce.Contracts">
<OperationOperation sequence Transactional="true" TransactionTimeout="0:05:0" TransactionIsolationLevel="ReadCommitted">
 …

The following are the Operation Sequence attributes that relate to transactions:

Attribute

Type

Description

Transactional

bool

Indicate whether the operation sequence is transactional (true) or not (false). The default value is false.

TransactionTimeout

TimeSpan

The span of time that must elapse before the transaction is automatically aborted if not committed. The default value is 2 minutes ("0:2:0").

TransactionIsolationLevel

IsolationLevel

The data isolation mode for the transaction. The default value is ReadCommitted.

The administrator may designate a component as transaction with the TransactionScopeOption attribute in the Component element.

<MessageHandlers>
  <MessageHandler name="UpdateOperation_Basket"responseType="Microsoft.Commerce.Contracts.Messages.UpdateOperationResponse, Microsoft.Commerce.Contracts">
<OperationOperation sequence>
 <Component name="Query for the CS basket" 
description="Query for the CS basket"   type="Microsoft.Commerce.Providers.Operation.Update.Order.GetCSBasketProcessor, Microsoft.Commerce.Providers"
                     TransactionScopeOption="RequiresNew"
                     TransactionTimeout="0:04:0"
                     TransactionIsolationLevel="RepeatableRead">
 …

The following are the Component attributes that relate to transactions:

Attribute

Type

Description

TransactionScopeOption

ComponentTransactionScopeOption

The options are

None (default) – No transaction support is defined or created.

Required – As in TransactionScopeOption.Required, the transactional scope of the component creates a new transaction if none is present or participates in the existing one otherwise.

RequiresNew – As in TransactionScopeOption.RequiresNew, the component executes within a new transactional scope.

Suppress – As in TransactionScopeOption.Suppress, the component executes in a non-transactional scope.

TransactionTimeout

TimeSpan

The span of time that must elapse before the transaction is automatically aborted if not committed. The default value is 2 minutes ("0:2:0").

TransactionIsolationLevel

IsolationLevel

The data isolation mode for the transaction. The default value is ReadCommitted.

Transaction Commits and Aborts

While executing in a transaction scope the component has access to the current transaction and may subscribe to transaction events. For instance, a component may subscribe to the TransactionCompleted event to verify whether the transaction committed or aborted.

using System.Transactions;
public class MyComponent : UpdateBaseComponent
{
    private static Hashtable table = new Hashtable();
    public override void Execute(…)
    {
        if (Transaction.Current != null)
        {
            Transaction clone = Transaction.Current.Clone();
            table[clone] = clone;
            // subscribe to the TransactionCompleted event
            Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted);
        }
    }
    …
   /// <summary>
   ///    Handle transaction aborts
   /// </summary>        
   void Current_TransactionCompleted(object sender,
                                   TransactionEventArgs e)
   {
      Transaction transaction = (Transaction)table[e.Transaction];
      if (transaction.TransactionInformation.Status == TransactionStatus.Aborted)
      {
          // do something here
      }            
      transaction.Dispose();
   }
}

Exceptions

When there is a transactional operation sequence or a transactional component, if an exception happens within the execution section of a component, the current transaction is aborted. The exception follows the normal course in the execution stack and/or catch blocks as with non-transactional components in Commerce Server 2009.

Exclusions

Transactional support is not available to operations against the Profile subsystem (UserProfile, StoreProfile).

Integration with Transactional Commerce Server Operation sequences

If a transactional operation sequence in Commerce Server 2009 calls a transactional Commerce Server pipeline, the latter will be executed within the context of the original transaction - that is, the transaction initiated in Commerce Server 2009. Other operations that persist to the database in Commerce Server such as Microsoft.CommerceServer.Runtime.Orders.Basket.SaveAsOrder() also execute within the context of the original Commerce Server 2009 transaction.

See Also

Other Resources

Developing with the Multi-Channel Commerce Foundation

Commerce Foundation Operation Sequences

Commerce Foundation Operation Sequence Extensibility Point