Reputation: 8585
Using JPA optimistic locking we can control via a @Version field if a database table has been updated by another transaction at the same time, allowing to have reliable data stored in database.
If a Java application has only one CRUD service in charge of an specific entity in database, we could synchronize its methods and manage the order the information is stored in database too.
So my question is, what's the difference between those scenarios? Does exist any advantage of performance or even best practices to follow?
Upvotes: 4
Views: 3558
Reputation: 1519
Think of synchronization as pessimistic locking: you have to reserve the lock before you start working as opposed to checking if you violated the lock only when you finished working (optimistic lock during commit). The two serve very different purposes:
In general: do not use synchronization to lock on entities, there is pessimistic lock support in JPA which locks on the actual DB row: http://docs.oracle.com/javaee/6/tutorial/doc/gkjiu.html
Upvotes: 1
Reputation: 23552
Drawbacks of method synchronization:
Upvotes: 3