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

Singleton in a Web app

This may not be the best place to post this, but I'm
coding in c# and, well this group is a "csharp" group!

Background -- We encapsulate data access for an application
through a home-grown "DataEngine" object. This object follows
the singleton pattern. All is well and good, and everything
works just great for our desktop apps that hit the database
through the DataEngine.

Now, we've got to expose the DataEngine object to a web
server. Initially, I'm concerned. I don't do much (any)
web programming, but I've got a good feel for how it works.

My concern is that the DataEngine, being a singleton, will
be shared amongst all user sessions on the web server for
this site. This brings up 2 related major issues that I
have no clue on.

(1) Multi-threading. The DataEngine is not "thread-safe".
It assumes a single user. This was a good, valid assumption
based on the way we build our desktop apps.

(2) If I start locking things up to make it "thread-safe",
I believe I'm in a world of hurt as far as the DataEngine
becoming a bottleneck on the web server.

Do these concerns make sense? What do other folks do in
similar situations?

Thanks.

-- Sarge

Nov 17 '05 #1
4 1785
In message <62***************************@FUSE.NET>, Sgt. Sausage
<no****@nowhere.com> writes
My concern is that the DataEngine, being a singleton, will
be shared amongst all user sessions on the web server for
this site. This brings up 2 related major issues that I
have no clue on.

(1) Multi-threading. The DataEngine is not "thread-safe".
It assumes a single user. This was a good, valid assumption
based on the way we build our desktop apps. (2) If I start locking things up to make it "thread-safe",
I believe I'm in a world of hurt as far as the DataEngine
becoming a bottleneck on the web server.

Do these concerns make sense? What do other folks do in
similar situations?


So it is currently a singleton within the context of a single user's
application? Is it definitely not thread-safe, or just not explicitly
designed to be so? Does it hold any state which would be unique to a
single user, or make any assumptions about the order of consecutive
calls?

I can see no reason why it being a singleton would in itself cause
problems, however the reasons for making it a singleton in the first
place may mean that it's unsuitable. As a thought experiment, and
ignoring any database connection logic, would it still work if you made
all of the methods static? I'm not suggesting this as a solution, just
fishing for the design of the thing.

--
Steve Walker
Nov 17 '05 #2
Hi,

My concern is that the DataEngine, being a singleton, will
be shared amongst all user sessions on the web server for
this site. This brings up 2 related major issues that I
have no clue on.
Yes, this is correct it will be "singleton" in the application context
meaning that all the sessions will share the same instance
(1) Multi-threading. The DataEngine is not "thread-safe".
It assumes a single user. This was a good, valid assumption
based on the way we build our desktop apps.
Well, I have a similar deployment in a couple of application and I have
never had a single issue, it all depends of how you coded the DataEngine
though. In mine I export methods similars to the SqlCommand (
ExecuteNonQuery, ExecuteScalar, etc) I made sure that I use no instance
variables in this case, I create a connection in each method ( and close it
at the end of the method) so the class is thread safe.
(2) If I start locking things up to make it "thread-safe",
I believe I'm in a world of hurt as far as the DataEngine
becoming a bottleneck on the web server.


It will, don;t do it, first make sure if you need to lock something, most
probably you could recode it in such a way that no instance variables are
used ( or values are modified ) and hence you do not need a sync mechanism.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #3

Move the state data in the singleton object to separate state objects.
Instantiate the state objects on the initial call into the singleton
and pass them arround. Therefore all your methods become
stateless--they rely on the parameter for state maintenance.

I also just recently switched to web development from desktop and the
biggest difference is the necessity of making all back-end classes
(data/business logic stuff) stateless. To account for this pass
around state parameters as needed but also evaluate if they really are
needed--if the object can do it's work without all the state, then
just pass it what it needs.

There are also context stores you can use for state management that is
specific to a single call--HttpContext in web pages and services,
CallContext in remoting. These can be used for storing state for a
single call throughout the call, but should be used sparingly--more
for design issues related to isolation of layers, technologies, and
change risk as opposed to performance.

HTH,

Sam
On Thu, 12 May 2005 04:39:00 -0400, "Sgt. Sausage"
<no****@nowhere.com> wrote:
This may not be the best place to post this, but I'm
coding in c# and, well this group is a "csharp" group!

Background -- We encapsulate data access for an application
through a home-grown "DataEngine" object. This object follows
the singleton pattern. All is well and good, and everything
works just great for our desktop apps that hit the database
through the DataEngine.

Now, we've got to expose the DataEngine object to a web
server. Initially, I'm concerned. I don't do much (any)
web programming, but I've got a good feel for how it works.

My concern is that the DataEngine, being a singleton, will
be shared amongst all user sessions on the web server for
this site. This brings up 2 related major issues that I
have no clue on.

(1) Multi-threading. The DataEngine is not "thread-safe".
It assumes a single user. This was a good, valid assumption
based on the way we build our desktop apps.

(2) If I start locking things up to make it "thread-safe",
I believe I'm in a world of hurt as far as the DataEngine
becoming a bottleneck on the web server.

Do these concerns make sense? What do other folks do in
similar situations?

Thanks.

-- Sarge


Nov 17 '05 #4

"Samuel R. Neff" <in**********@newsgroup.nospam> wrote in message
news:u5********************************@4ax.com...
Move the state data in the singleton object to separate state objects.
Instantiate the state objects on the initial call into the singleton
and pass them arround. Therefore all your methods become
stateless--they rely on the parameter for state maintenance.


Bravo! That's just the piece I was missing. One of those
"well ... duh!" moments for me. That should do the trick.

Thanks!

[snippage]
Nov 17 '05 #5

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

Similar topics

10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.