473,788 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQLConnection open and close vs. staying open.

2 New Member
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 2155
Curtis Rutland
3,256 Recognized Expert Specialist
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 Recognized Expert Moderator Expert
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
UlrikSL
2 New Member
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 Recognized Expert Expert
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
558
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 SqlCommand(). At that point I cannot use SqlConnection() again. Unless I .Close() it and then .Open it right? Then what is 'connection pooling' ? Can't I run multiple SqlCommand()s on the same SqlConnection() somehow ?
1
4941
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 get "Unreachable code detected" and if i close it before the return i get "Invalid attempt to FieldCount when reader is closed." When i refresh the page .. lot of sqlconnection is created.. and then i get this error message : Timeout expired....
4
5186
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 open & close the connection from everywhere in my app). My main form is an mdi parent which contains several mdi-childs. To open and close the sqlconnection in an mdi-child i use the following:
16
13997
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 Application that connects to a MSDE Database. I was reading the thread yesterday on this very same subject, but it only mentioned ASP.NET Applications. Can anyone tell me if it's a good idea
11
2900
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 reduces the need to getting and returning connections to the pool repeatedly if a page has multiple calls to the DB, and each one manages its own connection. However, this does requires more deliberate coding, like calling the...
4
2057
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 thing, then when everything is rendered down to the browser, and all references to the page object are ended, the connection to the server can be closed. First of all, are the SqlConnection Open and Close methods quick performers and I dont need to...
8
6802
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
3894
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 one SqlConnection object will be used. Can someone tell me is that a correct way to do? Does that affect the system performance? Cheers Victor
5
3430
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 write Pooling=true ? 2. What is the default Max Pool size ? After I open the connection, how can I check what is the max pool size ? 3. I always open the db right before I fill dataset or open a reader, then close it right away, but sometimes I...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.