• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Doubt on Bean Scopes

 
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Joe Harry
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For more information, you can read http://java.sun.com/docs/books/tutorial/essential/concurrency/.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!