Results 1 to 12 of 12

Thread: Connection Object and Pooling

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Connection Object and Pooling

    Hi all – this should be an easy question:

    I have read that when you call a connection objects close() method, the object is not actually closed, but put into the pool (default is for 60 seconds). I have also read that when you call dispose() that close() is called implicitly. Does dispose() put the connection in the pool, or does it totally destroy the object.

    Thanks

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Connection Object and Pooling

    MSDN Help
    The Close method rolls back any pending transactions. It then releases the connection to the connection pool, or closes the connection if connection pooling is disabled. If Close is called while handling a StateChange event, no additional StateChange events are fired.
    What we generally do is : Close the connection , Dispose it without running after these details .

  3. #3

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    I want to know if I need to open another connection if I call dispose, or if it will still exist.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Why you don't declare the connection obj as static to use it anywhere while your program is running . I'm doing this right here without any problems so far . One connection object is more better than having multiple ones .

  5. #5

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Then you only close() and open() it? You don't call dispose()?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Sorry missed that part ! This is what I have under my exit button .

    VB Code:
    1. MyConnection.Close()
    2. MyConnection.Dispose()
    3. Application.Exit()

  7. #7

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    My question is a result of an apparent difference between VS.NET 2002 and 2003.

    In VS.NET 2002 I have something like:
    VB Code:
    1. Public Class DB
    2.  
    3. Private m_CnnConnection As New SqlConnection("myConnectionStringHere")
    4.  
    5. Public Function GetData1(...)...
    6.  
    7. Connection().Open()
    8. 'use the connection in here
    9. Connection().Close()
    10. Connection().Dispose()
    11.  
    12. End Function
    13.  
    14. Public Function GetData2(...)...
    15.  
    16. Connection().Open()
    17. 'use the connection in here
    18. Connection().Close()
    19. Connection().Dispose()
    20.  
    21. End Function
    The calls that I made to are something like:
    VB Code:
    1. Dim objDB as New DB(...)
    2. objDB.GetData1
    3. objDB.GetData2

    In VS.NET 2002 this code worked fine. I am assuming it was due to Connection pooling, since the call to GetData2 clearly tries to use a disposed connection object.

    In VS.NET 2003 this code does not work after the GetData1 calls the dispose().

    Any ideas why?

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Well , you could have told me before that you're talking about VS.NET 2003 ! . I dunno about it . But something lik this will work just fine with both versions . I think :

    Any ways , I like working with static objects (not always though) . Some other times , I do some inheritance stuff (overriding , deriving etc )
    This
    VB Code:
    1. Private Shared My_Path As String = Application.StartupPath & "\mydb.mdb"
    2.  
    3. Private Shared My_Password As String = "anything"
    4.  
    5. Private Shared My_Connection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyPath & ";Jet OLEDB:Database Password=" & MyPassword)
    6.  
    7.  
    8. Public Shared Property MyPassword() As String
    9.             Get
    10.                 Return My_Password
    11.             End Get
    12.             Set(ByVal Value As String)
    13.                 My_Password = Value
    14.             End Set
    15.         End Property
    16.  
    17. Public Shared ReadOnly Property MyConnection() As OleDbConnection
    18.             Get
    19.                 Return My_Connection
    20.             End Get
    21.         End Property
    22.  
    23. Public Class OpenDB
    24.             Public Shared Sub OpenDB()
    25.                 If Not MyConnection.State = ConnectionState.Open Then
    26.                     MyConnection.Open()
    27.                 Else
    28.  
    29.                 End If
    30.             End Sub
    31.         End Class
    32.  
    33.  
    34.         Public Class CloseDB
    35.             Public Shared Sub CloseDB()
    36.                 If MyConnection.State = ConnectionState.Open Then
    37.                     MyConnection.Close()
    38.                 Else
    39.  
    40.                 End If
    41.             End Sub
    42.         End Class
    at the end I dispose my objects using either the Dispose of the form or from anywhere before closing event .

    Hope that helps !

  9. #9

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    [solved]

    Turns out I was thinking about it wrong. The connection pool is handled outside of your code, so anything you do in code - like connection.Dispose() does NOT remove it from the connection pool. You can set parameters to not allow your connection to be pooled, or set the timeout for pooled connections, etc.

    So basically, you must call Close() or Dispose() to have the connection put back into the pool. Dispose() does NOT remove it from the pool, it only releases resources on the client side.

  10. #10
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    The connection pool is handled outside of your code, so anything you do in code - like connection.Dispose() does NOT remove it from the connection pool. You can set parameters......
    What you said is a known subject. But you didnt describe why it was different in VS.NET 2003 (Framework 1.1) and VS.NET 2002 (Framework 1.0).
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  11. #11

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Not really sure why. VS.NET 2002 let me use the code I posted above without error. VS.NET 2003 caught it and burped at me. VS.NET 2002 should have burped too - as I was clearly trying to use a disposed connection object in the second call. Could have been a bug that I exploited in 2002

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It seems we're going to have our VB.NET2002 Code with lots of errors in VB.NET2003 . . BTW , Does it come with any code converter or upgrader ?

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