ADO connection time out

How do I create a function to test a db connection in ADO in
3 seconds ? I don't see there is any properties in ADO allows me to do that.

My purpose is doing db failover test. If the primary db fail to connect, then
I will use secondary db. Thus, time is very critical for me, I need the function
to return false, if it cannot connect to primary db in 3 seconds. I will test the failover by turning off SQL server and unplug the cable.

Thanks !
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

set the Connection timeout property of the connection object to 3 seconds.


Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Try set ConnectionTimeout Property of the Connection object:

conn.ConnectionTimeout = 3
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
hmm.., sorry as like repeating what jayeshshah mentioned, just ignore my post above. cheers
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

nicolleAuthor Commented:
This is what I try:

strConnString = "Driver={SQL Server};" & _
                  "Server=;" & _
                  "Address=,1433;" & _
                  "Network=DBMSSOCN;" & _
                  "Database=Test;" & _
                  "Uid=Test;" & _

  objSQLConn.ConnectionTimeout = 3
  objSQLConn.Open strConnString

when I unplug the cable or shutdown the server that hosts SQL server, I tried to connect
10 times, only 2 out 10 have timed out in about 3 seconds, 8 times timed out more than 10 seconds. What happen ?
u cud write a connect function

with the

Dim db As ADODB.Connection
db.ConnectionTimeout = 3 (not sure if it is sec/millisecs)

after the .connect statement, u cud check for the connection state.

OR else, if it throws an error on not connecting, use the error code.

nicolleAuthor Commented:

Please refer to my previous response. Thanks !
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>I tried to connect
10 times, only 2 out 10 have timed out in about 3 seconds, 8 times timed out more than 10 seconds. What happen ?

Maybe your system is hang when try to establish a connecting, it may not consider as a connecting timeout problem.

And personally think that 3 seconds for establishing a connecting may not enough, you may increase the figure to greater number.

something like this,
if objSQLConn.State = adstateClosed then
   'failure in connect
end if

~ fantasy ~
>What happen ?
William Vaughn discusses this 'feature' of ADO timeout in his ADO book, if you have it.  I have it, but it's at work, otherwise I'd post the explanation.  I'll post it tomorrow if nobody beats me to it

What database are you using?  Pardon my ignorance, but I thought the main point of clustering was to get automatic failover so the programs would continue accessing the database as if nothing had happened.  Perhaps you're trying to do something in the front end that is better done in the back end.

nicolleAuthor Commented:

Actually, I am doing the DB failover testing. If the primary SQL server is down, then I am going to secondary SQL server. Do you have much better idea ? The timing is very critical here.
I have a very fast connection. At most, the connection timeout is 5 seconds.
check this link i found. it shows a way how to connect to SQL Server. I hope this is helpful to you.
ignore my post....that was thanks to a small refreshing problm...dint c ur post bfore my posting ;)
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
You will need to wait for the response from the connecting process, i think we cant speed up the response time from the connecting process.
ConnectionTimeout starts when a LAN connection is established to the database server and ends when the data source provider creates a connection.  This means that that the network component is NOT part of the timing.  ADO time-out code is blocked until the network answers - if it ever does answer. Pull the plug and you're dependent on the timout of your NIC card, which is not adjustable.

ConnnectionTimeout also does not account for the time ADO and low-level providers require to connect to the LAN hardware.  
Are u using VB6.0 application to connect database with ADO

you first use a timer and set the interval to 1 second i.e 60000 milliseconds

and then keep a counter and increase in timer's subroutine


dim countme as integer
dim tryconnection as integer

Private sub form_load
countme = 0
end sub

Private sub urtimer_timer()

countme = countme + 1

if (countme mod 3) = 0 and (countme < 7) then
 select case countme
   case 3:
           server = "primary db"
           uid = ""
           pwd = ""
   case 6:
          server = "secondary db"
           uid = ""
           pwd = ""
 end select
 'Call the procedure to connect
 Call connectme()
end if

End sub

'Procedure to connect
Private sub connectme()
      'Your code to connect to db
End Sub

Hope it would help u

Kamlesh A.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nicolleAuthor Commented:

I have found the below code somewhere in the WWW:

  Dim objSQLConn As New ADODB.Connection
  Dim objSQLRs As New ADODB.Recordset
  Dim strConnString As String
  Dim blnConnected As Boolean
  Dim intCount As Integer
  intCount = 1
  strConnString = "Driver={SQL Server};" & _
                  "Server=;" & _
                  "Address=,1433;" & _
                  "Network=DBMSSOCN;" & _
                  "Database=Test;" & _
                  "Uid=Test;" & _

  objSQLConn.Open strConnString, , , ConnectOptionEnum.adAsyncConnect

  Do Until intCount >= 3
    If objSQLConn.State = ObjectStateEnum.adStateOpen Then
      blnConnected = True
      Exit Do
    End If
    Pause 1
    intCount = intCount + 1
  If blnConnected Then
    MsgBox "connected"
    MsgBox "not connected"
  End If

I think this code really help me out. The only problem is when I closed the program, it is still
remain in the memory for about 10 seconds. I am thinking objSQLconn.Cancel is not working right.
Well I agree with Dancebert regards to the LAN issue.

Rather than attempting to connect, straight away you could try using ICMP ping method (Returns in about 0.5 second). At least this way you know that when you Conn.Open you will at least attempt to connect to the machine, rather than wait for a packet to return on the LAN.

As for using cancel method, you could just nuke the object by using Set objSQLConn = Nothing or the horrible but sometimes useful End statement. Which will nuke your program.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.