aspose file tools*
The moose likes JDBC and the fly likes help needed immediately Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "help needed immediately" Watch "help needed immediately" New topic
Author

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
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
e-mail: longf_76@yahoo.com
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Adam Hardy
Ranch Hand

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


SCJP
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
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: http://aspose.com/file-tools
 
subject: help needed immediately