Results 1 to 16 of 16

Thread: Where to release the ADO in a DLL

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55

    Where to release the ADO in a DLL

    in the ActiveX DLL, I add a module, in the module I add the sub_main, and create the ADODB.Connetion.
    when my programs used the DLL first time, it will run the sub_main, but when I close the program, I want to set g_Conn = nothing, but I can't find the procedure to do, Does the program release it automatic?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    It does release the connections, but if you want to stay on the safe side, you can put that code in the class_terminate() event.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55
    if my DLL have 2 Class Module, all of the Class Module will the g_conn,if one of the Class Module use the class_terminate() event to release the g_conn, another one will wrong.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    HUH?? Oh... ok...

    The very fact that you have two g_conns in your DLL implies that they are private in scope to each module. There shouldn't be a problem when you close these in the class_terminate() event.

    However, if it still scares you, you can create a procedure that closes everything for you, and call this procedure in your form's Query Unload event.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55
    in fact, In my program, I neet 3-5 class module, and my program maybe will have 10 users, this means, if they are open my program, maybe I create 30 - 50 connection.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Dude, that shouldn't matter. For each user, you'll be having a separate instance of the class. Each object, upon termination will call the code to close the ADO connections. That's the basic concept behind OOPs!

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55
    But I think if I create the instance, I have to open the connection, and I release the instance,It will close the connection, but somebody told me:I will waste time to wait it open, and I will waste time to wait it close!How can I do?I think maybe use the COM+ is better!

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    COM+ is nothing to do with what you are trying top achieve. The time to open/close a connection is neglegable as the connections are pooled in memory.
    Don't create the connection in Sub Main, just create the connection when u need it, and then destroy it as soon as u have finished with it.

    Woak

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55
    if I am not use COM+, client always open/close connection, is a good choose? the connection will pooled in memory?

  10. #10

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Your friend has obviously given you incorrect information. You can safely do as we've told you in this thread.

    Trust us.

    Sincerely,
    Frog and Badger.

  12. #12
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Here is what I use in my database module:
    I use the property DBConnection to get the connection object.
    If I am using tranasctions then I use BeginTranascation before I get the DBConnection.
    Hope it makes sense.

    Woka

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const CLASS_NAME As String = "modDatabase"
    4.  
    5. Private Const DB_CONN As String = "Provider=SQLOLEDB.1;Svr Security Info=False;User ID=MBELots;Password = MBELots;Initial Catalog=MBELots;Data Source=MESDEV"
    6.  
    7. Private madoConn        As Connection
    8. Public gstrUserUID      As String
    9.  
    10. Private mlngCount       As Long
    11.  
    12. Private Property Get DBConnString(ByVal pblnAuthenticationReq As Boolean) As String
    13. On Error GoTo ErrHandler
    14.     If Len(gstrUserUID) = 0 And pblnAuthenticationReq Then
    15.         Err.Raise vbObjectError, CLASS_NAME, "Server authentication failed, user not logged in."
    16.     End If
    17.     DBConnString = DB_CONN
    18.     Exit Property
    19. ErrHandler:
    20.     HandleError CLASS_NAME, "DBConnString", Err
    21. End Property
    22.  
    23. Public Property Get DBConnection(ByVal pblnAuthenticationReq As Boolean) As Connection
    24. Dim adoConn     As Connection
    25. On Error GoTo ErrHandler
    26.     If madoConn Is Nothing Then
    27.         Set adoConn = New Connection
    28.         adoConn.CursorLocation = adUseClient
    29.         adoConn.Open DBConnString(pblnAuthenticationReq)
    30.     Else
    31.         Set adoConn = madoConn
    32.     End If
    33.     Set DBConnection = adoConn
    34.     Set adoConn = Nothing
    35.     Exit Property
    36. ErrHandler:
    37.     Set adoConn = Nothing
    38.     HandleError CLASS_NAME, "DBConnection", Err
    39. End Property
    40.  
    41. Public Property Get DateTimeSnapshot() As Date
    42. Static dteDate     As Date
    43.     If dteDate = 0 Then
    44.         dteDate = Now
    45.     End If
    46.     DateTimeSnapshot = dteDate
    47. End Property
    48.  
    49. Public Property Get TransactionUID() As String
    50. Static strUID   As String
    51. Dim objFuncs    As VBTools.GeneralFunctions
    52. On Error GoTo ErrHandler
    53.     If Len(strUID) = 0 Then
    54.         Set objFuncs = New VBTools.GeneralFunctions
    55.         strUID = objFuncs.CreateUID
    56.         Set objFuncs = Nothing
    57.     End If
    58.     TransactionUID = strUID
    59.     Exit Property
    60. ErrHandler:
    61.     Set objFuncs = Nothing
    62.     HandleError CLASS_NAME, "TransactionUID", Err
    63. End Property
    64.  
    65. Public Property Get TransactionIndex() As Long
    66. Static lngIndex     As Long
    67.     lngIndex = lngIndex + 1
    68.     TransactionIndex = lngIndex
    69. End Property
    70.  
    71. Public Sub BeginTransaction()
    72. On Error GoTo ErrHandler
    73.     If mlngCount = 0 Then
    74.         Set madoConn = New Connection
    75.         madoConn.CursorLocation = adUseClient
    76.         madoConn.Open DBConnString(True)
    77.         madoConn.BeginTrans
    78.     End If
    79.     mlngCount = mlngCount + 1
    80.     Exit Sub
    81. ErrHandler:
    82.     HandleError CLASS_NAME, "BeginTransaction", Err
    83. End Sub
    84.  
    85. Public Sub CommitTransaction()
    86. On Error GoTo ErrHandler
    87.     If mlngCount = 0 Then
    88.         Err.Raise 1
    89.     End If
    90.     mlngCount = mlngCount - 1
    91.     If mlngCount = 0 Then
    92.         madoConn.CommitTrans
    93.         madoConn.Close
    94.         Set madoConn = Nothing
    95.     End If
    96.     Exit Sub
    97. ErrHandler:
    98.     HandleError CLASS_NAME, "CommitTransaction", Err
    99. End Sub
    100.  
    101. Public Sub RollbackTransaction()
    102.     If mlngCount = 0 Then
    103.         Err.Raise 1
    104.     End If
    105.     mlngCount = mlngCount - 1
    106.     If mlngCount = 0 Then
    107.         madoConn.RollbackTrans
    108.         madoConn.Close
    109.         Set madoConn = Nothing
    110.     End If
    111. End Sub
    112.  
    113. Public Sub AddEventLog(ByVal pstrItemUID As String, ByVal pstrItem As String, ByVal pstrAction As String)
    114. Dim objEvent    As EventLogSvr
    115. On Error GoTo ErrHandler
    116.     Set objEvent = New EventLogSvr
    117.     objEvent.AddLog gstrUserUID, pstrItemUID, pstrItem, pstrAction
    118.     Set objEvent = Nothing
    119.     Exit Sub
    120. ErrHandler:
    121.     Set objEvent = Nothing
    122.     HandleError CLASS_NAME, "AddEventLog", Err
    123. End Sub

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55
    Oh, I am sorry, i made a mistake.My english is poor, so I always make mistake, I am so sorry!

    Originally posted by xunxm
    if I am not use COM+, client always open/close connection, is a good choose? the connection will pooled in memory?
    I want to say:If I create the ActiveX DLL, used in client computer, not used in server(I read some book told me, server can create the Connection Pool,when a lot user use the connection, server can reload the connection quickly),Does Client can create the pool like server, and load it quickly.

    in the DLL, I add some method like this.
    VB Code:
    1. Public Sub DeleteRecord()
    2.    Dim Conn as New ADODB.Connection
    3.    Dim strSQL as String
    4.    ..........
    5.    Set Conn = Nothing
    6. End Sub
    7.  
    8. Public Sub GetRecord() as Recordset
    9.    Dim Conn as New ADODB.Connection
    10.    .....
    11.    Set Conn = Nothing
    12. End Sub
    Can I neglect the time of "Dim conn......"?
    thanks a lot.
    thank mendhak and Wokawidget, thank you very much.

  14. #14
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Yes, I think it takes something like 20ms to create a connection. Neglegable really.
    What I would do, is instead of creating the connection in each sub I would have:
    VB Code:
    1. 'In a module
    2. Option Explicit
    3.  
    4. Private Const CONN_STRING As String = "Your DB Connection String"
    5.  
    6. Public Function DBConnection() As ADODB.Connection
    7. Dim adoConn   As ADODB.Connection
    8.    Set adoConn = New ADODB.Connection
    9.    With adoConn
    10.       .CursorLocation = adUseClient
    11.       .Open CONN_STRING
    12.    End With
    13.    Set DBConnection = adoConn
    14.    Set adoConn = Nothing
    15. End Function
    Then in your subs use:
    VB Code:
    1. Public Sub DeleteRecord()
    2. Dim adoConn   As ADODB.Connection
    3.    Set adoConn = DBConnection
    4.    'Blah Blah Blah
    5. End Sub
    Or for your select code you can use:
    VB Code:
    1. Public Sub FetchRecord()
    2. Dim adoRec   As ADODB.Recordset
    3.    Set adoRec = New Recordset
    4.    adoRec.Open "SELECT * FROM Table", DBConnection
    5.    'Blah Blah Blah
    For your fetch function above you don't even need to create a connection. If you make the CONN_STRING PUBLIC instead of PRIVATE then you can do:
    VB Code:
    1. Public Sub FetchRecord()
    2. Dim adoRec   As ADODB.Recordset
    3.    Set adoRec = New Recordset
    4.    adoRec.Open "SELECT * FROM Table", CONN_STRING
    5.    'Blah Blah Blah
    Hope that helps,

    Woka

  15. #15
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    VB Code:
    1. Public Sub SQLOpen(ByRef oCn As ADODB.Connection)
    2.  
    3.     On Error GoTo ERR_GetConnectionString
    4.    
    5.     Const REGISTRY_LOCATION As String = "SOFTWARE\MyCompany\DataAccess"
    6.     Const DEFAULT_CONNECTION_STRING As String = "Provider=%1;Password=%2;User ID=%3;Data Source=%4"
    7.    
    8.     Dim oSPM As SharedPropertyGroupManager
    9.     Dim fAlreadyExists As Boolean
    10.     Dim oGroup As SharedPropertyGroup
    11.     Dim oSQLCnString As SharedProperty
    12.     Dim sProvider As String
    13.     Dim sServer As String
    14.     Dim sDatabase As String
    15.     Dim sUsername As String
    16.     Dim sPassword As String
    17.     Dim SQLConnectionString As String
    18.  
    19.     '***************************************************************
    20.     '* Get the shared property (create it if it doesn't exist . . .
    21.     '***************************************************************
    22.     Set oSPM = New SharedPropertyGroupManager
    23.     Set oGroup = oSPM.CreatePropertyGroup("ConnectionStrings", LockMethod, Process, fAlreadyExists)
    24.     Set oSQLCnString = oGroup.CreateProperty("SQLConnectionString", fAlreadyExists)
    25.    
    26.     '*****************************************************************************************
    27.     '* If we don't have this shared property, then get the information from the registry . . .
    28.     '*****************************************************************************************
    29.     If Not fAlreadyExists Then
    30.    
    31.         Call Registry.GetValue(HKEY_LOCAL_MACHINE, REGISTRY_LOCATION, "SQLProvider", sProvider)
    32.         Call Registry.GetValue(HKEY_LOCAL_MACHINE, REGISTRY_LOCATION, "SQLDatabase", sDatabase)
    33.         Call Registry.GetValue(HKEY_LOCAL_MACHINE, REGISTRY_LOCATION, "SQLServer", sServer)
    34.         Call Registry.GetValue(HKEY_LOCAL_MACHINE, REGISTRY_LOCATION, "SQLUser", sUsername)
    35.         Call Registry.GetValue(HKEY_LOCAL_MACHINE, REGISTRY_LOCATION, "SQLPassword", sPassword)
    36.        
    37.         '********************************************
    38.         '* Replace tokens with the correct data . . .
    39.         '********************************************
    40.         SQLConnectionString = DEFAULT_CONNECTION_STRING
    41.         SQLConnectionString = Replace(SQLConnectionString, "%1", sProvider)
    42.         SQLConnectionString = Replace(SQLConnectionString, "%2", sPassword)
    43.         SQLConnectionString = Replace(SQLConnectionString, "%3", sUsername)
    44.         SQLConnectionString = Replace(SQLConnectionString, "%4", sServer)
    45.        
    46.         oSQLCnString.Value = SQLConnectionString
    47.  
    48.     Else
    49.         SQLConnectionString = oSQLCnString.Value
    50.     End If
    51.  
    52.     Set oSQLCnString = Nothing
    53.     Set oGroup = Nothing
    54.     Set oSPM = Nothing
    55.    
    56.     '*******************************************************
    57.     '* Release existing connection, then recreate it . . .
    58.     '*******************************************************
    59.     SQLClose oCn
    60.     Set oCn = New ADODB.Connection
    61.     oCn.ConnectionString = SQLConnectionString
    62.     oCn.Open
    63.    
    64.     Exit Sub
    65.    
    66. ERR_GetConnectionString:
    67.  
    68.     SQLClose oCn
    69.    
    70.     Set oSQLCnString = Nothing
    71.     Set oGroup = Nothing
    72.     Set oSPM = Nothing
    73.    
    74.     Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
    75.    
    76. End Sub
    77.  
    78. Public Function SQLClose(oCn As ADODB.Connection)
    79.  
    80.     If Not oCn Is Nothing Then
    81.         If oCn.State = adStateOpen Then
    82.             oCn.Close
    83.         End If
    84.         Set oCn = Nothing
    85.     End If
    86.    
    87. End Function

    I would then use this format in a function:

    VB Code:
    1. public AFunction() as boolean
    2.  
    3.    Dim oCn as ADODB.Connection
    4.  
    5.    SQLOpen oCn
    6.    oCn.Execute"Do Something Nice"
    7.    SQLClose oCn
    8.  
    9.    'do something else.
    10.  
    11. end function

    Works a treat on a COM+ server. You will need to change the registry access, and add the relevant COM+ stuff.

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55
    Thanks!!!!

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