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.
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
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
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.
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 by:autobot
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
a bit of art, as a gift, the permaculture playing cards