Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

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 !
0
nicolle
Asked:
nicolle
  • 4
  • 4
  • 2
  • +5
3 Solutions
 
jayeshshahCommented:
set the Connection timeout property of the connection object to 3 seconds.

K'Regards

Jayesh
0
 
Ryan ChongCommented:
Try set ConnectionTimeout Property of the Connection object:

conn.ConnectionTimeout = 3
0
 
Ryan ChongCommented:
hmm.., sorry as like repeating what jayeshshah mentioned, just ignore my post above. cheers
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nicolleAuthor Commented:
This is what I try:

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

  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 ?
0
 
_agj_Commented:
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.

0
 
nicolleAuthor Commented:
_agj_,

Please refer to my previous response. Thanks !
0
 
Ryan ChongCommented:
>>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.

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

~ fantasy ~
0
 
dancebertCommented:
>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.

0
 
nicolleAuthor Commented:
ryancys,

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.
0
 
jayeshshahCommented:
check this link i found. it shows a way how to connect to SQL Server. I hope this is helpful to you.

http://www.sqlwarehouse.com/create_a_ado_connection_to_sql.htm
0
 
_agj_Commented:
ignore my post....that was thanks to a small refreshing problm...dint c ur post bfore my posting ;)
0
 
Ryan ChongCommented:
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.
0
 
dancebertCommented:
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.  
0
 
kamlesh_agrawalCommented:
Well!!
Are u using VB6.0 application to connect database with ADO

then,
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

Like

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

rgds
Kamlesh A.

0
 
nicolleAuthor Commented:
Experts,

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=192.168.203.111;" & _
                  "Address=192.168.203.111,1433;" & _
                  "Network=DBMSSOCN;" & _
                  "Database=Test;" & _
                  "Uid=Test;" & _
                  "Pwd=;"

  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
   
    DoEvents
  Loop
 
  If blnConnected Then
    MsgBox "connected"
  Else
    MsgBox "not connected"
    objSQLConn.Cancel
    objSQLConn.Close
  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.
0
 
RainUKCommented:
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.

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now