ASP中的事务处理
这个项目使用ASP,其中一个页面要同时更新两个数据库的表,翻了翻MSDN之后,了解到使用@Transaction可以控制页面的事务,再使用ObjectContext.SetAbort或ObjectContext.SetComplete方法就可以了。基于Component Service的事务处理就是这么简单!
下面是我写的代码测试:
下面是我写的代码测试:
1<% @Transaction = Required %>
2<% Option Explicit %>
3<%
4
5Dim con1, con2
6Dim rs1, rs2
7
8On Error Resume Next
9Randomize
10
11' Open Connection
12Set con1 = Server.CreateObject("ADODB.Connection")
13Set con2 = Server.CreateObject("ADODB.Connection")
14
15con1.Open "provider=sqloledb.1; data source=192.168.107.102; user id=sa; password=123456; initial catalog=northwind"
16con2.Open "provider=sqloledb.1; data source=192.168.107.97; user id=sa; password=sa; initial catalog=northwind"
17
18' Open Recordset
19Set rs1 = Server.CreateObject("ADODB.Recordset")
20Set rs2 = Server.CreateObject("ADODB.Recordset")
21
22rs1.Open "SELECT * FROM T_ADO1", con1, 1, 3
23rs2.Open "SELECT * FROM T_ADO2", con2, 1, 3
24
25' AddNew Record
26rs1.AddNew
27rs1("num") = Rnd() * 100
28rs1.Update
29
30rs2.AddNew
31rs2("num") = Rnd() * 100
32rs2.Update
33
34' Raise Error Or Not
35If Request.QueryString("error") = 1 Then
36 tempNumber = CInt("abc")
37End If
38
39' If Error Occured, Rollback Transaction
40If Err.Number <> 0 Then
41 ObjectContext.SetAbort
42 Response.Write Err.Number & "<br>" & Err.Description
43Else
44 Response.Write "OK"
45End If
46
47' Close Recordset
48If Not (rs1 Is Nothing) Then
49 rs1.Close
50End If
51If Not (rs2 Is Nothing) Then
52 rs2.Close
53End If
54
55Set rs1 = Nothing
56Set rs2 = Nothing
57
58
59' Close Connection
60If Not (con1 Is Nothing) Then
61 con1.Close
62End If
63If Not (con2 Is Nothing) Then
64 con2.Close
65End If
66
67Set con1 = Nothing
68Set con2 = Nothing
69
70%>
2<% Option Explicit %>
3<%
4
5Dim con1, con2
6Dim rs1, rs2
7
8On Error Resume Next
9Randomize
10
11' Open Connection
12Set con1 = Server.CreateObject("ADODB.Connection")
13Set con2 = Server.CreateObject("ADODB.Connection")
14
15con1.Open "provider=sqloledb.1; data source=192.168.107.102; user id=sa; password=123456; initial catalog=northwind"
16con2.Open "provider=sqloledb.1; data source=192.168.107.97; user id=sa; password=sa; initial catalog=northwind"
17
18' Open Recordset
19Set rs1 = Server.CreateObject("ADODB.Recordset")
20Set rs2 = Server.CreateObject("ADODB.Recordset")
21
22rs1.Open "SELECT * FROM T_ADO1", con1, 1, 3
23rs2.Open "SELECT * FROM T_ADO2", con2, 1, 3
24
25' AddNew Record
26rs1.AddNew
27rs1("num") = Rnd() * 100
28rs1.Update
29
30rs2.AddNew
31rs2("num") = Rnd() * 100
32rs2.Update
33
34' Raise Error Or Not
35If Request.QueryString("error") = 1 Then
36 tempNumber = CInt("abc")
37End If
38
39' If Error Occured, Rollback Transaction
40If Err.Number <> 0 Then
41 ObjectContext.SetAbort
42 Response.Write Err.Number & "<br>" & Err.Description
43Else
44 Response.Write "OK"
45End If
46
47' Close Recordset
48If Not (rs1 Is Nothing) Then
49 rs1.Close
50End If
51If Not (rs2 Is Nothing) Then
52 rs2.Close
53End If
54
55Set rs1 = Nothing
56Set rs2 = Nothing
57
58
59' Close Connection
60If Not (con1 Is Nothing) Then
61 con1.Close
62End If
63If Not (con2 Is Nothing) Then
64 con2.Close
65End If
66
67Set con1 = Nothing
68Set con2 = Nothing
69
70%>