In programming, transactions are often used. The so-called transaction is a series of operations that must all succeed. As long as one operation fails, all other steps must also be undone. For example, using ASP to develop a network hard disk system, the user registration part needs to do the following:
record the user information into the database, open a folder for the user to store the initialization user operation log,
these three steps must use transactions, otherwise in case of disk operation Failure without undoing the database operation will result in a "dead user" phenomenon that can only log in but cannot operate.
Due to the special development history of database systems, everything from Access to DB2 has transaction support. Therefore, the above steps can be expressed as follows:
On Error Resume Next
Step 1:
Record user information into the database in a transaction environment
If Err Then
Close the connection and exitElse
Step 2: Create the folder If Err Then
Roll back the first database operation and exit Else
Step 3: Operate the log database in a transaction environmentIf Err Then
Roll back the first step, delete the folder created in the second step and exit End If
End If
End If
Submit the transaction of the first database operation. Submit the transaction of the second database operation.
End
needs to be judged at every step. If it fails, you need to manually roll back the previous steps, making the program complicated and difficult to understand. If the program is updated in the future and other steps are added, more layers of If...Else...End If will need to be nested, making the program flow more complicated.
The correct solution is to use ASP's transaction control function. By contacting the MTS service, IIS can control a variety of systems that support transactions. When the program sends a "failure" signal, all systems that support transactions will automatically roll back, even if the operation has been officially completed; operations that do not support transactions will also be rolled back. Provides a convenient manual rollback method. The above example is rewritten using the ASP transaction control function as follows:
<%@ TRANSACTION = Required %>
On Error Resume Next
Set Conn=Server.CreateObject("ADODB.Connection")
Conn.Open....
Conn.Execute "INSERT...."
Conn.Close
Set Conn=Nothing
Set Conn2=Server.CreateObject("ADODB.Connection")
Conn2.Open....
Conn2.Execute "INSERT...."
Conn2.Close
Set Conn2=Nothing
Set FSO=Server.CreateObject("Scripting.FilesystemObject")
FSO.CreateFolder "...."
If Err Then
ObjectContext.SetAbort 'Notify all components that support transactions to rollback and run manual rollback code
Else
ObjectContext.SetComplete
End If
Set FSO=Nothing
Sub OnTransactionAbort
Response.Write "Error"
FSO.DeleteFile Server.Mappath("a.txt") 'FSO manual rollback - delete folder
End Sub
SubOnTransactionCommit
Response.Write "Complete the task successfully"
End Sub
%>
The <%@ TRANSACTION = Required %> in the first line indicates that the ASP file of this page requires MTS transaction support. All operations in the middle are written in a normal order without considering rollback issues. Determine whether there are errors at the end of the program. If so, call the ObjectContext's SetAbort method. IIS will notify all components that support transactions through the MTS service to roll back (mainly databases), and run Sub OnTransactionAbort to manually roll back operations that do not support transactions; if no errors occur, call the ObjectContext's SetComplete method, Sub OnTransactionCommit will be run to display a success message.
The entire ASP program does not need to write redundant code for error judgment and rollback operations. It only needs to be judged at the end. Even if multi-step operations are added in the future, it only needs to be controlled in Sub OnTransactionAbort. It is very convenient and programmers can Focus on procedural writing rather than writing error-correcting code.
In fact, ASP also provides many more useful functions, waiting for us to use them. Don't think that because ASP uses scripting language, its functions must be weak.