Solved

Persistent SQL server connection

Posted on 2004-10-20
9
854 Views
Last Modified: 2010-08-05
My boss does not like the fact that in our ASP.NET pages, we bracket each DB query with a connection open and close. In my mind this couple of microseconds in a page load is irrelevant, but he is the boss. He says there is a System object you can use which represents a persistent connection to the server which is always open and does not have to be opened and closed, and is thus more efficient. True? Opinions?
Thanks
0
Comment
Question by:RichardFox
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 22

Assisted Solution

by:CJ_S
CJ_S earned 50 total points
ID: 12361896
What System object? System is a namespace which holds objects. And you definitely should close your connections. You can however open a connection at page level, and close it when the page is done. Just extend from Page and use that class in your inheritance.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 12361934
objects = classes
0
 
LVL 8

Assisted Solution

by:daffodils
daffodils earned 50 total points
ID: 12362076
I am unaware of any such object in System or any other namespace that holds persistent connection to the Server.

An option is to use SqlDataAdpater, instead of explicitly opening and closing connections.
SqlDataAdapater.Fill(query, conn) implicitly opens and closes connections ..
The connection object associated with the "query" must be valid, but it does not need to be open. If the connection is closed before the method is called is called, it is opened to retrieve data, then closed.

    SqlConnection conn = new SqlConnection(connection);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    adapter.Fill(dataset);
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Assisted Solution

by:mmarinov
mmarinov earned 50 total points
ID: 12362373
Hi RichardFox,

you must not leave the connection open becuase the connection pool fill be filled and you will not be able to use the connecitons
you can use auto close connection by sqldataadapter like this

    DataTable dt = new DataTable();
    SqlConnection conn = new SqlConnection(connection);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.Fill(dt, new SqlCommand("Select statement", conn), CommnadBehaviour.CloseConnection  );

B..M
0
 
LVL 33

Accepted Solution

by:
raterus earned 50 total points
ID: 12362375
Teach your boss a lession, the general rule of thumb is to open a connection at the very last second and close it as soon as possible.  You are doing it correctly, he is not.  .NET also uses connection pooling, so you really aren't recreating the connection each time.  Don't settle for "his" way of doing it, because he is wrong.  If he still says "but..", ask him what he plans to do when an exception occurs and the connection is never closed...hmmm.
0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 50 total points
ID: 12363168
Agree with all of the above...  Hanging connections are probably the single biggest obstacle to a site being able to scale to a large # of users (that and maybe session state).  Sounds like he is micrmanaging the code, so I would go with the DataAdapter suggestion above.  It does what you are doing, but you will not have the apparently politically incorrcet open and close lines of code.

John
0
 

Author Comment

by:RichardFox
ID: 12363375
Thanks a lot folks. I have lots of ammo now. I am not sure how to split/award points, though
0
 
LVL 33

Expert Comment

by:raterus
ID: 12363419
The general rule of thumb when you don't know how to split points is to give all the points to the expert who's first letter of their name most closely matches yours. :-)
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12363421
i think that all of us gave you something to fill the "hole" - hope that these posts will help you at the discussion with your boss
B..M
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question