Monday, March 19, 2012
Maintaing a connection across pages (Persistent)
I realize that SQL Server is designed to handle large amounts of traffic and my humble little site probably isn't killing it with shear traffic, but I'm wondering if my programming practicies are sub par and causing problems.
I'm coding in ASP (work hasn't given us the chance to migrate to .net). Anyway my site is getting some 30 - 40k hits a day on the actual web portion and probably another 10-20k on backed stuff - all which open up database connections to do their work.
I've had locks and blocks routinely because of this and I'm afraid I'm just going about this all wrong.
So I'm wondering if there is a way to maintain an established connection in ASP across multiple pages and/or sessions so I don't have to constantly drop and reconnect to the db?
Any help would be great!So I'm wondering if there is a way to maintain an established connection in ASP across multiple pages and/or sessions so I don't have to constantly drop and reconnect to the db?
I don't think that this is what you would want to do. Database connection pools would normally be the responsibility of an applications server (something like BEA's WebLogic). You could write your own custom (COM+) connection pool manager, but it's a fair amount of work and would only make a difference if your site were generating an order of magnitude more traffic.
Check out the following link for some "best practices" when coding in ASP.
ASP Best Practices (http://www.valtara.com/csc123/WhitePaper/ASPBestPractices.htm#15).
Probably your best starting point is to tune your T-SQL, avoid using dynamic SQL (use stored procs), ensure you have adequate indexing and avoid mistakes like pulling back an entire recordset when you only need one or two records (ie, use a WHERE clause).
Regards,
hmscott
Monday, March 12, 2012
Maintain a consistent DB connection through Application lifecycle
Hey everyone,
I'm new to .NET and I've recently inheirited a rather large and busy asp.net website. I was asked to add a testimonials section on each page that will randomly pull a testimonial out of the db. This is fine, however, I'm getting random errors about the DB connection either being closed or connecting. Here is the code for the testimonials class:
1public SqlDataReader GetTestimonials(ref SqlDataReader reader,int iCatID,string sLanguageType)
2 {
3 SqlCommand cmd =new SqlCommand("sp_DVX_Testimonials_Fetch", Connection);
4 cmd.CommandType = CommandType.StoredProcedure;
56 cmd.Parameters.Add(new SqlParameter("@.Cat_ID", iCatID));
7 cmd.Parameters.Add(new SqlParameter("@.LanguageType", sLanguageType));
89 reader = cmd.ExecuteReader();
1011return reader;
12 }
I know this isn't the best way to do this (especially for each page[this site averages about 1000 hits a day]), so I was wondering was--is there a way to maintain a single DB connection that's set up in the Application_Start that will maintain the connection so I don't have to worry about this error. If not, does anyone and any ideas as to what would help?
Thanks in advance!
Well, I don't think you need to do this, as ADO.Net connections are pooled. Usually the best practice would be: open connection -> retrive data -> close it immediately. The underlying managed data provider will help you to decide if "a connection" "really" closes physically..
msdn:
ASqlConnection object represents a unique session to a SQL Server data source
And where dose 'Connection' variable come from in your code? A member of the Page? As SqlConnection is not thread safe, there might be situations when one thread opens it and is ready to read, while another thread just closed it, so error occurs~
|||The Connection variable is initialized in the constructor of whatever class is using it. Currently, every class initializes its own DB connection. Would this have be bad? :-P
Thanks for the info!
|||Yes, this would have been bad.
It would be better to have your data access layer maintain the connections. Also, I would tell the datareader to close the connection automatically when the datareader closes, and create/open your connection object from within your DAL.
Although 1000 hits per day is an extremely low traffic site, you really should get in the habit of opening your connection when it's needed, do whatever you need to, then close it as quickly as possible.
|||Would you happen to know of any resources that show how to maintain connections within the DAL? I'm coming to .Net from a php background and I'm still getting the hang of the structure of everything.
Thanks!
Saturday, February 25, 2012
Mac users
When they use Safari RS refuses their connection and when they use IE 5 they
are prompted for User/Password/Doman (which is OK) but the drop downs
(prompts) aren't showing up.
Is there a solution?
ThanksGive them a PC! :)
Sorry, couldn't resist.
Alex wrote:
> We are having issues with Mac users accessing our RS reports.
> When they use Safari RS refuses their connection and when they use IE
> 5 they are prompted for User/Password/Doman (which is OK) but the
> drop downs (prompts) aren't showing up.
> Is there a solution?
> Thanks|||For a more serious reply, I did some cross-browser testing recently,
both mac and pc and got really poor results. Some of the worst problems
were IE on Mac!
I posted on a microsoft forum somewhere but got no response.
Chris
Chris McGuigan wrote:
> Give them a PC! :)
> Sorry, couldn't resist.
>
> Alex wrote:
> > We are having issues with Mac users accessing our RS reports.
> > When they use Safari RS refuses their connection and when they use
> > IE 5 they are prompted for User/Password/Doman (which is OK) but the
> > drop downs (prompts) aren't showing up.
> >
> > Is there a solution?
> >
> > Thanks|||You want me to be lynched don't you
"Chris McGuigan" wrote:
> Give them a PC! :)
> Sorry, couldn't resist.
>
> Alex wrote:
> > We are having issues with Mac users accessing our RS reports.
> > When they use Safari RS refuses their connection and when they use IE
> > 5 they are prompted for User/Password/Doman (which is OK) but the
> > drop downs (prompts) aren't showing up.
> >
> > Is there a solution?
> >
> > Thanks
>|||I've had success with 1 user being able to get on RS and having the drop
downs show up and work.
They were even able to run reports.
The only hick right now is that when they choose a format to export the
"Export" button never gets activated.
Any idead, anyone
"Chris McGuigan" wrote:
> For a more serious reply, I did some cross-browser testing recently,
> both mac and pc and got really poor results. Some of the worst problems
> were IE on Mac!
> I posted on a microsoft forum somewhere but got no response.
> Chris
>
> Chris McGuigan wrote:
> > Give them a PC! :)
> >
> > Sorry, couldn't resist.
> >
> >
> > Alex wrote:
> >
> > > We are having issues with Mac users accessing our RS reports.
> > > When they use Safari RS refuses their connection and when they use
> > > IE 5 they are prompted for User/Password/Doman (which is OK) but the
> > > drop downs (prompts) aren't showing up.
> > >
> > > Is there a solution?
> > >
> > > Thanks
>