次の方法で共有


自動トランザクションと XML Web サービス

ASP.NET には、Web フォーム開発者にとって使い慣れた一貫性のあるプログラミング概念を使用して XML Web サービスを作成および公開するための、組み込みサポートが用意されています。この場合のモデルはスケーラブルで拡張性があり、特に HTTP、XML、SOAP、WSDL の各オープン インターネット標準に対応します。オープン標準がサポートされているため、あらゆるクライアントやインターネット対応の装置が XML Web サービスにアクセスして使用できます。

XML Web サービスは、自動トランザクションのスコープ内でコードを実行するオプションを提供します。トランザクションでは、SQL サーバー、メッセージ キュー、Oracle サーバー、および SNA サーバーなどのリソース マネージャとのすべてのやり取りにおいて、堅牢な分散アプリケーションを実行するために必要な ACID 属性が維持されます。

自動トランザクションは、WebMethodAttribute 属性クラスの TransactionOption プロパティを使用することによって宣言できます。TransactionOption プロパティを TransactionOption.RequiresNew に設定すると、XML Web サービス クライアントが XML Web サービス メソッドを呼び出すたびに、新しいトランザクションが開始されます。

DeleteAuthor という名前の 1 つの XML Web サービス メソッドを公開するサービスを示すコード片を次に示します。この XML Web サービス メソッドは、自動トランザクションのスコープ内でデータベース操作を実行します。

<%@ WebService Language="VB" Class="Orders" %>
<%@ assembly name="System.EnterpriseServices" %>

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Util
Imports System.EnterpriseServices

Public Class Orders
   Inherits WebService
   
   <WebMethod(TransactionOption := TransactionOption.RequiresNew)> _
Public Function DeleteAuthor(lastName As String) As Integer

      Dim deleteCmd As [String] = "DELETE FROM authors2 where au_lname='" 
         & lastName & "'"
      Dim sqlConn As New SqlConnection("Integrated Security=SSPI;database=pubs;server=myserver")
      Dim myCommand As New SqlCommand(deleteCmd, sqlConn)

      ' If a XML Web service method is participating in a transaction and 
      ' an exception occurs, ASP.NET automatically aborts the transaction.
      ' Likewise, if no exception occurs, then the transaction is
      ' automatically committed.
      myCommand.Connection.Open()
      Return myCommand.ExecuteNonQuery()
   End Function
End Class
[C#]
<%@ WebService Language="C#" Class="Orders" %>
<%@ assembly name="System.EnterpriseServices" %>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web.Util;
using System.EnterpriseServices;

public class Orders : WebService 
  {
     [ WebMethod(TransactionOption=TransactionOption.RequiresNew)]
     public int DeleteAuthor(string lastName)  
     {
       String deleteCmd = "DELETE FROM authors2 
          where au_lname='" + lastName + "'" ;
    
         SqlConnection sqlConn = new SqlConnection("Integrated Security=SSPI;database=pubs;server=myserver");
         SqlCommand myCommand = new SqlCommand(deleteCmd,sqlConn);

   // If a XML Web service method is participating in a transaction and an 
   // exception occurs, ASP.NET automatically aborts the transaction.
   // Likewise, if no exception occurs, then the transaction is
   // automatically  committed.

         myCommand.Connection.Open();
      return myCommand.ExecuteNonQuery();
     }
}

メモ   トランザクションが開始されるのは、クライアントから呼び出される XML Web サービス メソッドがトランザクション メタデータを持つときだけです。呼び出される XML Web サービス メソッドが適切なトランザクション メタデータを持たない場合、それ以降の XML Web サービス メソッドは、既存のトランザクションに参加することも、新しいトランザクションを開始することもできません。

参照

自動トランザクション | 分散トランザクション | ASP.NET を使用して作成した XML Web サービスでのトランザクションへの参加