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

help needed immediately

 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am writing a e-commerce program using jsp, servlet. There is a problem about inventory(stock). Please help me. i want to decrease the stock when customer check out, the problem how to make sure the database consistent, i mean there will be two customer check out(decrese the stock) at the same time. Is there any method to lock the table when one is checking out?
thanks in advance.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I don't have enough experience to show you exactly how to do it, but look at Transactions. You can place the checkout method into a transaction. When it is done and everything is saved then you can commit it, otherwise you can rollback any changes.
JTA is the Transaction api. If you use EJBs it is even easier because the container can handle the transactions for you.
Mark
 
Val Pecaoco
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ordin,
I implemented mine using transactions in stored procedures.
Ex Animo Java!
-- Val
 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Val Pecaoco
Could you send me your code? or please tell me more detail about how to do. thanks
e-mail: longf_76@yahoo.com
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to synchronize the part of code involved in decreasing the stock and checking out.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your database and jdbc driver support transactions, then set AutoCommit to work and you will be using automatic transactions. There won't be a problem if you are simply doing two inserts simultaneously.
If you have more than one insert or update as part of the checkout process, autocommit won't be enough and you will have to turn it off and do the commit or rollback yourself on the connection object after the checkout.
HTH
Adam
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need a way to Synchronize the users accessing the database. Why can't you use Threads (monitors - wait, notify) in the place where users access databse. By this way, only the user who has lock can access the database.
Thanks,
- Raja.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The advantage of using database transactions rather than synchronizing the java code is that the transactions method has the additional advantage of being able to rollback both the checkout and the stock decrement if either of them fails, which means your data will stay clean in the event of a problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic