Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton pattern in j2ee?  RSS feed

 
Mark Wa
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it wise to use the singleton pattern in a J2EE web application?
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell us why we should not use singlton pattern in J2EE application?

In our product (that is j2ee) we are using singleton pattern. that why I am thinking we can use it.for class which we want to have only one instance with respect to client.

Please tell us.

Thanks in advance.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In part it was meant as a tongue-in-cheek response--the question was ill-formed and impossible to answer without context.

That said, singleton-considered-stupid. Or, for slightly less opinionated viewpoints, Singleton_pattern#Drawbacks, Use your singletons wisely, and finally SingletonPattern.

The correct answer to the original question is "Maybe; it depends"--it's impossible to answer (correctly) with a simple "yes" or "no."

Edit: Forgot a reference to the google-singleton-detector.
 
Mark Wa
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant in the context of will it cause problems with sessions, for example, if i have 2 users using the system at the same time, they will get one singleton between them? and not one each.
 
vignesh dhakshinamoorthy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am using singleton pattern in my web application.
Whether it is right or wrong, I am using it to preserve the JDBC Connection object.

Reason:
O I am hitting the database umpteenth times.
O Every time, i cannot create a Connection object to hit the DB.
O So i will create that Singleton once the user logs in.

And someone please tell me if i am following a 'wise' approach ?
If not, please do suggest the best approach
 
venkat nadikattu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why cann't you initilize it in the "init()" method so that it will be called only once right?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Wa wrote:I meant in the context of will it cause problems with sessions, for example, if i have 2 users using the system at the same time, they will get one singleton between them? and not one each.


It depends. If you application is not deployed in a cluster, and you have implemented the singleton pattern correctly, then they will share the same singleton instance. If your application is deployed in a cluster then they may get the same instance, they may also access a different instance. The sinlgeton pattern is one instance per JVM, if you can't tolerate more than one instance in an application (and your application may be deployed in a cluster) don't use a singleton. If you need caching consider a distributed cache.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vignesh dhakshinamoorthy wrote:Hi, i am using singleton pattern in my web application.
Whether it is right or wrong, I am using it to preserve the JDBC Connection object.

Reason:
O I am hitting the database umpteenth times.
O Every time, i cannot create a Connection object to hit the DB.
O So i will create that Singleton once the user logs in.

And someone please tell me if i am following a 'wise' approach ?
If not, please do suggest the best approach


Personally I wouldn't do this. Its far easier to just use a ConnectionPool. What you are doing sounds like a (missguided) early optimisation - what benefit is there to restricting the access to an inherently multiuser system (the database) from an inherently multiuser application (your web application) through the bottleneck of one solitary connection?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Wa wrote:I meant in the context of will it cause problems with sessions, for example, if i have 2 users using the system at the same time, they will get one singleton between them? and not one each.

In addition to what Paul said, I'd add that using singletons correctly also depends on the nature of the singleton--if there's shared data that's being written to it must be made thread-safe if the application depends on deterministic results. If the singleton doesn't contain any shared data then most likely everything's fine.

These days a lot of people manage singletons via Spring, which makes them seem less ugly (at least to me).
 
vignesh dhakshinamoorthy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:
Personally I wouldn't do this. Its far easier to just use a ConnectionPool. What you are doing sounds like a (missguided) early optimisation

Paul, thanks for taking time to correct me & guide me. My first ever forumn post is How to implement connection pooling ?.
As i am a beginner, one person told me that "Database connection is a costly hit & you should use connection pooling". When asked how, i was requested to read more about "Singleton pattern".
It's my first J2EE application and this is my 'singleton' story
Joe Ess has given me the idea. i have to learn & implement using it.
Thanks for your assistance.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome vignesh - we are all learning
 
Mark Wa
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your replies. I am using a clustered environment, so I guess that makes sense (one singleton per JVM). I would like to use spring, although I have not used it before, but I am only improving upon an existing application, and im stuck with the existing ways. I'm very slowly improving it to implement the MVC pattern, without it, its a bit of a maintenance nightmare
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!