I wonder what’s the best practice to make a db update thread safe.
I am calling a stateless session bean’s
public void addReservation(Reservation reservation)
Before adding this reservation – I would like to check if there are still valid places for that reservation, using an entity bean’s method
public boolean isThereRoomForAnotherReseravtion()
I read here that synchronized method will not solve the problem.
I thought of using stored procedure with a validation rule for the solution, but it doesn’t sound right.
Is there a standard practice for making write procedure thread safe?
Regards,
Danny
No Java synchronization can actually help you since there can always be some second database client (maybe even your own application in clustered environment) that can access the database simultaneously.
You need to go deeper on the database layer and use database locking facilities like optimistic/pessimistic locking. JPA 1.0 supports the former, 2.0 supports both. The choice depends on how much concurrency you suspect to experience.