Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a Singleton for a DAO class

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Good day to all ,


while declaring DAO on to the application-hibernate.xml , is it good to declare the DAO class as a singleton or not ?

Please tell me the best approach .

Thanks in advance .


 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I would always have all my DAOs as Singletons in Spring. I would never hold state in a DAO, so no reason to have more than one, one can handle a lot of clients simultaneously, and since it doesn't hold state you don't have to worry about thread safety and synchronization.

Mark
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All stateless objects should be singleton.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Mark , Kengkaj .

That helped .


One more question on this .

As far my knowledge , By Making an Object singleton will result in bottleneck know , so how can simultaneous requests can be handled ? (AS DAO are made singleton )

Please let me know the answer for this .
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Pavan wrote:
As far my knowledge , By Making an Object singleton will result in bottleneck know , so how can simultaneous requests can be handled ? (AS DAO are made singleton )

It depends on if your code use synchronize, if not it's fine. There is only one instance but it can be accessed by many threads.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Kengkaj Sathianpantarit . You really made the difference .
Myself very thankful to you .


 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Glad to know my answer is useful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic