Implementation of database transaction control in ASP program

Source: Internet
Author: User
Tags manual iis insert rollback

In programming, you often need to use transactions. A business is a series of operations that must be successful, and all other steps must be undone as long as one operation fails. For example, using ASP to develop a network hard disk system, the user registration part of the things to do are:

To record user information into a database

Open a folder for users to store

Initialize User action Log

These three steps must use transactions, or in the event of a disk operation failure, but did not undo the database operation, it will cause only landing and can not operate the "dead user" phenomenon.

Because of the special development History of database system, small to access, large to DB2, all with transaction support. The above steps can therefore be expressed as follows:

On Error Resume Next
  第一步:
  在事务环境下把用户信息记入数据库
  If Err Then
   关闭连接
   退出
  Else
   第二步:创建文件夹
  If Err Then
   回滚第一步数据库操作,退出
  Else
   第三步:在事务环境下操作日志数据库
  If Err Then
   回滚第一步操作,删除第二步建立的文件夹
   退出
  End If
 End If
End If
 提交第一步数据库操作的事务
 提交第二步数据库操作的事务
End

Each step needs to be judged, if it fails, you need to manually rollback the previous multi-step operation, so that the program becomes complex and difficult to understand. If you update your program in the future and add additional steps, you will need to nest more layers of If ... Else ... End If, making the program process more complex.

The correct solution is to use the ASP's transaction control capabilities. By contacting the MTS Service, IIS can control multiple systems that support transactions, and when the program issues a "failed" signal, all supporting transactions are automatically rolled back, even if the operation is formally completed, and a convenient manual rollback is provided for operations that do not support transactions. The example above 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 ' notifies all components that support the transaction to roll back and runs 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
Sub OnTransactionCommit
Response.Write "Victory completes the task "
End Sub
%"

The first line of <%@ TRANSACTION = Required%> indicates that this page of ASP files requires MTS transaction support. Each of the operations in the middle is written in a normal order, regardless of the rollback issue. At the end of the program to determine whether there are errors. If there is, call the ObjectContext SetAbort method, IIS notifies all supporting transaction components to rollback (primarily the database) through the MTS service, and runs sub OnTransactionAbort to manually rollback the operations that do not support transactions If no error occurs, the ObjectContext SetComplete method is invoked, and sub OnTransactionCommit is run to display the successful message.

The entire ASP program does not need to judge errors and rollback operations to write redundant code, only to be judged at the end, even if the future increase in multi-step operations, but only in the Sub OnTransactionAbort control can be very convenient, programmers can focus on process writing rather than writing error-correcting code.

In fact, ASP also provides a number of more useful features, waiting for us to use, do not think that ASP use scripting language, the function is necessarily weak.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.