Optimistic Lock vs Pessimistic Lock – Application Module in Oracle ADF

ADF BC (Application module) manage the state of data. It have two locking mechanism -Optimistic Lock and Pessimistic Lock.Locking is Database specific feature which prevents users from different transactions to modify the same data concurrently.
In ADF , we will specify the locking mode either as optimistic/Pessimistic in adf-config.xml file.

We can the locking mode in Application Module -> Configuration -> AppModuleLocal/Shared -> Properties tab.

For the property, jbo.locking.mode it will show either optimistic /pessimistic. It will take the value overridden in adf-config.xml file.. It have same concept of database. Database also have same two locking mechanism.
Now understand each other –

Pessimistic Locking is when you lock the record for your exclusive use until you have finished with it. It has much better integrity than optimistic locking but requires you to be careful with your application design to avoid Deadlocks. To use pessimistic locking you need either a direct connection to the database (as would typically be the case in a two tier client server application) or an externally available transaction ID that can be used independently of the connection.

Example- user A (travel agent) try to book last flight ticket to London.He started the transaction but before clicking book the ticket, he got a phone call and talking. He got an lock of that data.
User B (Another travel agent) tries to book same ticket and wanted to make an lock to that row and he will get

oracle.jbo.AlreadyLockedException. . So its a situation of deadlock.

The disadvantage of pessimistic locking is that a resource is locked from the time it is first accessed in a transaction until the transaction is finished, making it inaccessible to other transactions during that time.

Optimistic Locking is a strategy where you read a record, take note of a version number (other methods to do this involve dates, timestamps or checksums/hashes) and check that the version hasn’t changed before you write the record back. When you write the record back you filter the update on the version to make sure it’s atomic. (i.e. hasn’t been updated between when you check the version and write the record to the disk) and update the version in one hit.
If the record is dirty (i.e. different version to yours) you abort the transaction and the user can re-start it.

This strategy is most applicable to high-volume systems and three-tier architectures where you do not necessarily maintain a connection to the database for your session. In this situation the client cannot actually maintain database locks as the connections are taken from a pool and you may not be using the same connection from one access to the next.

example-
user A (travel agent) try to book last flight ticket to London.He started the transaction but before clicking book the ticket, he got a phone call and talking. Here it will not try to acquire lock.

User B (Another travel agent) tries to book same ticket and he click commit. Ticket booked.Changes will happen in Database. But now user A will try to book the ticket , he will get oracle.jbo.RowInconsistentException.

which one to use
Oracle recommends using optimistic locking for web applications. Pessimistic locking, which is the default, should not be used for web applications as it creates pending transnational state in the database in the form of row-level locks.