473,398 Members | 2,427 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,398 software developers and data experts.

Disposing in a sealed class

I have taken over an application that has a sealed (singleton) class for database access. I want to add a private
SQLConnection class variable and open the connection it whenever it is instantiated and close it when the class it
disposed. I do this so that I can call mutiple methods within the class without having to open a connection each time.
I implemented an IDisposable interface (and a finalize interface) but it doesn't appear to work so I'm left with open
database connections. Is it just not possible to implement IDisposable on a singleton class? Should I jsut implement
finalize (if that's even possible)?

On a side note I don't see any reason to even have the Database class be a singleton. I plan on chnaging it in the
future but I don't have time to do it now.
--Buddy
Jul 21 '05 #1
5 1971
Buddy Ackerman <bu**********@buddyackerman.com> wrote:
I have taken over an application that has a sealed (singleton) class
for database access. I want to add a private SQLConnection class
variable and open the connection it whenever it is instantiated and
close it when the class it disposed.


That sounds very odd if it's a singleton - singletons are typically
created a single time and then left for the entire lifetime of the app.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Jon Skeet [C# MVP] wrote:
Buddy Ackerman <bu**********@buddyackerman.com> wrote:
I have taken over an application that has a sealed (singleton) class
for database access. I want to add a private SQLConnection class
variable and open the connection it whenever it is instantiated and
close it when the class it disposed.

That sounds very odd if it's a singleton - singletons are typically
created a single time and then left for the entire lifetime of the app.

Actually that's fine that way the entire app will use the same connection for the lifetime of the app. My only question
is the possible serialization of the calls to the singleton. If 100 users hit the site at the same time then does each
call have to wait for the previous one to finish?
Jul 21 '05 #3
It depends on how your class is handling the calls. If you create
asynchronous methods in your singleton(!!) class, then a call does not have
to wait for another call to finish.

BTW, a sealed class is not synonymous with a singleton class.

with regards,

J.v.
"Buddy Ackerman" wrote:
Jon Skeet [C# MVP] wrote:
Buddy Ackerman <bu**********@buddyackerman.com> wrote:
I have taken over an application that has a sealed (singleton) class
for database access. I want to add a private SQLConnection class
variable and open the connection it whenever it is instantiated and
close it when the class it disposed.

That sounds very odd if it's a singleton - singletons are typically
created a single time and then left for the entire lifetime of the app.

Actually that's fine that way the entire app will use the same connection for the lifetime of the app. My only question
is the possible serialization of the calls to the singleton. If 100 users hit the site at the same time then does each
call have to wait for the previous one to finish?

Jul 21 '05 #4
Buddy Ackerman <bu**********@buddyackerman.com> wrote:
That sounds very odd if it's a singleton - singletons are typically
created a single time and then left for the entire lifetime of the app.


Actually that's fine that way the entire app will use the same
connection for the lifetime of the app. My only question is the
possible serialization of the calls to the singleton. If 100 users
hit the site at the same time then does each call have to wait for
the previous one to finish?


Not unless you put some synchronization in to make that happen.
However, you won't be able to use the same connection for more than one
request at a time...

You should generally open database connections and close them as soon
as possible, and let connection pooling take care of the rest.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
<quote>On a side note I don't see any reason to even have the Database class
be a singleton. I plan on chnaging it in the
future but I don't have time to do it now.</quote>
A singleton class could be useful for creating and maintaining a single
connection object because in ADO.Net database pooling is automatically
managed by ADO.Net provided the connection string does not change from the
initial string. You could create a singleton class as below since you mention
you do not have the time to do it right away
using System.Data.SqlClient;
public class DBConnect
{
private DBConnect()
{

}
private static SQLConnection obj;
public static SqlConnection SetConnection()
{
if (obj!=null)
obj=new SQLConnection("connectionString--");
return obj;
}
}

In ADO.Net, the recommended way to manage connection pool is to open a
connection and close it as soon as the app is done with its task. The Close
method implicitly calls on the dispose method as well.

With the above connection class, you could have a single connection object
and use it to open and close a connection as and when required. It is not
advisable to save and use a connection state.

Aynchronous method calls help in making a call and returning the call back
to the calling object so that the calling object can continue with its
processing and when the called method has executed a callback will notify the
calling object and the results are collected by the calling method. So, if
you want to execute multiple methods simultaneously, then you should use
asynchronous methods.

with regards,

J.v.

"Buddy Ackerman" wrote:
I have taken over an application that has a sealed (singleton) class for database access. I want to add a private
SQLConnection class variable and open the connection it whenever it is instantiated and close it when the class it
disposed. I do this so that I can call mutiple methods within the class without having to open a connection each time.
I implemented an IDisposable interface (and a finalize interface) but it doesn't appear to work so I'm left with open
database connections. Is it just not possible to implement IDisposable on a singleton class? Should I jsut implement
finalize (if that's even possible)?

On a side note I don't see any reason to even have the Database class be a singleton. I plan on chnaging it in the
future but I don't have time to do it now.
--Buddy

Jul 21 '05 #6

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

Similar topics

5
by: Bharat Karia | last post by:
Hi, Is it possible to writed Sealed classes in C++ . i.e. there is no sealed/final keyword in C++, but is it possible to achieve the same effect? i.e. deriving from a sealed class is an error...
7
by: Bryan D. | last post by:
Our application makes extensive use of C#'s event handling mechanism to communicate between classes. A problem has cropped in that it becomes difficult to know for sure that all observers of an...
14
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
13
by: Mark Rae | last post by:
Hi, Since sealed classes can't be instantiated with the new keyword e.g. CClass objClass = new CClass(), does this mean that they don't have constructors / deconstructors or, if they do, that...
9
by: Kylin | last post by:
any better reason ? -- FireCrow Studio Kylin Garden EMail:gaotianpu@gmail.com ICQ:156134382
5
by: Buddy Ackerman | last post by:
I have taken over an application that has a sealed (singleton) class for database access. I want to add a private SQLConnection class variable and open the connection it whenever it is instantiated...
8
by: shawnz | last post by:
Is there any way to either derive a sealed class, or access private members of a sealed class? Either that, or is there a way to run internal methods in a sealed class outside the current assembly?...
18
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent...
7
by: Zytan | last post by:
I know you cannot have a sealed static class, but why not? Why must static classes be left open to inheritance? This article: http://msdn.microsoft.com/msdnmag/issues/03/07/NET/ recommends to...
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: 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
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.