Transaction processing

xiaoxiao2021-03-06  98

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:

Wire the user information into the database to open a folder for the user to store the initialization user operation log

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 "Victory completed task" End Sub%>

转载请注明原文地址:https://www.9cbs.com/read-102148.html

New Post(0)