Clever ways of dealing with open connections..?

Results 1 to 5 of 5

Thread: Clever ways of dealing with open connections..?

  1. #1
    SteveW Guest

    Default Clever ways of dealing with open connections..?

    It can sometimes be pretty tricky to make sure your closing and destroying connections properly (specially if there are lots of IF statements on a page). I wondered if anyone had any good techniques for spotting them? For example, closing them if they&#039;re open at the top of every page...<BR><BR>

  2. #2
    Ed Guest

    Default RE: Clever ways of dealing with open connections..

    Just close everything once you are done using them. After the last "End If"<BR><BR>

  3. #3
    Join Date
    Dec 1969

    Default RE: Clever ways of dealing with open connections..

    &nbsp;<BR>Maybe at the bottom of every page you could write some test code that queried each of the databases that have been opened on that page.<BR>Obviously, if the connection had been closed an &#039;object required&#039; error or something similar would occur, though if it was still open, you would be able to run the script ok.<BR>You could test run the script until the error appeared all the time, indicating that you have thouroughly destroyed all connections.<BR><BR>Thinking maybe easier to just organise the code nicely so you can see when connections are opened and hence close them.

  4. #4
    Join Date
    Dec 1969

    Default more Clever ways of dealing with open connections.

    Place your database connection statement in the global.asa file then you don&#039;t need to worry about opening or closing them in your scripts.<BR><BR>&#060;OBJECT RUNAT=Server SCOPE=Session ID=GlobalDBConnect PROGID="ADODB.Connection"&#062;<BR>&#060;/OBJECT&#062;<BR><BR>&#060;SCRIPT LANGUAGE=VBScript RUNAT=Server&#062;<BR>Sub Session_OnStart<BR> GlobalDBConnect.Open("ODBC;DATABASE=MyDatabase;UID =sa;PWD=;DSN=MyDatabase") <BR>End Sub<BR>&#060;/SCRIPT&#062;<BR><BR>&#060;SCRIPT LANGUAGE=VBScript RUNAT=Server&#062;<BR>Sub Session_OnEnd<BR> GlobalDBConnect.Close<BR>End Sub<BR>&#060;/SCRIPT&#062;

  5. #5
    Nathen Guest

    Default RE: more Clever ways of dealing with open connecti

    Actually this is not the best method because you&#039;re introducing thread affinity by putting in instance of the Connection class in a session level variable. Your application will not scale.<BR><BR>But you do have the right idea by using the object tag. With Windows 2000 you can place the objects you want to create in object tags at the top of your ASP file like so:<BR><BR>&#060;snip&#062;<BR>...<BR>&#060;%@ Language="VBScript" %&#062;<BR>&#060;OBJECT RUNAT="Server" ID="Conn" PROGID="ADODB.Connection"&#062;&#060;/OBJECT&#062;<BR>...<BR>&#060;/snip&#062;<BR><BR><BR>In your code for the ASP you won&#039;t ever have to call Server.CreateObject because the object will automatically be created the first time you call a method of the object. In addition you won&#039;t ever have to set the object equal to nothing because the object is automatically destroyed when the page has completed running. Some of the advantages of using this method include being able to put all your objects at the top of the page so you immediately know what you&#039;re using when you open the file, easier object management as far as creating and destroying go, and if you never call a method of a object in the page it will never be created. Some of the disadvantages are you don&#039;t have a finer level of control to destroy the object after you&#039;re done using it and code encapsulated in page-level functions utilizing an object won&#039;t be truly encapsulated. Just my .02.<BR>

Posting Permissions

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