ASP.NET Singleton usage

F

fredd00

Hi

I'm just starting with singleton and would like to implement in my new
web app.

I have a question

lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do

SqlConnection cn = DataHelper.GetConnection()


when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?

What would be the advantage of using singleton objects over putting my
objects in application variables?


Thanks
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

fredd00 said:
lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do

SqlConnection cn = DataHelper.GetConnection()

when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?

You will get some weird errors occasionally.

Singleton is not suited for this type of usage.

To get it working you would need to synchronize access and that
would cripple performance.

Just use connection pool.
What would be the advantage of using singleton objects over putting my
objects in application variables?

Among other thing Singleton can be used in code that are
not web specific.

Arne
 
M

Mark Rae

lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do

SqlConnection cn = DataHelper.GetConnection()

This is one of the worst things you can do for a web app in terms of
performance and scalability...

SqlConnection objects should be created whenever required and explicitly
destroyed immediately they are no longer required... That way you take
advantage of connection pooling, so long as the SqlConnection objects are
always created with *exactly* the same connection string.
 
M

Michael Nemtsev

Hello fredd00,

Just add to Arne, using variables isn;t good idea either. Because asp.net
page has a limited live time

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

f> What would be the advantage of using singleton objects over putting
f> my objects in application variables?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Michael said:
f> What would be the advantage of using singleton objects over putting
f> my objects in application variables?
Just add to Arne, using variables isn;t good idea either. Because
asp.net page has a limited live time

I actually read that as having it in the Application object.

Arne
 
G

Guest

fred00,
If you look at best-practices code like the SqlHelper V2 (Data Access
Application Block) you will see that all the methods are static. The
SqlParameteCache Class therein is also static, so that it can cache
SqlParameter collections across multiple pages and multiple requests.

Singleton provides an instance of a class, but "only one" instance. Take a
look at the code I mentioned and you will see the difference.

Peter
 
F

fredd00

I used to have a helper class that returned a SQLConnection so i guess
i'm already using pooling.

If i want to cache data (let's say dataset) would it be better to
cache it in a singleton or in the Cache object?

I want to use the Data Access Application block , is there data caching
in it ?

thanks
 
S

sloan

See my blog entry:

10/24/2005
Web Session Wrapper for storing and retrieving objects


http://sholliday.spaces.live.com/blog/



In the web environment, I created a singleton which piggyback's off of the
Session object.
It could be an application object as well.

...

Also search for "WeakReference" ............ it allows a holding mechanism
but an "out" if memory demands become too much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top