473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

SQLConnection open and close vs. staying open.

My question is performance wize, is it better to open and close each time I make a request to the database, in small periods I make like 5 queryes a second, other times only every 10 seconds.

I can understand that close and open do not close the underlaying connection, so would the correct way be to open and close with each request, and give a better chance of avoiding faulty connection? Or would it cause worse performance?

Thanks.. Ulrik
Aug 14 '08 #1
4 2138
Curtis Rutland
3,256 Expert 2GB
I personally close my database after every transaction, unless I am doing several at a time. For example, if I needed to find the PK of a row to update, I don't close the conn between my select and update. If I need to loop through a list of commands I don't close. But I do close as soon as my chunk of commands are closed. I can always open it again.
Aug 14 '08 #2
Frinavale
9,735 Expert Mod 8TB
I personally close my database after every transaction, unless I am doing several at a time. For example, if I needed to find the PK of a row to update, I don't close the conn between my select and update. If I need to loop through a list of commands I don't close. But I do close as soon as my chunk of commands are closed. I can always open it again.

I agree with Insert on this one.
It will prevent problems in the future if you keep things clean and close the connection when you finish with the transaction. The only time it should stay open is if your doing a few transactions at the same time in a function.

Keeping your code clean will help in the long run.

-Frinny
Aug 14 '08 #3
Thanks for the answer..

After implementing the close and open in my singleton I still got some errors with my connection, now it was because of connection was still open. But I was closing the connection in a finalize, so I was sure it was closed everytime. After adding some sync locking to my code, everythings works like a charm.. Didn't know that asp.net runs its code-behind in multiple threads.. hmm..
Aug 14 '08 #4
Plater
7,872 Expert 4TB
Didn't know that asp.net runs its code-behind in multiple threads.. hmm..
Every user that connects to your page will effectively have their "own thread".

I also use the open and close method. However I am been "living dangerously" by not locking anything. Fortunatly, it doesn't get used much and it has not yet been a problem. Its on my "to do" list to fix
Aug 14 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Donnie Darko | last post by:
I'm trying to understand SqlConnection(), SqlCommand() For example. I use SqlConnection() as a parameter when I create a new SqlCommand. Then I open SqlConnection(), then I execute the...
1
by: huzz | last post by:
I have a method that gets sqldatareader as shown below.. my question is how do i close the sqlconnection (objConn) object within this method? If i put the objconn.Close(); after the return then i...
4
by: The Coolest Dolphin | last post by:
Hi all, I have a question/problem concerning usage of my sqlconnection throughout my program. In my main form (frmMain) I defined an sqlconnection (I've set the connection public so that I can...
16
by: ed_p | last post by:
Hello, I have implemented the singleton pattern for a class to hold a SqlConnection object that will be used thruout the application to create commands. My application is a simple Windows Form...
11
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
4
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
8
by: John J. Hughes II | last post by:
Is there a way of resetting the SqlConnection with closing/opening it again. I am not seeing anything called reset. Regards, John
4
by: Victor | last post by:
Hi Guys I have a problem here. I want to improve the performance for a website. When I looked into the system, I have found that the system made the "SqlConnection Object" static. That mean only...
5
by: fniles | last post by:
I am using VB.NET 2003. When using SQLClient.SQLConnection with SQL 2005 database, 1. connection pooling is automatically used, right ? I mean, in the connection string I do not need to explicitly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.