Suppose I have
Country
Id
CanISellProductsToThisCountry
Order
Id
Country
Before adding an order I want to check if I can sell products to the country, so I ask
if (!order.getCountry().getCanISellProductsToThisCountry())
throw new Exception("You cannot order");
session.save(order);
The problem is that I really need to be sure that noone changed the CanISellProductsToThisCountry field since the moment I checked it to the moment the data is commited.
Can I have optimistic concurrency on the CanISellProductsToThisCountry field?
Do I need to do a session.lock() when reading the country to make sure noone changes it and then check it?
Note that if I need to do the latter I need to do that check when I'm in the context of a session, so I cannot put it in another place of my domain model (for example, when setting the order's country).
Thanks
|