Tuesday, March 27, 2012

DB access from master page

I am using content and master pages. The content page has to query the database to get the master page name (among other things), which is done in 'Page_PreInit', and then the master page has to query the database to get some layout options, done in 'Page_Load'.

Is it possible for the master page to use the existing open connection, or am I forced to close the connection in the content page and open up a new connection in the master page? I've tried various things without success.

The second question is, does it matter? Will opening and closing two connections be much slower than opening and closing one?

In my opinion you should open a connection as late as possible and close it as early as possible. So I think you should not use the same connection, instead close the one as soon as its work gets done (dispose it or use theusing block) and then create a new one for the other work.

HTH,

Vivek

|||

Thanks.

I've since done some reading up on connection pools, and I agree with what you are saying. I assumed that opening and closing a connection twice would require twice as much work, but this is not the case.

We live and learn.

|||Yes, connection pooling reduces the number of times that new connections need to be opened. The pooler maintains ownership of the physical connection. Every time a connection is open/closed, the pooler does not actually open/close a connection, thus improves performance. For details you can take a look at this article:Using Connection Pooling
And this article gives some tips on connection pooling performance:
Tuning Up ADO.NET Connection Pooling in ASP.NET Applications

No comments:

Post a Comment