共用方式為


使用 SOAP 工作階段的交易

交易通常是由依序提交的一系列批次所組成,且需要依序執行。如果交易中有任何批次未完成,就可能會回復交易,即恢復對該交易範圍內先前的批次所做的變更,並將受影響的資料還原至其先前的狀態。

就傳統的 SQL 資料存取而言,基礎的網路連接是依賴在處理和執行多批次交易時,服務該交易中的所有批次。例如,請考慮下列 SQL Server 連接和三個不同交易之間的關係範例:

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

請注意交易 (1)(3) 是包含在相同的批次中並在其中認可和執行;然而交易 (2) 的範圍涵蓋批次 (2)(3)。基礎連接所提供的內容可確保交易能依照所需的順序執行所有的批次。透過 HTTP SOAP 存取,若內容為單一基礎網路連接,將無法執行交易處理。因此,為了支援多批次交易的處理,單一 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 要求/回應訊息配對中執行每個批次。

以 SOAP 為基礎的交易是由 SQL Server 2005 所管理

在交易狀態變更發生時,SQL Server 2005 的執行個體就會初始化 SQL 交易。下列任一事件發生時都可能造成初始化,因為伺服器會處理用戶端的 SOAP 要求:

  • 開始交易
  • 認可交易
  • 回復交易
  • 交易中的 DTC 編列
  • 從交易進行 DTC 脫離

根據預設,伺服器會以自動認可模式運作。此行為是假設相當簡單的一對一、批次與交易的比率,而且不會對用戶端傳回任何交易資訊 (標頭、描述項)。

自動認可交易模式對於前面範例的交易 (1)(3) 可能已足夠。然而,該範例的交易 (2) 需要一個以上的批次,因此將需要手動管理交易。

手動管理交易

若要手動管理交易認可和回復,SOAP 用戶端必須在初始化 SOAP 工作階段之前先設定 sqloptions:environmentChangeNotification 選項,並在該標頭中將其 transactionBoundary 屬性值設為 true,如下列 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/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 事件,並且會傳回在相同交易的後續 SOAP 要求中用戶端所使用的交易描述項 (AQAAADMAAAA=)。

<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>

如此一來用戶端就可以使用前面回應中伺服器所傳回的工作階段識別碼以及交易式描述項,來手動聯結後續 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 工作階段識別碼。
  • 未指定 SOAP 工作階段識別碼。
  • 目前 SOAP 工作階段的交易描述項是無效的。

sqlSession 標頭的 transactionDescriptor 屬性中一次只能使用一個交易。若要在相同的工作階段中建立多個獨立的交易,您可以使用 sqlSession 標頭,但不指定其 transactionDescriptor 屬性來編列於工作階段中。請注意此方法是假設用戶端應用程式持續記錄不同的 transactionDescriptor 值。當有多個獨立的交易在相同的工作階段中為使用中時,在聯結交易時就無任何差別,只要指定 SOAP 要求中 sqlSession 標頭的 transactionDescriptor 屬性即可。

ms189243.note(zh-tw,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 工作階段

概念

交易 (Database Engine)

其他資源

使用 SOAP 工作階段

說明及資訊

取得 SQL Server 2005 協助