0

I have figured out how to create a global SQL connection object, using GLOBAL.ASA, in the following manner:

<object runat="server" scope="application" id="oConnGlobal" progid="ADODB.Connection"></object>

    sGlobalSQLConnectionString = "Not shown here"
    oConnGlobal.ConnectionTimeout = 240
    oConnGlobal.CommandTimeout = 240
    oConnGlobal.ConnectionString = sGlobalSQLConnectionString
    oConnGlobal.Mode = adModeReadWrite
    oConnGlobal.Open

This technique seems to be working okay, but over time, I have been reading up that this is not a good idea, and that the connection object should be created and closed during any SQL query throughout your code. Is this true, or is using a global SQL connection object in this manner okay?

PaulScott
  • 117
  • 8
  • 1
    That is correct. Ideally the `ADODB.Connection` should be open the minimal amount of time required to return the data and then closed. This doesn’t mean it can’t be opened again, but leaving it open for a long time is very inefficient. – user692942 Jul 22 '20 at 16:53
  • 1
    It’s fine to store your connection string in the `global.asa` to make it easily accessible by any `asp` page. Would also recommend using `ADODB.Command` for any queries as you can pass the connection string directly to the `ADODB.Command` using the `ActiveConnection` property which will use the connection string to instantiate the `ADODB.Connection` for you. Some example code [here](https://stackoverflow.com/a/21944948/692942) aimed at using stored procedures but the tips still apply. – user692942 Jul 22 '20 at 16:57
  • Hello Lankymart. So, just so that I have this correct, it okay to store the connection string for global use, but NOT the connection object itself? That's my takeaway from your comments. – PaulScott Jul 23 '20 at 18:24
  • 1
    That is correct. – user692942 Jul 24 '20 at 06:57

0 Answers0