• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

To use singleton or not?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic