The author is the Star of Microsoft Community in February Microsoft China Community Star
In programming, you often need to use transactions. The so-called transaction is a series of operations that must be successful, as long as there is a step failure, all other steps must also be revoked. For example, use ASP to develop a network hard disk system, and its user's registration section is:
Make user information into the database to open a folder for users to store initialized user operation logs
These three steps must be used in transactions, otherwise the disk operation failed, and without undoing the database operation, it will cause only "dead users" phenomenon that can only be landed without being able to operate. Due to the special development history of the database system, small to Access, large to DB2, have no business support. Therefore, the above steps can be represented as follows: On Error Resume Next: In the transaction environment, use the user information into the database if Err THEN Turn off the connection Exit ELSE Step 2: Create a Folder If Err Then Roll Rolling First Database Operation, Exit ELSE Step 3: Operating Log Database if Err Then Roll Rolling in the Transaction Environment, Deleting the Second Step Document End If End Ifnd IF Submit Transaction Submitting a Transaction for Transactions Submitting the Second Step Database Operation transaction end
Every step needs to be judged. If it fails, you need to manually return multiple steps in front, so that the program is complicated, it is difficult to understand. If the program is updated in the future, add additional steps, and need nesting more layers of IF ... Else ... END IF, making the program process more complicated.
The correct solution is to use the ASP's transaction control function. IIS passes through and the MTS service, you can control a system that supports transaction. When the program issues a "failed" signal, all systems that support transactions will automatically roll back, even if the operation is formally completed; do not support transaction operations It provides a convenient manual rollback method. The above example rewrites with 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.closset conn = Nothing
Set conn2 = server.createObject ("adodb.connection") conn2.open .... conn2.execute "insert ...." conn2.closset conn2 = Nothing
SET FSO = Server.createObject ("scripting.filesystemObject") fso.createfolder "..."
If Err Then ObjectContext.Setabort 'Informs All components that support transactions rollback, and run handmade rollback code else ObjectContext.setCompleteEnd iFset Fso = Nothing
Sub ontransactionabortresponse.write "Error" fso.deletefile server.mappath ("a.txt") 'FSO manual rollback - delete folder end subs ontransactioncommitresponse.write "End Sub%>" End Sub%> "End Sub%" % @ Transaction = Required%> means this page ASP file requires MTS transaction support. The various operations in the middle are written in ordinary order without considering rollback problems. In the final judgment, there is an error. If there is, call ObjectContext's setabort method, IIS will notify all component rollbacks (mainly databases) through the MTS service, and run Sub OntransactionAbort to hand-roll back to operations that do not support transactions; if no errors have occurred, call ObjectContext SetComplete method, run Sub OntransactionCommit to display successful messages. The entire ASP program does not need to write extra code for judgment errors and rollback operations, and only need to be judged, even in the future, it is only necessary to control in Sub Ontransactionabort, which is very convenient, programmers Focus on process writing rather than writing error correction code. In fact, the ASP also provides a number of more useful features, waiting for us, don't think that ASP uses scripting languages, the function is so weak.