惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 董晓涛

如何将存储过程执行后的结果集放入临时表 SQL Server 2005 symmetric encrytion sample 如何解决:Error 14274: 无法添加、更新或删除从MSX服务器上发起的作业. Useful Links(to Learn SQL Server) Encrypting Data With the SQL Server Encrypt Function Convert IP To Numberic Generate table structure Generate Time Dim Generate Insert data script on a table. - 董晓涛 Execute T-SQL asynchronously Don't Use Select * Sql Server数据库被置疑后解决方法 SQL Server 2005中对BLOB的支持(ntext,text and image) SQL Server 2000 Service Pack 4 is released 数据规范化 Microsoft SQL Server 2005 and Vs.net 2005 April 2005 Version XML IN 20 MINUTES! CLR Integrated in SQL Server 2005 XQuery in SQL Server 2005
Detect SQL timeout from ASP & Issue RollbackTrans
董晓涛 · 2005-07-29 · via 博客园 - 董晓涛
Detect SQL timeout from ASP & Issue RollbackTrans

I developed an application that worked fine in development and single user testing, but when multiple users starting using the application, I ran into some cases when users got timeout errors from ASP while the code was trying to execute a long SQL stored procedure.   This long stored procedure uses a transaction because it involves a lot of updates, inserts, and deletes, and if any statement fails, then all of the statements must be rolled back to the state that they were in before the transaction began.   When the timeout was occurring from ASP, the data in my database was getting messed up because some of the statements in the transaction were being executed but then the transaction was just stopped in the middle of execution without being rolled back.

I wanted to find out a way to detect when a

timeout occurs so that I could then manually tell the stored procedure to rollback the transaction.   I started searching for how to do this, and I found that I couldn't find much information on this topic.  

I started out trying to use the

LOCK_TIMEOUT value in the SQL Server stored procedure on all of my SQL statements and to then detect if this was exceeded.   However, I learned that if the LOCK_TIEMOUT value is exceeded, your transaction skips the blocked operation and receives trappable error number 1222 with the message "Lock request time out exceeded."  In this situation, the blocked statement is cancelled and the rest of the transaction continues as soon as a locking conflict occurs.   A transaction isn't rolled back if you exceed the lock timeout.  Exceeding the timeout skips the blocked operation, but any remaining SQL statements in the transaction still execute.  You need to trap for error 1222 and take appropriate action if you want to roll back any user-defined transactions.   This was unacceptable to me because it is too much of a hassle to write your own error-detection code after each statement in your stored procedure and then to write the code to rollback the transaction.  

Well, I finally was able to figure out how to detect the

timeout and to rollback the transaction from the ASP page in the case that a timeout does occur.   It is kind of a roundabout way of doing things, but it works.   I still declare a transaction inside my stored procedure and use the normal method of detecting errors and conditions where I need to rollback the transaction from within the stored procedure.   However, I also begin a transaction from within the ASP page by calling adoConn.BeginTrans in my ASP code.   Then I set a CommandTimeout value for the maximum time I want to allow for this stored procedure to execute, and if the ADO command takes longer than  this specified time, I check for the timeout error and if it exists, then I rollback the transaction from the ASP code with RollbackTrans.   Else, I go ahead and commit the transaction with CommitTrans.

It took me a lot of research and testing to figure this out, but it now works perfectly!  Just thought I would pass along what I learned.   Here is a code excerpt.
-----------------------------------------------------
Dim colErrs, objError,

timeout, adoConn, adoCmd
timeout=False
Set adoConn = Server.CreateObject("ADODB.Connection")
adoConn.CursorLocation = adUseClient
adoConn.Open Session("UserConnStr")
adoConn.IsolationLevel = adXactReadUncommitted
'The next line starts a transaction
adoConn.BeginTrans
Set adoCmd = Server.CreateObject("ADODB.Command")
'I added this to give the command 45 seconds to execute.
adoCmd.CommandTimeout = 45
adoCmd.ActiveConnection = adoConn
adoCmd.CommandText = "sproc_RecalculateQuantities"
adoCmd.CommandType = adCmdStoredProc
adoCmd.Execute
Set colErrs=adoConn.Errors
If adoConn.Errors.Count <> 0 then
     For Each objError In colErrs
         'This is the error number for a timeout.
         If objError.Number=-2147217871 Then
             adoConn.RollbackTrans
             Response.Write "The query timed out before finishing.  Please try again." 
             timeout=True
             adoConn.Errors.Clear
             Exit For
         End If
     Next
End If
Set adoCmd = Nothing

If Not

timeout Then
    adoConn.CommitTrans
End If
adoConn.Close
Set adoConn = Nothing

-------------------------------------------------------
Hope that this helps you.   I know that I will be using this code a lot from now on for my most complex stored procedures involving transactions.