Solved

Persistent SQL server connection

Posted on 2004-10-20
9
842 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now