It's probably a the duplicate of a subject but i can't find an answer in google. (Probably using the wrong words)
I'm working on a Java REST API and i have two java object stored in my database. An appointment and an availability. To make an appointment i need to check if my user is available.
public void addAppointment(Date startDate, Date endDate, User user)
{
if(availabilityService.isAvailable(startDate, endDate, user)
dao.addAppointment(startDate, endDate, user);
}
Everything is ok if I only have one instance of my services. But what if i had two instance and they check at the same time my method before storing the new appointment ?
How to manage the concurrency with object that are stored in my database ?
I have no idea if it will change something but i am planning to use Google App Engine or AWS to deploy my application.
Thx.
The typical way to handle this issue is by versioning the records. A strategy called optimistic locking. When you read the user, there should a be a version for that user. You then make the changes you want. When you go to write it, if the version numbers don't match, then you know that someone else has updated that record since you read it. The write fails. You re-read the record to get the updates and you try again (if it makes sense with the new data).
Many libs for dealing with databases will have this built in:
For AWS Dynamo DB: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html
If you plan to bring your own db, hibernate is a java ORM that implements this type of locking: http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#entity-pojo-optlock
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments