Use transaction control in ASP

Source: Internet
Author: User
ASP transaction control function. By contacting the MTS service, IIS can control multiple transaction-supporting systems. Program When a "failed" signal is sent, all systems that support the transaction will automatically roll back, even if the operation has been formally completed; manual rollback is also convenient for operations that do not support transactions:

<% @ 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 transaction rollback 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 "successfully completed the task"
End sub
%>

<% @ Transaction = required %> In the first line indicates that the ASP file on this page requires MTS transaction support. All the operations in the middle are written in the normal order, so you do not need to consider rollback. At the end of the program, determine whether there is an error. If the setabort method of objectcontext is called, IIS will notify all transaction-supporting components (mainly databases) to roll back through the MTS service ), run sub ontransactionabort to manually roll back operations that do not support transactions. If no error occurs, call the setcomplete method of objectcontext to run sub ontransactioncommit to display successful messages.

The entire ASP program does not need to write redundant code for judgment errors and rollback operations. It only needs to make judgment at the end, even if multiple steps are added in the future, you only need to control sub ontransactionabort, which is very convenient. programmers can focus on process writing rather than writing error correction code.

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.