Results 1 to 4 of 4

Thread: SQL connection state and setting timeout

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    SQL connection state and setting timeout

    I am using ADODB code to connect to a SQL Server 2005 database from my VB6 program. The connection is opened in the following :

    Code:
        
    
    Public g_SQL_Con As New ADODB.Connection
    
    Public Function OpenSQLConnection() As Boolean
    '**************************************************************************
    ' Purpose   :   Initializes the SQL connection
    '
    '**************************************************************************
    
        g_SQL_Con.ConnectionString = _
            "Provider=sqloledb;" & _
            "Data Source=" & g_strSQLServerIP & ";" & _
            "Initial Catalog=catalog;" & _
            "User ID= user;" & _
            "Password=password;" & _
            "Connection Timeout=10"
            
        g_SQL_Con.Open
    
    End Function
    When this is completed the g_SQL_Con.State = 1. Throughout the program if I need to execute a command I build the command string and then test g_SQL_Con.State to see if it is 1. Then the command is executed. If not 1 then a call is made to the function OpenSQLConnection.

    This all works fine as long as the server is available after the initial g_SQL_Con.Open. If, for some reason, the network is down, the server has stopped, etc., my g_SQL_Con.State doesn't seem to change. It stays at 1. An attempt to execute the command is made, the system hangs for a good 30-40 seconds and an error occurs. I trap the error and execute g_SQL_Con.Close to correct the state. Next time a call is requested it sees the need to open the connection and calls OpenSQLConnection.

    My questions are :

    1. If the server isn't available when executing g_SQL_Con.Open the system hangs for 30-40 seconds no matter what I set Connection Timeout to.
    2. If the connection state is incorrectly reported as 1 my command execution hangs for 30-40 seconds no matter what I set Command Timeout to.
    3. Should I be opening and closing the connection each time I need to access the server or continuing to open it and leave it open as long as the program is running?
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: SQL connection state and setting timeout

    I open and close connections each call
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: SQL connection state and setting timeout

    1 - are you SURE it's the CONNECTIONTimeout you're setting? It's a setting on the Connection object... set it directly, not through the connection string.
    2 - the COMMANDTimeout only affects how long it will wait for a command to execute before bailing... it has nothing to do with the connection and if there is no connection, the command can't be executed so the CommandTimeout is moot.
    3 - Yes - open the connection, do what you need to do, which could be one query or a series of commands, then close the connection and get out. This way you should be in a known state each time. You don't (shouldn't) have to guess what the connection state is.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: SQL connection state and setting timeout

    I started by using g_SQL_Con.ConnectionTimeout = 10 first. Still hanging 30-40 seconds before error. Then tried putting it in the connection string as shown. Seemed to be same results either way.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width