Share via


SOAP セッションとトランザクションの併用

トランザクションは、多くの場合、決められた実行順序に従って 1 つずつ送信される一連のバッチから構成されます。トランザクション内のあるバッチが完了しなかった場合はトランザクションをロールバックできます。このとき、トランザクションのスコープの中で問題のバッチより前に行われた変更が元に戻され、影響を受けたデータが前の状態に復元されます。

従来の SQL データ アクセスでは、複数のバッチに分割されたトランザクションの処理および実行のとき、すべてのバッチを基になるネットワーク接続に依存して処理します。たとえば、SQL Server 接続と 3 つの異なるトランザクションの関係を示す、次の例を考えてみます。

SQL connection(1)
     --> SQL batch(1)
          --> transaction(1)
     --> SQL batch(2)
          --> transaction(2a)
     --> SQL batch(3)
          --> transaction(2b), transaction(3)

トランザクション (1)(3) は、いずれも 1 つのバッチ内にあり、そこで実行およびコミットされます。一方、トランザクション (2) はバッチ (2)(3) にまたがっています。基になる接続では、トランザクションのすべてのバッチを必要な順序で実行することを保証するコンテキストが用意されます。HTTP SOAP アクセスでは、基になるネットワーク接続が 1 つというコンテキストを前提としてトランザクションを実行することはできません。したがって、複数のバッチに分割されたトランザクションの処理をサポートするために、1 つの SOAP セッションが同じ目的を果たします。たとえば、前述と同じパターンのバッチとトランザクションが、HTTP SOAP アクセスでどのように実行されるかを次のコードで示します。

SOAP session(1)
     --> SQL batch(1)
          --> transaction(1)
     --> SQL batch(2)
          --> transaction(2a)
     --> SQL batch(3)
          --> transaction(2b), transaction(3)

同一の SOAP セッション (1) がアクティブである間は、そのセッションを基になるコンテキストとして、それぞれのバッチを別個の SOAP 要求/応答メッセージの組で実行できます。

SQL Server 2005 による SOAP ベースのトランザクションの管理方法

SQL トランザクションの状態変化が発生すると、SQL Server 2005 インスタンスでトランザクションが初期化されます。クライアントからの SOAP 要求を処理するサーバーが発生させる次のイベントのうち、いずれかによって状態変化が起こります。

  • トランザクションの開始
  • トランザクションのコミット
  • トランザクションのロールバック
  • トランザクションの DTC への参加
  • トランザクションの DTC からの参加解除

サーバーの既定の動作モードはトランザクションの自動コミット モードです。このモードでは、バッチとトランザクションの比率が単純に 1:1 と想定され、トランザクションの情報 (ヘッダー、記述子) はクライアントに返されません。

上記の例のトランザクション (1)(3) には、トランザクション自動コミット モードで十分です。しかし、トランザクション (2) には複数のバッチが必要なので、手動でのトランザクション管理が必要です。

トランザクションの手動管理

トランザクションのコミットとロールバックを手動で管理するには、次の SOAP 要求メッセージの例で示すとおり、SOAP セッションを初期化する前に SOAP クライアントで sqloptions:environmentChangeNotification オプションを指定し、ヘッダー内で transactionBoundary 属性の値を true に設定する必要があります。

<SOAP-ENV:Envelope  xmlns:SOAP-ENV="https://schemas.xmlsoap.org/soap/envelope/"
              xmlns:sql="https://schemas.microsoft.com/sqlserver/2004/SOAP"
              xmlns:xsi="http://www.w3.org/2004/XMLSchema-instance"
              xmlns:sqlparam="https://schemas.microsoft.com/sqlserver/2004/sqltypes/SqlParameter"
              xmlns:sqlsoaptypes="https://schemas.microsoft.com/sqlserver/2004/sqltypes"
              xmlns:sqloptions="https://schemas.microsoft.com/sqlserver/2004/SOAP/Options">
  <SOAP-ENV:Header>
    <sqloptions:environmentChangeNotifications transactionBoundary="true" />
    <sqloptions:sqlSession initiate="true" timeout="60"/>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <sql:sqlbatch>
      <sql:BatchCommands>
        USE master
        BEGIN TRANSACTION
        CREATE TABLE session_table (col1 int);
      </sql:BatchCommands>
    </sql:sqlbatch>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

このメッセージは、現在のセッションのトランザクション自動コミット モードを無効にするようサーバーに通知しています。するとサーバーは、次のような SOAP 応答を送信して、セッションを識別し (jGqn3/X73EGHjFxZ12zovw==)、SqlTransaction 値を返します。この値は、サーバーでの BEGIN TRANSACTION イベントおよびトランザクション記述子 (AQAAADMAAAA=) を確認しています。トランザクション記述子は、同じトランザクションの一部となる今後の SOAP 要求でクライアントが使用します。

<SOAP-ENV:Envelope xml:space="preserve"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                   xmlns:SOAP-ENV="https://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:sql="https://schemas.microsoft.com/sqlserver/2004/SOAP"
                   xmlns:sqlsoaptypes="https://schemas.microsoft.com/sqlserver/2004/SOAP/types"
                   xmlns:sqlrowcount="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlRowCount"                    xmlns:sqlmessage="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlMessage"                    xmlns:sqlresultstream="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlResultStream"                    xmlns:sqltransaction="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlTransaction"                    xmlns:sqltypes="https://schemas.microsoft.com/sqlserver/2004/sqltypes">
  <SOAP-ENV:Header xmlns:sqloptions="https://schemas.microsoft.com/sqlserver/2004/SOAP/Options">
    <sqloptions:sqlSession sessionId="jGqn3/X73EGHjFxZ12zovw==" timeout="1200">
    </sqloptions:sqlSession>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <sql:sqlbatchResponse>
       <sql:sqlbatchResult>
          <sqlresultstream:SqlTransaction xsi:type="sqltransaction:SqlTransaction">
             <sqltransaction:Descriptor>AQAAADMAAAA=</sqltransaction:Descriptor>
             <sqltransaction:Type>Begin</sqltransaction:Type>
          </sqlresultstream:SqlTransaction>
       </sql:sqlbatchResult>
    </sql:sqlbatchResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

クライアントは、前の応答でサーバーから返されたものと同じのセッション ID とトランザクション記述子を使用して、これ以降の SOAP 要求でトランザクションに手動で参加することができます。次の例を参照してください。

<SOAP-ENV:Envelope  xmlns:SOAP-ENV="https://schemas.xmlsoap.org/soap/envelope/"
                    xmlns:sql="https://schemas.microsoft.com/sqlserver/2004/SOAP"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:sqlparam="https://schemas.microsoft.com/sqlserver/2004/sqltypes/SqlParameter"
                    xmlns:sqlsoaptypes="https://schemas.microsoft.com/sqlserver/2004/sqltypes"
                    xmlns:sqloptions="https://schemas.microsoft.com/sqlserver/2004/SOAP/Options">
  <SOAP-ENV:Header>
    <sqloptions:sqlSession sessionId="jGqn3/X73EGHjFxZ12zovw==" transactionDescriptor="AQAAADMAAAA="/>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <sql:sqlbatch>
      <sql:BatchCommands>
        INSERT INTO session_table values (2005)
        COMMIT TRANSACTION
      </sql:BatchCommands>
    </sql:sqlbatch>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SOAP 要求が明示的にトランザクションに参加できるのは、トランザクションを開始した SOAP セッションと同一のセッション内でバッチを実行している場合に限られます。それ以外の場合で、次のいずれかの条件に該当すると SOAP エラーが返されます。

  • 異なる SOAP セッション ID が指定されている。
  • SOAP セッション ID が指定されていない。
  • トランザクション記述子が現在の SOAP セッションでは有効でない。

sqlSession ヘッダーの transactionDescriptor 属性で一度に使用できるトランザクションは 1 つのみです。同一のセッション内に複数の独立したトランザクションを確立するには、transactionDescriptor 属性を指定しないで、sqlSession ヘッダーを使用することでセッションに参加できます。この方式は、クライアント アプリケーションでさまざまな transactionDescriptor 値を追跡していることを前提としています。独立した複数のトランザクションが同一のセッション内でアクティブな場合も、トランザクションへの参加方法は同じで、SOAP 要求の sqlSession ヘッダーで transactionDescriptor 属性を指定するだけです。

ms189243.note(ja-jp,SQL.90).gifメモ :
アクティブなトランザクションの入れ子のレベルを判別するには、Transact-SQL @@TRANCOUNT 関数の値を使用します。

sqlTransaction の XSD スキーマ

次に、SOAP メッセージで使用する sqlTransaction ヘッダーの XSD スキーマを示します。

<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    attributeFormDefault="qualified"
    elementFormDefault="qualified"
    targetNamespace="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlTransaction">
<xsd:annotation><xsd:documentation xml:lang="en">&#xd;&#xa;(c) Copyright 2004, Microsoft Corporation&#xd;&#xa;&#xd;&#xa;The following schema for Microsoft SQL Server is presented in XML format and is for informational purposes only. Microsoft Corporation ("Microsoft") may have trademarks, copyrights, or other intellectual property rights covering subject matter in the schema.&#xd;&#xa;&#xd;&#xa;Microsoft does not make any representation or warranty regarding the schema or any product or item developed based on the schema. The schema is provided to you on an AS IS basis.  Microsoft disclaims all express, implied and statutory warranties, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, and freedom from infringement. Without limiting the generality of the foregoing, Microsoft does not make any warranty of any kind that any item developed based on the schema, or any portion of the schema, will not infringe any copyright, patent, trade secret, or other intellectual property right of any person or entity in any country. It is your responsibility to seek licenses for such intellectual property rights where appropriate.&#xd;&#xa;&#xd;&#xa;MICROSOFT SHALL NOT BE LIABLE FOR ANY DAMAGES OF ANY KIND ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SCHEMA, INCLUDING WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL (INCLUDING ANY LOST PROFITS), PUNITIVE OR SPECIAL DAMAGES, WHETHER OR NOT MICROSOFT HAS BEEN ADVISED OF SUCH DAMAGES.&#xd;&#xa;</xsd:documentation></xsd:annotation>
  <xsd:complexType name="SqlTransaction">
    <xsd:sequence minOccurs="1" maxOccurs="1">
      <xsd:element name="Descriptor" type="xsd:base64Binary" />
      <xsd:element name="Type">
         <xsd:simpleType>
            <xsd:restriction base="xsd:string">
              <xsd:enumeration value="Begin"/>
              <xsd:enumeration value="Commit"/>
              <xsd:enumeration value="Rollback"/>
              <xsd:enumeration value="EnlistDTC"/>
              <xsd:enumeration value="Defect"/>
            </xsd:restriction>
         </xsd:simpleType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

SQL トランザクションの XSI 型は xsi:type="typesNs:SqlTransaction" で、typesNshttps://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlTransaction 名前空間にバインドされます。

参照

関連項目

SOAP セッションの使用

概念

トランザクション (データベース エンジン)

その他の技術情報

SOAP セッションを使用した作業

ヘルプおよび情報

SQL Server 2005 の参考資料の入手