Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Posts
    107

    Unanswered: closing db connection in err handlers.

    I have a database object that has a connect & a disconnect method which other objects can use.

    In my database object (called databroker) my connect & disconnect looks like:

    Function Connect () as boolean
    set goConn = adodb.connection....
    ....
    ..
    goConn.Open strConnectionstring
    End Function

    Function Disconnect() as boolean
    set goConn = Nothing
    end Function

    Often times, I'll do things like:

    Dim objDB as databroker
    set objDB = new databroker
    If objdb.Connect Then
    'do something...
    objDB.Disconnect
    End If
    Exit Function
    ErrHandler:
    Disconnect
    End Function

    What I'm wondering is when the code crashes (for whatever reason) before the disconnect is called, I try to disconnect again in the error handler. Will setting the global connection object to nothing "properly" close my database or should I actully be using goConn.Close ?? If the database didn't get opened and i try to call goConn.Close, will it work?

  2. #2
    Join Date
    Oct 2005
    Posts
    178
    The connection object is just the means for your applicaiton to provide a connection to your datasource which specify the specific data provider and paramenters. It does not open your database.
    The Recordset open/close as implies will do your tables and records.
    In my experience, when the application crashed for one reason or another, the connection seems to stay active until I re-start the applcation. In Access database it's easy to determine because once the database is open it creates a record-locking file and disappear when the connection is closed.
    It is safer I say that the tables be closed first to prevent corruption if your application does not have a roll back when it malfuncitons during data movements.
    Last edited by fredservillon; 12-08-05 at 15:13.

  3. #3
    Join Date
    Feb 2003
    Posts
    107

    so...

    are you saying that unless i explicitly call goConn.Close, the connection will remain open despite setting goConn = Nothing...
    is this correct?

  4. #4
    Join Date
    Oct 2005
    Posts
    178
    I guess i have the same question. Will the database close automatically if the connection closed? I'm sure that this mechanics is considered in the database system design. Or will it disconnect if the database is still open?
    YOu have these sequence of events here.

    first you make connection

    second open the database

    third close the database

    fourth you disconnection or close the connection.

    I guess you can add a verification on your error handler to verify that connection is closed after the variable is set to nothing

    Further.. Your goConn is a variable you declared and assigned to an instance for the class connection, then you have to set it to nothing to vanish the instance when no longer desire to use.

    so the question is. If the instance is set to nothing, will the connection parameter get disconected automatically and all the database tables automatically closed?
    or Does the system allow the the connection instance to be set to nothing when database is currently connected and database is currently opened?
    Last edited by fredservillon; 12-09-05 at 21:25.

  5. #5
    Join Date
    Oct 2004
    Posts
    24

    Try this.

    setting the connection object to "nothing" doesn't mean the object is closed. To
    totally closed the connection object used the "close" method.

    dbConn.Close
    set dbConn = nothing

    dbConn must first set somewhere and before calling the Close method, or it will
    produces and error.

    That's all folks.

Posting Permissions

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