ServiceBehaviorAttribute.TransactionIsolationLevel 속성

정의

서비스 내부에서 만들어지는 새 트랜잭션 및 클라이언트로부터 들어오는 트랜잭션의 트랜잭션 격리 수준을 지정합니다.

public:
 property System::Transactions::IsolationLevel TransactionIsolationLevel { System::Transactions::IsolationLevel get(); void set(System::Transactions::IsolationLevel value); };
public System.Transactions.IsolationLevel TransactionIsolationLevel { get; set; }
member this.TransactionIsolationLevel : System.Transactions.IsolationLevel with get, set
Public Property TransactionIsolationLevel As IsolationLevel

속성 값

IsolationLevel

트랜잭션 격리 수준을 지정하는 IsolationLevel 값입니다. 기본값은 Unspecified입니다.

예외

값이 IsolationLevel 값 중 하나가 아닌 경우

예제

다음 코드 예제에서는 트랜잭션 격리 수준을 ReadCommitted로 설정하고, 동시 트랜잭션을 지원하지 않으며, 메서드 호출로부터의 흐름이 지정된 트랜잭션이 필요하고, 처리되지 않은 예외가 발생하지 않으면 트랜잭션을 자동으로 커밋합니다.

using System;
using System.ServiceModel;
using System.Transactions;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Namespace="http://microsoft.wcf.documentation",
    SessionMode=SessionMode.Required)]
  public interface IBehaviorService
  {
    [OperationContract]
    string TxWork(string message);
  }

  // Note: To use the TransactionIsolationLevel property, you
  // must add a reference to the System.Transactions.dll assembly.
  /* The following service implementation:
   *   -- Processes messages on one thread at a time
   *   -- Creates one service object per session
   *   -- Does not release the service object when the transaction commits
   *   -- The isolation level is read committed.
   */
  [ServiceBehavior(
    ConcurrencyMode=ConcurrencyMode.Single,
    InstanceContextMode=InstanceContextMode.PerSession,
    ReleaseServiceInstanceOnTransactionComplete=false,
    TransactionIsolationLevel=System.Transactions.IsolationLevel.ReadCommitted
  )]
  public class BehaviorService : IBehaviorService, IDisposable
  {
    Guid myID;

    public BehaviorService()
    {
      myID = Guid.NewGuid();
      Console.WriteLine(
        "Object "
        + myID.ToString()
        + " created.");
    }

    /*
     * The following operation-level behaviors are specified:
     *   -- Always executes under a transaction scope.
     *   -- The transaction scope is completed when the operation terminates
     *       without an unhandled exception.
     */
    [OperationBehavior(
      TransactionAutoComplete = true,
      TransactionScopeRequired = true
    )]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    public string TxWork(string message)
    {
      // Do some transactable work.
      Console.WriteLine("TxWork called with: " + message);
      // Display transaction information.

      TransactionInformation info = Transaction.Current.TransactionInformation;
      Console.WriteLine("The distributed tx ID: {0}.", info.DistributedIdentifier);
      Console.WriteLine("The tx status: {0}.", info.Status);
      return string.Format("Hello. This was object {0}.",myID.ToString()) ;
    }

    public void Dispose()
    {
      Console.WriteLine(
        "Service "
        + myID.ToString()
        + " is being recycled."
      );
    }
  }
}


Imports System.ServiceModel
Imports System.Transactions

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="http://microsoft.wcf.documentation", SessionMode:=SessionMode.Required)> _
  Public Interface IBehaviorService
    <OperationContract> _
    Function TxWork(ByVal message As String) As String
  End Interface

  ' Note: To use the TransactionIsolationLevel property, you 
  ' must add a reference to the System.Transactions.dll assembly.
'   The following service implementation:
'   *   -- Processes messages on one thread at a time
'   *   -- Creates one service object per session
'   *   -- Does not release the service object when the transaction commits
'   *   -- The isolation level is read committed.
'   
    <ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Single, InstanceContextMode:=InstanceContextMode.PerSession, _
      ReleaseServiceInstanceOnTransactionComplete:=False, TransactionIsolationLevel:=System.Transactions.IsolationLevel.ReadCommitted)> _
    Public Class BehaviorService
        Implements IBehaviorService, IDisposable
        Private myID As Guid

        Public Sub New()
            myID = Guid.NewGuid()
            Console.WriteLine("Object " & myID.ToString() & " created.")
        End Sub

        '    
        '     * The following operation-level behaviors are specified:
        '     *   -- Always executes under a transaction scope.
        '     *   -- The transaction scope is completed when the operation terminates
        '     *       without an unhandled exception.
        '     
        <OperationBehavior(TransactionAutoComplete:=True, TransactionScopeRequired:=True), TransactionFlow(TransactionFlowOption.Mandatory)> _
        Public Function TxWork(ByVal message As String) As String Implements IBehaviorService.TxWork
            ' Do some transactable work.
            Console.WriteLine("TxWork called with: " & message)
            ' Display transaction information.

            Dim info As TransactionInformation = Transaction.Current.TransactionInformation
            Console.WriteLine("The distributed tx ID: {0}.", info.DistributedIdentifier)
            Console.WriteLine("The tx status: {0}.", info.Status)
            Return String.Format("Hello. This was object {0}.", myID.ToString())
        End Function

        Public Sub Dispose() Implements IDisposable.Dispose
            Console.WriteLine("Service " & myID.ToString() & " is being recycled.")
        End Sub
    End Class
End Namespace

다음 코드 예제가 제대로 실행되려면 기본 바인딩에서 트랜잭션 이동을 지원해야 합니다. 예를 들어, WSHttpBinding을 사용하여 트랜잭션 이동을 지원하려면 코드 또는 애플리케이션 구성 파일에서 TransactionFlow 속성을 true로 설정합니다. 다음 코드 예제에서는 앞에 나온 샘플에 대한 구성 파일을 보여 줍니다.

<configuration>
  <system.serviceModel>
    <services>
      <service  
        name="Microsoft.WCF.Documentation.BehaviorService" 
        behaviorConfiguration="mex"
        >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/BehaviorService"/>
            <add baseAddress="net.tcp://localhost:8081/BehaviorService"/>
          </baseAddresses>
        </host>
        <!--
          Note:
            This example code uses the WSHttpBinding to support transactions using the 
            WS-AtomicTransactions (WS-AT) protocol. WSHttpBinding is configured to use the  
            protocol, but the protocol is not enabled on some computers. Use the xws_reg -wsat+ 
            command to enable the WS-AtomicTransactions protocol in the MSDTC service.          
          -->
        <endpoint 
           contract="Microsoft.WCF.Documentation.IBehaviorService"
           binding="wsHttpBinding"
           bindingConfiguration="wsHttpBindingWithTXFlow"
           address="http://localhost:8080/BehaviorService"
          />
        <endpoint 
               contract="Microsoft.WCF.Documentation.IBehaviorService"
               binding="netTcpBinding"
               bindingConfiguration="netTcpBindingWithTXFlow"
               address="net.tcp://localhost:8081/BehaviorService"
          />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mex">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!-- binding configuration - configures a WSHttpBinding to require transaction flow -->
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBindingWithTXFlow" transactionFlow="true" />
      </wsHttpBinding>
      <netTcpBinding>
        <binding name="netTcpBindingWithTXFlow" transactionFlow="true" />
      </netTcpBinding>
    </bindings>

  </system.serviceModel>
</configuration>

설명

트랜잭션이 실행되는 격리 수준에 따라 다른 트랜잭션에서 변경이 있을 경우 데이터를 처리하는 방법 및 이러한 변경으로부터 보호하기 위해 사용자의 트랜잭션이 잠금을 보유해야 하는 기간이 결정됩니다. 예를 들어, 기본값인 Unspecified는 트랜잭션이 완료될 때까지 어떠한 업데이트나 삽입도 발생할 수 없음을 지정합니다.

이 속성은 클라이언트로부터 들어오는 트랜잭션의 격리 수준을 제한하는 데에도 사용할 수 있습니다.

적용 대상