This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton or Prototype for DI bean injection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Singleton or Prototype for DI bean injection?" Watch "Singleton or Prototype for DI bean injection?" New topic
Author

Singleton or Prototype for DI bean injection?

Dinesh Sundrani
Ranch Hand

Joined: Mar 21, 2006
Posts: 78
Do you think singleton scope is a good idea in multithread environments, how do we maintain the synchronism if multiple threads are trying to modify the same single bean object?

Do you think using prototype scope is better in such a case or does having a single copy of the bean for each client makes it a dead tortoise speedwise?

I am especially interested in high throughput, ultra low latency trading applications where timed execution is of prime essence. Losing a minute (in creating a copy of the bean), could cause the trade to stale out and enter a dead queue, eventuall losing out on $$.

Regards,
DS [SCJP 1.5, SCWCD, SCDJWS]
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
If there is no instance variables, the bean is thread-safe.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Object instantiation in Java is *very* fast. Unless it has very lengthy startup/initialization requirements it's unlikely instantiation would be the cause of any major issues.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Create too much objects can cause issues because each object uses memory.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

"Too many" is pretty vague: typical Java applications create and destroy tens, if not hundreds, of thousands of objects during normal execution.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Every single object uses memory. Amount of memory depends on number of objects multiply concurrent users.
If using some feature of application requires creating 10 objects, if there are 100 concurrent users, there will be 1000 objects created at a given time. But if the objects are singleton there will be only 10 objects regardless the number of concurrent users.

My recommendation is make services stateless if possible and use singleton scope.

Creating too many objects can cause full GC which is very bad for applications that response time is important, and the worst is OutOfMemoryError happens.

I don't think time used in creating objects will take up to a minute (even consider concurrent users), but if combine with full GC/repeately full GC, it's possible.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

if there are 100 concurrent users, there will be 1000 objects created at a given time

Well yeah; I understand math. My point was simply that object instantiation time is almost never an issue unless there's time-consuming initialization.
 
jQuery in Action, 2nd edition
 
subject: Singleton or Prototype for DI bean injection?
 
Similar Threads
Spring factory not creating new objects
Is the managed bean singleton?
Making a bean as Stateful using spring
Bean Scope: Prototype Vs Singleton
Spring - singleton attribute not available when using namespace