File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Two sessions started simultaneously, updating an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Two sessions started simultaneously, updating an object" Watch "Two sessions started simultaneously, updating an object" New topic
Author

Two sessions started simultaneously, updating an object

ross bertolonoi
Greenhorn

Joined: Nov 24, 2008
Posts: 6
Hi
Part of a web application function is to add an item to a customer object.
Some customer only can have one item. The application checks if the customer already has an item and if so it prevents adding more items.
if the check is passed system does other stuff and at the end it increases the number of item for that customer.

The problem is; if there are two sessions started simultaneously and both try to put an item on the same customer, they can get around this check.
In the first session, the check is performed and is passed. In the other session the check is performed and and is passed as well. At the end of each sessions, the customer end up with 2 items(stored in DB).

How can I solve this issue without keeping a track on the account by for example storing a temporary flag in DB indicting this customer is locked and any other session check for this flag.
Maybe synchronizing the Customer object throughout a session? but that would be too resource expensive

Any ideas ?


Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on the JavaRanch site.

For more information, please read this.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kamlesh yadav
Greenhorn

Joined: Dec 04, 2008
Posts: 2
[Thread hijack removed.]
[ December 04, 2008: Message edited by: Bear Bibeault ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
if there are two sessions started simultaneously and both try to put an item on the same customer,


How would two separate sessions end up with access to the same customer?

Maybe synchronizing the Customer object throughout a session? but that would be too resource expensive


How did you come to that conclusion?

Bill
Srikanth Nutigattu
Ranch Hand

Joined: Oct 30, 2007
Posts: 114
The above situation can arise if the user has opened "multiple windows".

In your method that adds the item to the cart, you can synchronize the two steps using a monitor. as shown below. So, that at any point of time only one thread can update the cart.




Try to avoid synchronizing the entire method since its highly taxing.

HTH.
[ December 07, 2008: Message edited by: Srikanth Nutigattu ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
1. Disallow the 2nd window as soon as you see it
2. Speaking frankly:

Try to avoid synchronizing the entire method since its highly taxing.


Yow, another jump to a conclusion - not a record but still impressive. Given all the other time consuming things that go on between a HTTP request being sent and the response received, I suspect the effect is insignificant.

Bill
Srikanth Nutigattu
Ranch Hand

Joined: Oct 30, 2007
Posts: 114
Yikes! I think I've missed the point. Actually, I was referring to "Avoid Synchronizing of 'service' method of the servlet" But, It�s good practice to avoid sync. of any method entirely, if you could apply the synchronization to couple of steps alone...

As far as the "turn-around" times of http's request-response and other things are concerned, we don't have control on those things (not exactly though..) So, at least we should aim that our code is not one of the 'causes' for a delay...
[ December 09, 2008: Message edited by: Srikanth Nutigattu ]
ross bertolonoi
Greenhorn

Joined: Nov 24, 2008
Posts: 6
Thank you all for your suggestions. But I'm not sure it can work for my situation .
Here is more info and the flow :
-----------------------------------
User accesses the webapp and tries to add an item to the customer.
Servlet-1 does
if(number of items for the customer in DB == 0)
{
allowAddingItem = true;
}
return allowAddingItem flag to user

User sees allowAddingItem is ture and submit the request to servlet-2 to increment the number of items


And here is the problem scenario.
-----------------------------------
Session 1
User-1 accesses the webapp and tries to add an item to customer-1
Servlet-1 does
if(number of items for customer-1 in DB == 0)
{
allowAddingItem = true;
}
return allowAddingItem flag to user
User-1 sees the result and WAITS.
At this time User-2 starts the session-2, and tries to add an item to customer-1
Servlet-1 does
if(number of items for customer-1 in DB == 0)
{
allowAddingItem = true;
}
return allowAddingItem flag to user

Now both users have allowAddingItem flag = ture on their screen and submit the request to servlet-2 to increment the number of items.

Notes :
-I can't add the check in servlet-2 as it is a thrid party app that I can't change.
-The only place I can make any changes is in Servlet-1.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3859

If you don't have access to servlet-2 then you better get the 2nd request (which is for increment the number of items) to the servlet-1(or any other servlet which you have access to) and after doing the necessary validations forward it to the servlet-2 for actual update...?
[ December 09, 2008: Message edited by: Vijitha Kumara ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
ross bertolonoi
Greenhorn

Joined: Nov 24, 2008
Posts: 6
my hands are tight . I can't change the flow of requests.(Above I've simplied the flow significantly, there are many black box components involved which can not be touched).

How about keeping allowAddingItem as a global parameter between sessions?
Servlet-1 looks it up using customerid and if this parameter is true, it doesn't set allowAddingItem to true.
Servlet-1 sets this global param to true if the condition is true.

allowAddingItem = get value of allowAddingItem for customerID.
if(number of items for customer-1 in DB == 0 && allowAddingItem is not true)
{
allowAddingItem = true;
add allowAddingItem for customerid
}

Although I'm not sure if this is a good idea and how it should be implemented!!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3859

That's sounds OK (by looking at the conditions given by you),but you will have to check very accurately for all the scenarios (with pros & cons ) like how you are going to reset the "allowAddingItem" if necessary or do you need to do it and so on.... specially things may occur in the real world when the application is running .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two sessions started simultaneously, updating an object