Excuse me if this question may seem naive but I have come across a scenario where I need to manage the product count in the database of an e-commerce store.
There is a Product class with an integer variable productCount which signifies the number of available products in the database which is visible to users of the site. Now this class is accessed by several threads or can say several users of the e-commerce site. Everyone is adding or removing the product to his cart.
The ORM framework being used is hibernate
As it is clear from the code that I need to keep a concurrency check on the product count in the database to prevent lost updates.
Also if several users are trying to add only single left product in the database. Which user's cart the product should be added to?
I did a little research on this
Possible ways I found were
Creating a singleton class for Product. That would ensure that just one instance of product is available throughout the application.
Synchronize the & methods. which would allow only one thread to update the product count & update the db at a time.
Use database concurrency control apply some db transaction isolation level, optimistic/pessimistic locking for the productCount. I am using mysql the default isolation level is .
What would be the best approach to deal with this?
shivang sarawagi wrote:Excuse me if this question may seem naive . . .
It's a perfectly good question.
Possible ways I found were
Creating a singleton class for Product. That would ensure that just one instance of product is available throughout the application. . . .
That sounds more like a possible problem.
Synchronising the Java® code does not allow only one thread to update things; it allows only one thread at a time to access the object's data. You should consider making the object immutable, which will mak it thread‑safe.
It is a very long time since I did any database programming but maybe converting the update to a transaction which will be “ACID” would be a good solution.
When I had a similar problem (namely getting the next number from a sequence in the database, to use as an order number) I eventually found that to avoid two customers getting the same number from the sequence it was necessary to flush the session immediately after getting the number. Waiting for the session commit to do that was too late.
You may find that a singleton works for you, although I don't know how to tell Hibernate to use a singleton. But when somebody decides that your application needs to run in more than one instance of whatever's running it, then you won't have a singleton any more and you'll be back looking for a solution again.
However upon rereading your post I find I'm a bit confused. You've got a variable which tells you how many products are in the database... this should be constant, no? Even when somebody buys something the database still has the same number of products, doesn't it? Suppose you're selling blue bricks and red bags and yellow balloons. Then there are three products in the database. When somebody buys a yellow balloon you still have the same three products in the database.
Or perhaps you're only selling blue bricks, and that variable tells you how many blue bricks are available? If that's the case then I'd advise storing the number in the database and updating it using normal database transactions.
It's not the number of Products.
It's the number of a Product.
So there are 18 blue bricks, but only 6 yellow balloons.
When someone adds a blue brick to their cart then the number available should be reduced by 1.
This is fairly standard db stuff, though.
When someone adds a product to the cart then it should be deducted from the number available.
If someone else has got in before hand then a prompt should be sent to the first person that the product has gone.