473,414 Members | 1,709 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,414 software developers and data experts.

Singleton Pattern for Database Access --- Leave Open or Close Connection

I am currently implementing a singleton pattern for accessing a
database. Is it advisable to close the connection to the database at
all -- thus leaving it open or should it be closed. If closed, when
should I it close it -- after the execution of the each command to the
database? If leaving open, what impacts does it have. Imagining that
it is one connection to the database.

Thanks,
mescano

Nov 21 '05 #1
4 3941
That depends. What is your application's architecture? How many users does
it need to support? What database are you using?Leaving the connection open
is not scalable but if you only have a couple connections and you need the
performance then it may work for you. This is how many client/server
applications have been architected in the past. If you need to move to a
more scalable, distributed design then you should open the connection only
when you need it and then close it when you are done. Furthermore, if you
use the same connection string each time, you can take advantage of
connection pooling which provides even more scalability. Different providers
handle connection pooling differently but in general it is transparent to
the programmer. For more info, take a look at :
http://msdn.microsoft.com/library/en...asp?frame=true

HTH,
-B

"mescano" <ma****@binarymediasystems.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I am currently implementing a singleton pattern for accessing a
database. Is it advisable to close the connection to the database at
all -- thus leaving it open or should it be closed. If closed, when
should I it close it -- after the execution of the each command to the
database? If leaving open, what impacts does it have. Imagining that
it is one connection to the database.

Thanks,
mescano

Nov 21 '05 #2
April 12, 2005

I would Definitely Close It! For each connection you have open requires
a SQL Server Client Access License. These "CAL"s cost big $$s! I cannot
remember how much they are, but I do know that most of the time they costs
thousands of dollars! Hope this helps!

Joseph MCAD

"mescano" <ma****@binarymediasystems.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I am currently implementing a singleton pattern for accessing a
database. Is it advisable to close the connection to the database at
all -- thus leaving it open or should it be closed. If closed, when
should I it close it -- after the execution of the each command to the
database? If leaving open, what impacts does it have. Imagining that
it is one connection to the database.

Thanks,
mescano

Nov 21 '05 #3
Mescano,

In addition to the others.

It depends from your database. For a database server like SQLserver it is
adviced to close (dispose in this special case) as soon as possible.
For a database like Access it can be better to leave it open because
otherwise the user can delete/replace the file maybe while it is running and
than gives than an unpredicted error.

I hope this helps,

Cor
Nov 21 '05 #4
Mescano,
In addition to the other comments.

Which database client are you using? (System.Data.Odbc, System.Data.OleDb,
System.Data.SqlClient, System.Data.OracleClient, System.Data.SqlServerCe, or
other (MySQL))

As each may or may not support connection pooling, I know the first 4 (Odbc,
OleDb, SqlClient, & OracleClient do). Although you Open & Close the
connection object (SqlConnection for example) the physical connection to the
database is kept open for a short period of time in anticipation of being
reused.

http://msdn.microsoft.com/library/de...taProvider.asp
http://msdn.microsoft.com/library/de...taProvider.asp
http://msdn.microsoft.com/library/de...taprovider.asp
http://msdn.microsoft.com/library/de...rfororacle.asp
Are you writing a server side app or a client side app?

Leaving the connection object open on a server side app will hurt
scalability of your server app itself, as each client request will be
conflicting with the single connection object. For client side apps it will
hurt scalability of the database server itself, as all those physical
connections are held open.

In both cases I would consider opening & closing the connection for each
"unit of work" that I am doing. The trick is ensuring you close the
connection when the "unit of work" is finished. By "unit of work" I mean a
group of related transactions. For example retrieving an Order, from both
Order Header & Order Detail, would be a unit of work. Saving an Order, to
both Order Header & Order Detail, would be another unit of work. Otherwise I
would simply close the connection when I am done with it.

Hope this helps
Jay

"mescano" <ma****@binarymediasystems.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
|I am currently implementing a singleton pattern for accessing a
| database. Is it advisable to close the connection to the database at
| all -- thus leaving it open or should it be closed. If closed, when
| should I it close it -- after the execution of the each command to the
| database? If leaving open, what impacts does it have. Imagining that
| it is one connection to the database.
|
| Thanks,
| mescano
|
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
4
by: Macca | last post by:
Hi, I have an windows forms application that accesses a SQL database I have a few questions as to connecting to the database. This application will run 24 hours a day. It is a monitoring...
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...
9
by: Hasan O. Zavalsiz | last post by:
Hi , i am trying to figure out which approach is better to use . let me explain the scenario. i am using the "Nortwind" database . in this database i have "Customers " table .The following is the...
8
by: Gaensh | last post by:
HI, I have a singleton pattern to acess my database the following is the sample code use to implement singleton pattern public class DataAccessHelper { private static DataAccessHelper instance;...
9
by: FluffyCat | last post by:
To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern. http://www.fluffycat.com/PHP-Design-Patterns/Singleton/ In the...
2
by: Gary | last post by:
If you develop a stand alone c# windows application that only reads from a single local Access database, do you need to close the connection each time you navigate to a different record? No other...
3
by: fniles | last post by:
In the Windows application (using VB.NET 2005) I use connection pooling like the following: In the main form load I open a connection using a connection string that I stored in a global variable...
14
by: Rob Wilkerson | last post by:
Hey all - Not being a seasoned PHP developer, tonight I started playing with the use of the Singleton pattern to store configuration information. What I was surprised to find was that the...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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,...
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
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...
0
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...

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.