• Post Reply Bookmark Topic Watch Topic
  • New Topic

where singleton class used in java  RSS feed

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where singleton classes are used in j2ee web application.can we use it for connection pooling.
 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Singleton class is used when you want only ONE instance of a class to be available in your application.

Yes, Database Connection Pooling would be a good example of its use case.
For example you could have a singleton class called DBConnectionPoolManager, with methods like getConnection, returnConnection etc. It can have a getInstance static method to return the only instance that exists of the class.
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friend, Thanks alot for your reply
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the Singleton pattern has really fallen out of favor; see http://faq.javaranch.com/java/SingletonPattern for more information.

Connection pooling is not such a good example. Complex enterprise apps frequently need to access multiple databases - in which case you'd need a pool manager instance for each DB.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've developed the MultipleSingleton pattern for just this use, in fact.

Step 1: Develop MultipleSingleton pattern.
Step 2: ???
Step 3: Profit!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!