Current location: Hot Scripts Forums » Programming Languages » ASP.NET » Static database connection good idea or not?


Static database connection good idea or not?

Reply
  #1 (permalink)  
Old 08-25-10, 07:43 AM
Matrix28's Avatar
Matrix28 Matrix28 is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Static database connection good idea or not?

In an effort to speed up my pages I thought about setting up a static database connection in a subroutines page so I wouldn't have to connect and disconnect on each page, something like

Code:
public partial class SubsEngine : System.Web.UI.Page {
            public static MySqlConnection MySQLCon;
public static int ConnectToMySQL() {
                  MySQLCon = new MySqlConnection(blah;");
and then on each page I can use subs_aspx.SubsEngine.MySQLCon which would still be open.

Is this a good idea or not? In theory if 2 people requested a page at the same time would they get an "open data reader assosciated with this connection" while the first page is loading?
__________________
Server Talk - Need help with server configurations?
www.ComputerForums.org Computer Discussion forum
Nintra Search Engine - The Nintra Search Engine
Reply With Quote
  #2 (permalink)  
Old 08-27-10, 03:24 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Hi,

It is not a good idea. The scenario you describe with the Open Reader will occur. It will be very hard to accomodate all scenarios. .Net actually uses connection pooling

Connection pooling allocates actual connections and everything is managed for you. If many sessions need access, multiple connections are allocated and shared, as needed. As long as the connection strings are the same, you will get optimized allocation for multiple users.

Keeping static connection migth might make it unavaliable for the next command, or another command in another session.

So connection pooling is a fundamental part of the framework. Make use of it. Don't try to negate it's purpose.
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #3 (permalink)  
Old 08-27-10, 09:24 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
I have to agree with Yeroon on that one. What you could do however, assuming you are trying to reduce the number of queries you run on the database (if the same data such as navigation menu items, Latest News, etc is repeating from page to page), is to cache that dataset by assigning it to a session variable and then when the page reloads or changes you can simply grab the dataset from the session variable without re-querying of the database.

Pete
__________________
Reply With Quote
  #4 (permalink)  
Old 08-30-10, 06:35 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Please note though that Session usually is user based. If you want to keep it global for all user, use the Cache.
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #5 (permalink)  
Old 08-30-10, 09:14 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Quote:
Originally Posted by Yeroon View Post
Please note though that Session usually is user based. If you want to keep it global for all user, use the Cache.
Correct. My assumption was that the displayed content changes from user to user, which is why I recommended Session Variable. If it does not, you would use cache.

Pete
__________________
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make offline database consistent bubuna Database 1 12-07-10 11:56 AM
is it a good idea to run 23 search expressions? lordmerlin ASP 1 03-24-04 04:05 PM
A good online MSSQL database editor MadDog Script Requests 2 08-18-03 08:55 PM
From idea to inception at zero cost !!! JasonD General Advertisements 0 06-06-03 03:37 PM


All times are GMT -5. The time now is 02:46 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.