Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Global connection in global.asa : session_onstart

Status
Not open for further replies.

rolandschwarz2001

IS-IT--Management
Aug 17, 2001
21
0
0
DE
Hi folks !

I have a so called small problem developing an asp webshop.
Whenever someone points his browser to our webshop we create a new session, and with this new session we want to open one (!) global connection to our database to process queries.
In global.asa in Session_OnStart we at first define and zero some variables.
The question is : how can I create one Connection object in global.asa.Session_OnStart that exists until it is closed via Session_OnEnd and is available through the whole site structure ?

Any ideas ?

Thanks alot for any help and have a nice weekend !

Roland
 
I would not recommend opening a global connection to the database on the session level.

It is much more efficient to open and close connection on each page; this way it gets back into connection pool and becomes available for another user.

If you still want to do it, I assume you just go like this:

On_Start:

conn = Server.CreateObject("ADODB.Connection")
conn.Open(...)

On_End:

conn.Close
Set conn = Nothing

But again, unless you make user to explicitly ending the session by calling a page that has Session.Abandon in it, the connection will be busy for 20 minutes (if that's your session length), while the connection was only needed for 5 seconds.

Just a thought...[neutral] <Dmitriy>
dbrom@crosswinds.net
 
I'm pretty sure you can't use ADO in the .asa file....meaning you can't make a connection to the database. I could be wrong...(have been before)...but I'm pretty sure about it...

Also, to open a db connection to last for a user throughout the site, your creating an enormous strain on the server...especially when there are multiple users, because it has to hold the db connection open all while the user is connected to the server (20 min or days(?))...then compound that with dozens, hundreds, or even thousands of users. It's not practical.

If you don't have to update your db, or add new records, or anything, then you could create a disconnected recordset and transfer that through the session object and pull information out if it if you like...that would lighten up the load a little bit, but, you wouldn't necessarily be working with the most recent information past the first couple minutes...depending on the mission of your server/workgroup.

For a quicker reponse time, you can transfer the recordset from the session variable to a local variable for each page...but, for the best, and most accurate information, it is best to open and close the database connections on each page as quickly as possible.

Hope this helps... -Ovatvvon :-Q
 
The best advice on this is to stay away from creating the object in the global.asa. Like everyone else said, it will hold those connections open for a long time, and again. If you home page has any ASP on it, then it will envoke the session_onstart command when somebody hits that, even if they don't do anything else. So now you have a database conneciton for a user that didn't use the database.

I put all my database connection strings into a separate ASP file with the rest of the common subs and functions that I use. That way I include that file on all my pages with a server side include, and then simply call my functions to open the connection when i need it. That way I can open it and close it with a simple command instead of retyping all the stuff to create the connection.


Conn.asp

Sub login
dsn = &quot;Appropriate connection string&quot;
Set myConn = server.createobject(&quot;adodb.connection&quot;)
set myRS = server.createobject(&quot;adodb.recordset&quot;)
myconn.open dsn

end sub

Then when I need to call my database connection

<%
login

%>

That's it. Generally i will do the same to close my connection. So that I can dump the connection as fast a possible after I have the recordset.
 
You can create ADO objects in global.asa - though their response varies.

This becomes important when clearing out Session cart data from a database

Session On_End does not seem to like using FileSystemObjects, but does not mind ADODB stuff. Just weird really.

<bb/>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top