Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Managing the product count in the database

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

Sample code


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?
 
Marshal
Posts: 74783
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Other people will doubtless be able to help more.
 
Sheriff
Posts: 26973
85
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
shivang sarawagi
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the response guys I believe handling this scenario at the db level would be the best. Setting up an appropriate database transaction isolation level & lock.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic