Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to maintain single transaction for multiple users

 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

In my project i have a scenario.

I am maintaining medicine stock.In that the availability of crocin tablet is 50.
2 or more users requesting the crocin tablet at the same time.It shows the availability to all users as 50.
But my intention is while issuing the crocin tablet to the first user then how to display the updated available quantitiy to other users.

Thanks in Advance,
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already answered that, because the thread title says that. You can create a transaction in your update procedure. Alternatively you can put a lock on the database while the number is being altered, and take the lock off again when the update is complete, but I think a transaction would be easier.
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When showing the current stock to all, all will be shown 50. However, when each one of them starts requesting for tablets, you need to have a synchronized block/method that will allow the stock to be updated based on the availability of the tablets.
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks For Your Reply,

I can maintain synchronized method/block.But all the other threads need to wait when a thread is accessing the method/block.
That will show impact on Application Performance,right?Is there any other alternative..
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can use Asynchronous request processing using Message Queues and JMS API.
 
Sudheer Bhat
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your issue method/procedure you need to acquire the lock on the row that has available quantity for Crocin. So the pseudo code would look like below.

select the available quantity with the FOR UPDATE clause.
if available quantity >= requested quantity
logic for issuing the medicine, followed by update of available quantity.
else
raise an exception saying requested quantity cannot be fulfilled with current onhand.

commit;
exception
clean up;
rollback;
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks EveryOne for your Reply,

I am using Ajax request when clicking on submit button.If the first user updates the data then second user will be getting the alert telling him the available quantity is empty.

My Application is limited not distributed.And so no need of using JMS API.Once Again Thanks for your replies.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic