This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Spring and the fly likes Doubt on Bean Scopes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Doubt on Bean Scopes" Watch "Doubt on Bean Scopes" New topic
Author

Doubt on Bean Scopes

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

Guys,

I have my enterprise application objects scatterred acros multiple tiers (1)web tier - The Action classes (2)service tier - The Facade and Service classes. I clearly understand that each request from the browser should be served by seperate instances of action classes, but what about the facade. Here my facade is responsible for starting the transactions and it is a one to one mapping to the method calls in the service layer. I do not have state in either of my service and facade. The question is which could be modelled as a singleton and which could be modelled as a prototype scope?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Both would be Singleton. and in a Spring architecture your transaction scope is bound to the Service class, not before or after that layer.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Jothi Shankar Kumar wrote:I clearly understand that each request from the browser should be served by seperate instances of action classes

How do you know that? There should be only one action instance per class, creating one action instance per request can be expensive.

Jothi Shankar Kumar wrote:
, but what about the facade. Here my facade is responsible for starting the transactions and it is a one to one mapping to the method calls in the service layer. I do not have state in either of my service and facade. The question is which could be modelled as a singleton and which could be modelled as a prototype scope?

If the services don't have any states, you should use singleton scope.


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
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

But how does this Singleton scope compare against the connecting pooling concept that we employ for databases? Now supposing, I model my facade and service as a singleton and there are two requests coming with each request calling two different methods. Say Request A (the first request) calling method A in the facade layer and Request B (the second request) calling method B in the facade. Now how does Spring handles this? There is just one instance of the Facade....will the Request B call have to wait till the single facade instance finishes servicing Request A??
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

I understand it like this...Spring creates new thread for each subsequent request on the shared instance. Is my understanding correct here?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Jothi Shankar Kumar wrote:I understand it like this...Spring creates new thread for each subsequent request on the shared instance. Is my understanding correct here?


Since most of the beans will be Service and Repository/DAO classes, they don't hold state, and they aren't synchronized, so two threads can be running the same code at the same time, no problems.

If you accidentally put state in a Service or repository (not good design) then you will have a problem. But in that bad design rare case, you can just set the scope to "prototype", but that is only if you are stuck with really bad code someone else wrote. Because we know you won't do that.

Mark
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

It did not quite answer my question. Will the request B to the singleton instance has to wait for the Request A to complete its work so that it (Request B) can get hold of the Singleton instance to call Method B? In the end, it is just sequential processing. But what if Request A has a long running process??? Can you please provide me insights in this direction?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Jothi Shankar Kumar wrote:Will the request B to the singleton instance has to wait for the Request A to complete its work so that it (Request B) can get hold of the Singleton instance to call Method B?

If you do not synchronize your code, the answer is no.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

Kengkaj Sathianpantarit wrote:
Jothi Shankar Kumar wrote:Will the request B to the singleton instance has to wait for the Request A to complete its work so that it (Request B) can get hold of the Singleton instance to call Method B?

If you do not synchronize your code, the answer is no.


Fine, how do you justify your answer. Can you explain me that?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Jothi Shankar Kumar wrote:
Kengkaj Sathianpantarit wrote:
Jothi Shankar Kumar wrote:Will the request B to the singleton instance has to wait for the Request A to complete its work so that it (Request B) can get hold of the Singleton instance to call Method B?

If you do not synchronize your code, the answer is no.


Fine, how do you justify your answer. Can you explain me that?

Why do you think request B has to wait? If you cannot give a valid reason, that can be a reason why it doesn't need to wait.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Will the request B to the singleton instance has to wait for the Request A to complete its work


No, Java does not stop multiple threads from running the same method code in a class.

That is why it is called multithreading. ;)

Mark
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
For more information, you can read http://java.sun.com/docs/books/tutorial/essential/concurrency/.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on Bean Scopes