Part of a web application function is to add an item to a customer object.
Some customer only can have one item. The application checks if the customer already has an item and if so it prevents adding more items.
if the check is passed system does other stuff and at the end it increases the number of item for that customer.
The problem is; if there are two sessions started simultaneously and both try to put an item on the same customer, they can get around this check.
In the first session, the check is performed and is passed. In the other session the check is performed and and is passed as well. At the end of each sessions, the customer end up with 2 items(stored in DB).
How can I solve this issue without keeping a track on the account by for example storing a temporary flag in DB indicting this customer is locked and any other session check for this flag.
Maybe synchronizing the Customer object throughout a session? but that would be too resource expensive
Any ideas ?