• Post Reply Bookmark Topic Watch Topic
  • New Topic

To use singleton or not?  RSS feed

 
Vinnie Nat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a Connection object that needs to be available application wide.
Apart from that, we use the logged in userId to control access to certain clients in the application depending on the region.
We have wrapped the Connection object and the userId in another object called UserSession..

What is a good way to implement this?
Can we use the singleton pattern for this given that the userId can change according to the user logged in?

Or do we implement the Connection object as a singleton and pass the userId around to specific objects that require it?
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What platform are you using? Most servers use the concept of database pools to manage distributed connections in a larger-scale system (which it sounds like your application might be).

If you can avoid Singletons, do it, but if the performance is better and you have full control over the application (which you don't generally have on most platforms) than go for it. Keep in mind J2EE advocates against static singletons.
 
Vinnie Nat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply.
the architecture is pretty basic.
we are using Plain Old Java classes which talk to the database using jdbc.
the jsps/servlets talk to the POJOs from the web container.
All these POJOs need access to the Connection object and as I said some of them need access to the user login information.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you now reveal, you are writing a web app. Web apps should use connection pooling. It's a simple as that!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!