473,769 Members | 6,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1997
Buddy Ackerman <bu**********@b uddyackerman.co m> 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.co m>
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**********@b uddyackerman.co m> 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**********@b uddyackerman.co m> 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**********@b uddyackerman.co m> 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.co m>
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.Sql Client;
public class DBConnect
{
private DBConnect()
{

}
private static SQLConnection obj;
public static SqlConnection SetConnection()
{
if (obj!=null)
obj=new SQLConnection(" connectionStrin g--");
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
7641
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 and the compiler should flag it as such. Thanks Bharat Karia
7
4076
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 object's events have been deregistered when it's time to delete said object. If some other object is registered for one of his events, he will not get garbage collected and just lays around, receiving that event. What I was hoping I could do...
14
2942
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
8108
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 the code inside the constructor / desconstructor will never run? Mark
9
8409
by: Kylin | last post by:
any better reason ? -- FireCrow Studio Kylin Garden EMail:gaotianpu@gmail.com ICQ:156134382
5
269
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 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...
8
2637
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? I don't mind if the solution is hackish, the dll i'm writing is already full of code hacks. =p
18
2959
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 definition is perfectly legal in other CLR languages. For example in C#; static class XXX
7
1848
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 place DLL imports into a sealed class, with a private constructor to prevent instantiation (see Figure 1 at the very top): http://msdn.microsoft.com/msdnmag/issues/03/07/NET/default.aspx?loc=&fig=true#fig1 Why not just make a static class to...
0
9423
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
10049
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...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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...
0
6675
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
5310
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...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.