File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes help needed immediately Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "help needed immediately" Watch "help needed immediately" New topic

help needed immediately

fei long
Ranch Hand

Joined: Apr 04, 2002
Posts: 48
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

Joined: Feb 05, 2001
Posts: 17276

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.

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Val Pecaoco
Ranch Hand

Joined: Dec 05, 2001
Posts: 156
Hi ordin,
I implemented mine using transactions in stored procedures.
Ex Animo Java!
-- Val

"Knowledge is power, but enthusiasm is the key." -- Lavern Barn
fei long
Ranch Hand

Joined: Apr 04, 2002
Posts: 48
Hi, Val Pecaoco
Could you send me your code? or please tell me more detail about how to do. thanks
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You may want to synchronize the part of code involved in decreasing the stock and checking out.

So much trouble in the world -- Bob Marley
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 567
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.

I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
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.
- Raja.

Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 567
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.
I agree. Here's the link:
subject: help needed immediately
It's not a secret anymore!