Java Reference
In-Depth Information
The easiest way to implement optimistic locking is to use a version column.
But it is not always possible to add a version column to a legacy schema that
you have no control over. What's more, you might not be able to modify the
legacy applications that also use the schema to increment the version column.
Optimistic locking does not guarantee that a transaction will be able to
update the rows that it read. If those rows have been changed by another
transaction, it will have to start over, which can be inefficient.
Optimistic locking does not prevent inconsistent reads. Fortunately, many
applications can tolerate some amount of inconsistency.
When to use it
Despite these drawbacks, optimistic locking is a useful concurrency mechanism. A
general recommendation is that an application should use optimistic locking
unless:
The database schema does not support optimistic locking. It's a legacy
schema whose tables have columns that contain values such as floating-
point values that cannot be compared and you cannot add a version or
timestamp column.
The application must be guaranteed to be able to update the rows that it
read.
The application requires consistent reads.
12.1.3
Pessimistic locking
When optimistic locking won't work, another way to handle concurrent updates is
by using pessimistic locking. As the name suggests, this mechanism assumes that
concurrent updates will occur and so incurs an overhead regardless of whether
they do. However, this overhead is much less than with fully isolated transactions.
A transaction that uses pessimistic locking locks the rows that it reads, which pre-
vents other transactions from reading and updating them. Other transactions will
block until the transaction releases those locks by either committing or rolling
back. Pessimistic locking prevents lost updates and provides some degree of read
consistency because it prevents the read rows from being changed by other trans-
actions. However, because pessimistic locking does not prevent new rows from
being inserted, re-executing the same query might return different results.
 
 
 
 
Search WWH ::




Custom Search