Monica. Shiralkar wrote:Does that mean that if scope is singleton and say 2 multiple users are trying to access the web page the second user will have to wait until first one is done with it? Is this true ?
Monica. Shiralkar wrote:the two users will get the same object and work on the same object which may be an issue.
If a bean in singleton, this is not true. Single bean in shared between them.
Monica. Shiralkar wrote:If it is a singleton will it not happen. Suppose 2 users are accessing the website same time. User A does a banking transaction and does update. User B also does update. User A's data gets updated in user B's account.?
I was under wrong impression that if object is singleton and accessed simultaneously then all users get same Object and it can be modified incorrectly by all users at once whereas in reality all will get different copy of objects and cannot modify each other's object incorrectly. Is that correct.
No. If a bean in singleton, then there is only one object created.
If there is only 1 object created. If User1 tries to access this object user1 will get it. If user2 tries to access this object User2 will get it. If user1 is doing some operation on this object (e.g banking transaction) and user 2 is calling update on this object, will user2 not update user1's data as they are working on same object.
Only one object of Class will be created for singleton and all instance variables will be shared among two different thread (request) but thread do not share local variable instance.
Prasad Krishnegowda wrote:
Only one object of Class will be created for singleton and all instance variables will be shared among two different thread (request) but thread do not share local variable instance.
so, what are you trying to say, its fine even if we have member variables in singleton?
No, it should not be fine, just run the example i provided, you will find it out.
Monica. Shiralkar wrote:Prasad Krishnegowda says
No. If a bean in singleton, then there is only one object created.
If there is only 1 object created. If User1 tries to access this object user1 will get it. If user2 tries to access this object User2 will get it. If user1 is doing some operation on this object (e.g banking transaction) and user 2 is calling update on this object, will user2 not update user1's data as they are working on same object.
thanks
I suggest that you drop Spring for a while, and learn multi-threading. People who designed and teach Spring assume that you are familiar with multi-threading.
Member variables are shared among threads. In Java, every thread gets it's own copy of local variables.
THis means that when both threads call save at the same time, one of them will be saved twice, and other will be lost. This is a bug.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |