ASP.NET and Database Connections across a website

Alphathree33

Platinum Member
Dec 1, 2000
2,419
0
0
Scenario:

I have website X with many ASPX pages. Most pages will be talking to the same Access 2003 database on a regular basis.

I understand that database connections are expensive. Is there a reasonable way to make one connection object and have it used by all of the pages on my website, or do I need to reinitialize a database conection every time a page loads?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
ASP.NET automatically uses connection pooling internally (unless you've disabled it).
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
From this site:

  • Be sure than your connections use the same connection string each time. Connection pooling only works if the connection string is the same. If the connection string is different, then a new connection will be opened, and connection pooling won't be used.
  • Only open a connection when you need it, not before.
  • Close your connection as soon as you are done using it.
  • Don't leave a connection open if it is not being used.
  • Be sure to drop any temporary objects before closing a connection.
  • Be sure to close any user-defined transactions before closing a connection.
  • Don't use application roles if you want to take advantage of connection pooling.

Basically, create, open and close your connections on every page. The .NET framework will handle the connection pooling seemlessly in the background.
 

mAdMaLuDaWg

Platinum Member
Feb 15, 2003
2,437
1
0
And to make it easier, have the connection string stored in the web.config file
If you want to take it to another level, hash the connection string and store it in the registry.
 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
Originally posted by: singh
ASP.NET automatically uses connection pooling internally (unless you've disabled it).

Yes, it does. But I dont think Access supports pooling.


Edit: OP Maybe you should look into page/fragment caching if you are concerned about performance.
 

Alphathree33

Platinum Member
Dec 1, 2000
2,419
0
0
Thanks guys. I'm not overly concerned about performance RIGHT NOW, but I'd like to do it right in the first place rather than fix it later.

I'll investigate connection pooling a bit further. Thank you.