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?
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.
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.