Supports XML Web Service transactions during runtime using the common language. It is based on the same Distributed Transaction Model in Microsoft Transaction Server (MTS) and COM + services. This model clearly determines whether an object participates in a transaction, rather than writing a specific Code It is used to process the delegate and callback of a transaction. For an ASP. net, you can declare the transaction behavior of an XML Web Service by setting the transactionoption attribute of the webmethod attribute applied to an XML Web Service method. If an exception is thrown when the XML Web Service method is executed, the transaction ends automatically. On the contrary, if no exception occurs, the transaction is automatically delegated.
The transactionoption attribute of the webmethod attribute specifies how an XML Web Service method participates in a transaction. Although this declaration level represents a transaction logic, it is a step to eliminate the actual transaction. When a transaction object accesses a data source (such as a database or Message Queue), the actual transaction is generated. The transactions associated with this object are automatically routed to appropriate resource management.Program. Like. NET framework data provider (for SQL Server or OLE DB. NET framework data provider searches for transactions in the context of the object and cataloguing the transactions through Distributed Transaction Coordinator (DTC, distributed transaction Coordination Program. All transactions are automatically generated.
The XML Web Service method can only participate in one transaction as the root of the new transaction. As the root of a new transaction, all the servers that run with the Resource Manager (such as Microsoft SQL Server, Microsoft Message Queuing, and Microsoft Host Integration Server) to maintain the ACID properties of distributed applications. The XML Web Service method that calls other XML Web Service Methods participates in different transactions because the transactions do not flow through the XML Web Service methods.
Transactions using XML Web Service Methods
Declare an XML Web Service.
[C #] <% @ WebService Language = "C #" class = "orders" %> [Visual Basic] <% @ WebService Language = "VB" class = "orders" %> |
Add an assembly command to system. enterpriseservices.
<% @ Assembly name = "system. effeciseservices, version = 1.0.3300.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a" %> |
Add the domain name space referenced to system. Web. Services and system. enterpriseservices.
[C #] Using system. Web. Services; Using system. enterpriseservices; [Visual Basic] Imports system. Web. Services Imports system. enterpriseservices |
Declare an XML Web Service method and set the transactionoption attribute of the webmethod attribute to transactionoption. requiresnew.
[C #] [Webmethod (transactionoption = transactionoption. requiresnew)] Public int deleteauthor (string lastname) [Visual Basic] <Webmethod (transactionoption: = transactionoption. requiresnew)> _ Public Function deleteauthor (lastname as string) as integer |
The following code example shows an XML Web service that uses a single XML Web Service method and calls deletedatabase. This XML Web Service method executes database operations within a transaction range. If the database operation throws an exception, the transaction is automatically stopped; otherwise, the transaction is automatically delegated.
[C #] <% @ WebService Language = "C #" class = "orders" %> <% @ Assembly name = "system. effeciseservices, version = 1.0.3300.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a" %> Using system; Using system. Data; Using system. Data. sqlclient; Using system. Web. Services; Using system. enterpriseservices; Public class orders: WebService { [Webmethod (transactionoption = transactionoption. requiresnew)] Public int deleteauthor (string lastname) { String deletecmd = "delete from authors where au_lname = '" + Lastname + "'"; String exceptioncausingintosql = "delete from nonexistingtable where Au_lname = '"+ lastname + "'"; Sqlconnection sqlconn = new sqlconnection ( "Persist Security info = false; Integrated Security = sspi; database = pubs; server = myserver "); Sqlcommand deletecmd = new sqlcommand (deletecmdsql, sqlconn ); Sqlcommand exceptioncausingcmd = new Sqlcommand (exceptioncausingcmdsql, sqlconn ); // This command shocould execute properly. Deletecmd. Connection. open (); Deletecmd. executenonquery (); // This command results in an exception, so the first command is // Automatically rolled back. Since the XML Web Service method is // Maid in a transaction, and an exception occurs, ASP. NET // Automatically aborts the transaction. The deletecmd that // Executed properly is rolled back. Int cmdresult = predictioncausingcmd. executenonquery (); Sqlconn. Close (); Return cmdresult; } } [Visual Basic] <% @ 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 <Webmethod (transactionoption: = transactionoption. requiresnew)> _ Public Function deleteauthor (lastname as string) as integer Dim deletecmdsql as string = "delete from authors where au_lname = '" + _ Lastname + "'" Dim predictioncausingdetail SQL as string = "delete from" + _ "Nonexistingtable where au_lname = '" + lastname + "'" Dim sqlconn as sqlconnection = new sqlconnection (_ "Persist Security info = false; Integrated Security = sspi; database = pubs; server = myserver ") Dim deletecmd as sqlcommand = new sqlcommand (deletecmdsql, sqlconn) Dim exceptioncausingcmd as sqlcommand = new _ Sqlcommand (exceptioncausingcmdsql, sqlconn) 'This command shoshould execute properly. Deletecmd. Connection. open () Deletecmd. executenonquery () 'This command results in an exception, so the first command is 'Automatically rolled back. Since the XML Web Service method is 'Putating in a transaction, and an exception occurs, ASP. NET 'Automatically aborts the transaction. The deletecmd that 'Executed properly is rolled back. Dim cmdresult as integer = exceptioncausingcmd. executenonquery () Sqlconn. Close () Return cmdresult End Function End Class |
Http://www.yesky.com/227/1886227.shtml