Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to set a recordset.open timeout???

Posted on 2001-06-22
11
12,640 Views
Last Modified: 2007-11-27
I have an application that generates queries dinamicaly and some of them are huge. I'd like to set a time limit for the queries.

I'm using ADO recordsets to run the queries in SQL 7. I've tried to set the CommandTimeout property for the connection i use in the Recordset.Open Method, but it didn't work.

Here is my test:

    Set con = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
   
    con.CommandTimeout = 5
    con.CursorLocation = adUseClient
    con.Open "Provider=SQLOLEDB.1;Password=*****;Persist Security Info=True;User ID=USR;Initial Catalog=DB;Data Source=Server"

    rs.Open "select * from table", con, adOpenStatic, adLockReadOnly

The query runs for several seconds, even if i have set the CommandTimeout to 5 seconds.
0
Comment
Question by:RobertoTY
11 Comments
 
LVL 10

Expert Comment

by:arana
ID: 6218977
i think the commandtimeout property is for settin a timeout before command STARTS executing (due to network traffic or slow connection)
but if the query already began to run, then the commandtimeout does not apply.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6219247
The Connection CommandTimeout property only affects the Connection Execute method.  So that probably explains the reason it has no effect.

One solution would be to use a Stored Prodecure and set the CommandTimeout property for the Command object.

Hope this helps,

Anthony
0
 

Author Comment

by:RobertoTY
ID: 6219839
I think that even using the connection.execute method, I face the same problem. I've made the following test and the query remains running even after the time estipulated for timeout.

    Dim con As ADODB.Connection
    Dim rs As ADODB.Recordset

    Set con = CreateObject("ADODB.Connection")
    con.CursorLocation = adUseClient
    con.Open "Provider=SQLOLEDB.1;Password=****;Persist Security Info=True;User ID=USR;Initial Catalog=DB;Data Source=SRV"
    con.CommandTimeout = 3

    Set rs = con.Execute("select * from TABLE")


I think there must be another way to set a timeout for these queries, since this property should be for something like an Idle timeout.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6219992
Roberto,

I could be wrong about this, but I believe the Connection CommandTimeout property needs to be specified prior to the Open.

Have you tried this and if so does it have any effect?

Suerte,
Anthony
0
 

Author Comment

by:RobertoTY
ID: 6220057
Extracted from msdn:

On a Connection object, the CommandTimeout property remains read/write after the Connection is opened.

It doesn't work either.

I discovered that recordset has a dinamic property:
Command Time Out
But it didn't work as well.

0
 

Author Comment

by:RobertoTY
ID: 6220078
Extracted from msdn:

On a Connection object, the CommandTimeout property remains read/write after the Connection is opened.

It doesn't work either.

I discovered that recordset has a dinamic property:
Command Time Out
But it didn't work as well.

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6220107
Roberto,

Yes, I know I spoke too soon:  I checked the CommandTimeout and in my case it works as advertised, placed prior to the Connection open or after.  Basically it triggers a Timeout expired (-2147217871).

I am not sure why it is not working in your case.  The only other thing I can suggest is that you try out the CommandTimeout property of the Command object.  Perhaps you will have better luck.

Anthony
0
 

Author Comment

by:RobertoTY
ID: 6220155
acperkins,

I am very curious to see the code you said that works for you. Can you send me the code sample you used? Maybe i'm doing something wrong.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 50 total points
ID: 6220223
I very much doubt my code is any different to yours.  By the way I am using ADO 2.5.

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim SQL As String

SQL = "SELECT ... " 'SQL statement that is guaranteed to take longer than 1 second

'Next line was originally Set cn = New ADODB.Connection
'but changed it to match your code
Set cn = CreateObject("ADODB.Connection")
With cn
   '.CommandTimeout = 1
   .ConnectionString = "Provider=SQLOLEDB.1;Password=*****;Persist Security Info=True;User ID=USR;Initial Catalog=DB;Data Source=SQLServer"
   .Open
   .CommandTimeout = 1
   Set rs = .Execute(SQL, , adCmdText)
   rs.Close
   .Close
End With

Set rs = Nothing
Set cn = Nothing
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139699
Hi RobertoTY,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept acperkins@devx's comment(s) as an answer.

RobertoTY, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7173998
Comment from expert accepted as expert

Computer101
E-E Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question