次の方法で共有


自動トランザクションと .NET Framework クラス

必要な設定を行うことによって、.NET Framework クラスのインスタンスが自動トランザクションに参加できます。クラスのインスタンス、つまりオブジェクトによってアクセスされるリソースは、トランザクションで確保されます。たとえば、あるオブジェクトが ADO.NET を使用してデータベース内のアカウントにお金を転記する場合、そのデータベースのリソース マネージャは、そのオブジェクトがトランザクションで実行される必要があるかどうかを決定します。必要がある場合、リソース マネージャはそのデータベースをトランザクションに確保します。

クラスを自動トランザクションに参加させるには、次の手順に従います。

  1. クラスに TransactionAttribute を適用します。

  2. ServicedComponent クラスからクラスを派生させます。

  3. アセンブリに厳密な名前で署名します。

    属性を使用してアセンブリに署名するには、Sn.exe を使用してキー ペアを作成します。

    sn -k TestApp.snk
    

    厳密な名前でアセンブリに署名するときに使用するキー ペアを格納するファイルの名前を指定して、AssemblyKeyFileAttribute アセンブリ属性または AssemblyKeyNameAttribute アセンブリ属性を追加します。

    <assembly: AssemblyKeyFileAttribute("TestApp.snk")>
    [C#]
    [assembly: AssemblyKeyFileAttribute("TestApp.snk")]
    

    詳細については、「厳密な名前でのアセンブリへの署名」を参照してください。

  4. クラスを含むアセンブリを COM+ カタログに登録します。

    クラスのインスタンスを呼び出すクライアントが共通言語ランタイムによって管理される場合は、自動的に登録が行われます。ただし、アンマネージの呼び出し元がクラスのインスタンスを作成して呼び出すことが予測される場合は、.NET サービス インストール ツール (Regsvcs.exe) を使用して手動で登録します。

ServicedComponent クラスから派生するクラスに TransactionAttribute 属性を適用する例を次に示します。

<Transaction(TransactionOption.Required)> Public Class SampleClass
   Inherits ServicedComponent
   '. . .
End Class
[C#]
[Transaction(TransactionOption.Required)]
public class SampleClass(): ServicedComponent
{
  //. . .
}

トランザクション属性を適用するときは、TransactiontransactionTransactionAttributetransactionattribute を使用できます。たとえば、Transactiontransactionattribute のどちらを使用しても同じ結果が得られます。

各コンストラクタのバリエーションの一覧と説明を次の表に示します。

属性値 説明
Disabled オブジェクトにおける自動トランザクションの制御を削除します。この属性値を適用されたオブジェクトは、分散トランザクション コーディネータ (DTC: Distributed Transaction Coordinator) を直接使用して、トランザクション サポートを実現できます。
[Transaction(TransactionOption.Disabled)]
NotSupported オブジェクトはトランザクションのスコープ内で実行されません。要求が処理されるときに、アクティブなトランザクションがあるかどうかに関係なく、トランザクションなしにオブジェクトのコンテキストが作成されます。
[Transaction(TransactionOption.NotSupported)]
Supported オブジェクトは既存のトランザクション (存在する場合) のコンテキスト内で実行されます。トランザクションが存在しない場合は、オブジェクトがトランザクションなしで実行されます。
[Transaction(TransactionOption.Supported)]
Required

(既定値)

オブジェクトにはトランザクションが必要です。オブジェクトは既存のトランザクション (存在する場合) のスコープ内で実行されます。トランザクションが存在しない場合は、オブジェクトがトランザクションを開始します。
[Transaction(TransactionOption.Required)]
RequiresNew オブジェクトにはトランザクションが必要であり、要求ごとに新しいトランザクションが開始されます。
[Transaction(TransactionOption.RequiresNew)]

クラスのサンプル

次のコード例は、自動トランザクションのいくつかの要素を示します。この例では、クラスを呼び出すクライアントとトランザクション クラスの両方が、ランタイムによって管理されます。

' -----------------------------------------------------------------
' TestApp.vb
' Generate a Strong name: 
'    sn -k TestApp.snk
' Compile the code:
'    vbc /target:exe /r:System.EnterpriseServices.dll TestApp.vb
' Run TestApp:
'    start TestApp.exe
' -----------------------------------------------------------------
Option Explicit
Option Strict

Imports System
Imports System.Runtime.CompilerServices
Imports System.EnterpriseServices
Imports System.Reflection

'Registration details.
'COM+ application name as it appears in the COM+ catalog.
<assembly: ApplicationName("TestApp")>
'Strong name for assembly.
<assembly: AssemblyKeyFileAttribute("TestApp.snk")>

<Transaction(TransactionOption.Required)> Public Class Account
   Inherits ServicedComponent
   
   'Provides SetComplete behavior in the absence of exceptions.
   <AutoComplete()> Public Sub Debit(amount As Integer)
      ' Do some database work. Any exception thrown here aborts the 
      ' transaction; otherwise, transaction commits.
   End Sub
End Class

Public Class client
   Public Shared Sub Main()
      Dim accountX As New Account()
      accountX.Debit(100)
      Environment.Exit(0)
   End Sub
End Class
[C#]
// -----------------------------------------------------------------
// TestApp.cs
// Generate a Strong name: 
//    sn -k TestApp.snk
// Compile the code:
//    csc /target:exe /r:System.EnterpriseServices.dll TestApp.cs
// Run TestApp:
//    start TestApp.exe
// -----------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
using System.EnterpriseServices;
using System.Reflection;

//Registration details.
//COM+ application name as it appears in the COM+ catalog.
[assembly: ApplicationName("TestApp")]
//Strong name for assembly.
[assembly: AssemblyKeyFileAttribute("TestApp.snk")]

[Transaction(TransactionOption.Required)]
public class Account : ServicedComponent
{
  //Provides SetComplete behavior in the absence of exceptions.
  [AutoComplete]
  public void Debit(int amount)
  {
     // Do some database work. Any exception thrown here aborts the 
     // transaction; otherwise, transaction commits.
  }
}

public class client
{
  public static int Main() 
  {
    Account accountX = new Account();
    accountX.Debit(100);
    return 0;
  }
}

参照

自動トランザクション | 自動トランザクションにおけるボート | サービス コンポーネントの作成