在.NET中处理事务(2)

自动事务和 XML Web services [C#]

ASP.NET 使用与 Web 窗体一致且相似的编程抽象模型为创建和公开 XML Web services 提供内置支持。结果模型是可伸缩和可扩展的,并接受 HTTP、XML、SOAP、WSDL 开放式 Internet 以及其他标准。XML Web services 支持开放式标准,因此任何客户端或支持 Internet 的设备都可访问和使用 XML Web services。

XML Web services 提供了在自动事务范围内运行代码的选项。事务确保与资源管理器(如 SQL Server、MSMQ 服务器、Oracle 服务器和 SNA 服务器)的所有交互维护运行可靠的分布式应用程序所需的 ACID 属性。

使用 WebMethod 特性的 TransactionOption 属性声明一个自动事务。如果将 TransactionOption 属性设置为 TransactionOption.RequiresNew ,则每次 XML Web services 客户端调用 XML Web services 方法时,都会开始一个新事务。

下列代码片断显示公开一个 XML Web services 方法(名为 DeleteAuthor)的服务。此 XML Web services 方法执行一个自动事务范围内的数据库操作。

[Visual Basic]

1@ WebService Language="VB" Class="Orders" 
1@ 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

 1<webmethod(transactionoption :="TransactionOption.RequiresNew)"> _ 
 2
 3Public Function DeleteAuthor(lastName As String) As Integer 
 4
 5Dim deleteCmd As [String] = "DELETE FROM authors2 where au_lname='" 
 6
 7&amp; lastName &amp; "'" 
 8
 9Dim sqlConn As New SqlConnection("user 
10
11id=sa;database=pubs;server=myserver") 
12
13Dim myCommand As New SqlCommand(deleteCmd, sqlConn) 
14
15' If a XML Web service method is participating in a transaction and 
16
17' an exception occurs, ASP.NET automatically aborts the transaction. 
18
19' Likewise, if no exception occurs, then the transaction is 
20
21' automatically  committed. 
22
23myCommand.Connection.Open() 
24
25Return myCommand.ExecuteNonQuery() 
26
27End Function 
28
29End Class 
30
31[C#] 

@ WebService Language="C#" Class="Orders"

@ assembly name="System.EnterpriseServices"

  1
  2using System; 
  3
  4using System.Data; 
  5
  6using System.Data.SqlClient; 
  7
  8using System.Web.Services; 
  9
 10using System.Web.Util; 
 11
 12using System.EnterpriseServices; 
 13
 14public class Orders : WebService 
 15
 16{ 
 17
 18[ WebMethod(TransactionOption=TransactionOption.RequiresNew)] 
 19
 20public int DeleteAuthor(string lastName) 
 21
 22{ 
 23
 24String deleteCmd = "DELETE FROM authors2 
 25
 26where au_lname='" + lastName + "'" ; 
 27
 28SqlConnection sqlConn = new SqlConnection("user 
 29
 30id=sa;database=pubs;server=myserver"); 
 31
 32SqlCommand myCommand = new SqlCommand(deleteCmd,sqlConn); 
 33
 34// If a XML Web service method is participating in a transaction and an 
 35
 36// exception occurs, ASP.NET automatically aborts the transaction. 
 37
 38// Likewise, if no exception occurs, then the transaction is 
 39
 40// automatically  committed. 
 41
 42myCommand.Connection.Open(); 
 43
 44return myCommand.ExecuteNonQuery(); 
 45
 46} 
 47
 48} 
 49
 50** 注意  ** 仅当激活的  XML Web services 方法(从客户端调用的方法)有事务元数据时,事务才会开始。如果激活的 XML Web services 方法未携带合适的事务元数据,则后继的 XML Web services 方法可能既不参与现有事务也不开始新事务。 
 51
 52###  自动事务和  .NET  框架类  [C#] 
 53
 54只要准备了  .NET 框架类参与自动事务,此类的实例就可以参与自动事务。类实例或对象访问的每个资源都在事务中登记。例如,如果一个对象使用 ADO.NET 发送数据库中某帐户上的钱,此数据库的资源管理器将确定该对象是否在事务中执行。如果对象是在事务中执行,则资源管理器自动在事务中登记此数据库。 
 55
 56使用下列过程准备参与自动事务的类: 
 57
 58  1. 将  TransactionAttribute 应用于此类。 
 59  2. 从  ServicedComponent 类派生此类。 
 60  3. 用强名称为程序集签名。 
 61
 62
 63
 64若要使用特性为程序集签名,请使用  Sn.exe 实用工具创建一个密钥对。 
 65    
 66    
 67    sn -k **TestApp.snk**
 68
 69添加 ** AssemblyKeyFileAttribute  ** 或 **AssemblyKeyNameAttribute** 程序集特性(它们指定包含密钥对的文件的名称),以使用强名称为程序集签名。 
 70    
 71    
 72    [Visual Basic]  
 73    
 74    
 75    <assembly: ")="" **testapp.snk**="" assemblykeyfileattribute("="">
 76    
 77    
 78    [C#]
 79    
 80    
 81    [assembly: AssemblyKeyFileAttribute(" **TestApp.snk** ")]
 82
 83  4. 向  COM+ 目录注册包含此类的程序集。 
 84
 85
 86
 87如果类的客户端调用实例是由公共语言运行库管理的,则注册将自动执行。但是,如果预期非托管调用方可能创建和调用类的实例,请使用  .NET 服务安装工具 (Regsvcs.exe) 手动执行注册。 
 88
 89下列示例显示如何将 ** TransactionAttribute  ** 应用到从 **ServicedComponent** 类派生的类。 
 90    
 91    
 92    [Visual Basic]
 93    
 94    
 95    <transaction(transactionoption.required)> Public Class Bar
 96    
 97    
 98       Inherits ServicedComponent
 99    
100    
101       '. . .
102    
103    
104    End Class
105
106[C#] 
107    
108    
109    [Transaction(TransactionOption.Required)]
110    
111    
112    public class Bar(): ServicedComponent
113    
114    
115    {
116    
117    
118      //. . .
119    
120    
121    }
122
123应用事务特性时,可以交替使用 ** Transaction  ** 、 ** transaction  ** 、 ** TransactionAttribute  ** 和 **transactionattribute** 。例如,可以使用 **Transaction** 或 **transactionattribute** 产生相同的结果。 
124
125下表列出并描述每个构造函数变体。 
126
127** 特性值  ** ** **
128
129| 
130
131** 说明  ** ** **  
132  
133---|---  
134  
135** Disabled  **
136
137| 
138
139消除自动事务对对象的控制。应用此特性值的对象可以直接将分布式事务处理协调器  (DTC) 用于事务性支持。 
140    
141    
142    [Transaction(TransactionOption.Disabled)]  
143  
144** NotSupported  **
145
146| 
147
148指示对象不在事务范围内运行。处理请求后,不管是否有活动事务,均在没有事务的情况下创建其对象上下文。 
149    
150    
151    [Transaction(TransactionOption.NotSupported)]  
152  
153** Supported  **
154
155| 
156
157指示如果有事务,则对象在现有事务的上下文中运行。如果没有事务,则对象在没有事务的情况下运行。 
158    
159    
160    [Transaction(TransactionOption.Supported)]  
161  
162** Required  **
163
164(默认值) 
165
166| 
167
168指示对象需要事务。如果有事务,则对象在现有事务范围中运行。如果没有事务,则对象启动一个事务。 
169    
170    
171    [Transaction(TransactionOption.Required)]  
172  
173** RequiresNew  **
174
175| 
176
177指示对象需要事务且为每个请求启动新事务。 
178    
179    
180    [Transaction(TransactionOption.RequiresNew)]  
181  
182##  示例类 
183
184下列代码示例说明自动事务的若干元素。此例中,事务性类和调用此类的客户端都由运行库管理。 
185    
186    
187    [Visual Basic]
188    
189    
190    ' -----------------------------------------------------------------
191    
192    
193    ' TestApp.vb
194    
195    
196    ' Generate a Strong name: 
197    
198    
199    '    sn -k TestApp.snk
200    
201    
202    ' Compile the code:
203    
204    
205    '    vbc /target:exe /r:System.EnterpriseServices.dll TestApp.vb
206    
207    
208    ' Run TestApp:
209    
210    
211    '    start TestApp.exe
212    
213    
214    ' -----------------------------------------------------------------
215    
216    
217    Option Explicit
218    
219    
220    Option Strict
221    
222    
223     
224    
225    
226    Imports System
227    
228    
229    Imports System.Runtime.CompilerServices
230    
231    
232    Imports System.EnterpriseServices
233    
234    
235    Imports System.Reflection
236    
237    
238     
239    
240    
241    'Registration details.
242    
243    
244    'COM+ application name as it appears in the COM+ catalog.
245    
246    
247    <assembly: applicationname("testapp")="">
248    
249    
250    'Strong name for assembly.
251    
252    
253    <assembly: assemblykeyfileattribute("testapp.snk")="">
254    
255    
256     
257    
258    
259    <transaction(transactionoption.required)> Public Class Account
260    
261    
262       Inherits ServicedComponent
263    
264    
265       
266    
267    
268       'Provides SetComplete behavior in the absence of exceptions.
269    
270    
271       <autocomplete()> Public Sub Debit(amount As Integer)
272    
273    
274          ' Do some database work. Any exception thrown here aborts the 
275    
276    
277          ' transaction; otherwise, transaction commits.
278    
279    
280       End Sub
281    
282    
283    End Class
284    
285    
286     
287    
288    
289    Public Class client
290    
291    
292       Public Shared Sub Main()
293    
294    
295          Dim accountX As New Account()
296    
297    
298          accountX.Debit(100)
299    
300    
301          Environment.Exit(0)
302    
303    
304       End Sub
305    
306    
307    End Class
308
309[C#] 
310    
311    
312    // -----------------------------------------------------------------
313    
314    
315    // TestApp.cs
316    
317    
318    // Generate a Strong name: 
319    
320    
321    //    sn -k TestApp.snk
322    
323    
324    // Compile the code:
325    
326    
327    //    csc /target:exe /r:System.EnterpriseServices.dll TestApp.cs
328    
329    
330    // Run TestApp:
331    
332    
333    //    start TestApp.exe
334    
335    
336    // -----------------------------------------------------------------
337    
338    
339    using System;
340    
341    
342    using System.Runtime.CompilerServices;
343    
344    
345    using System.EnterpriseServices;
346    
347    
348    using System.Reflection;
349    
350    
351     
352    
353    
354    //Registration details.
355    
356    
357    //COM+ application name as it appears in the COM+ catalog.
358    
359    
360    [assembly: ApplicationName("TestApp")]
361    
362    
363    //Strong name for assembly.
364    
365    
366    [assembly: AssemblyKeyFileAttribute("TestApp.snk")]
367    
368    
369     
370    
371    
372    [Transaction(TransactionOption.Required)]
373    
374    
375    public class Account : ServicedComponent
376    
377    
378    {
379    
380    
381      //Provides SetComplete behavior in the absence of exceptions.
382    
383    
384      [AutoComplete]
385    
386    
387      public void Debit(int amount)
388    
389    
390      {
391    
392    
393         // Do some database work. Any exception thrown here aborts the 
394    
395    
396         // transaction; otherwise, transaction commits.
397    
398    
399      }
400    
401    
402    }
403    
404    
405     
406    
407    
408    public class client
409    
410    
411    {
412    
413    
414      public static int Main() 
415    
416    
417      {
418    
419    
420        Account accountX = new Account();
421    
422    
423        accountX.Debit(100);
424    
425    
426        return 0;
427    
428    
429      }
430    
431    
432    }
433
434###  自动事务中的投票  [C#] 
435
436.NET 框架类和 ASP.NET 页可以通过投票来提交或中止它们的当前事务。默认情况下,如果代码中没有显式投票,则默认为赞成提交。但默认提交可能会延长为每个事务释放昂贵资源所用的时间,从而可能降低应用程序的性能。 
437
438显式投票还允许类或页在遇到严重错误时中止事务。此外,可以通过在事务处理的早期捕获致命错误、结束事务、释放资源等手段来提高应用程序的性能。 
439
440##  使用自动完成 
441
442** System.EnterpriseServices.AutoCompleteAttribute  ** 使参与事务的对象投票赞成在方法正常返回时完成事务。如果方法调用引发异常,则中止事务。只能将此特性应用于从 ServicedComponent 类派生的类。 
443
444若要使用此功能,请在类方法前插入此特性。如果将此特性添加到接口方法,则公共语言运行库将忽略它。下列代码片断显示此特性在事务识别类上的位置。 
445    
446    
447    [Visual Basic]
448    
449    
450    &lt;SPAN lang=EN-US style="DISPLAY: none; FONT-SIZ</autocomplete()></transaction(transactionoption.required)></assembly:></assembly:></transaction(transactionoption.required)></assembly:></webmethod(transactionoption>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus