A Persistence application locks entity x with a LockModeType.PESSIMISTIC_READ lock type.
Which statement is true?
Click on the arrows to vote for the correct answer
A. B. C. D.B.
https://docs.oracle.com/javaee/7/api/javax/persistence/LockModeType.htmlWhen an application locks an entity using LockModeType.PESSIMISTIC_READ, it means that it is requesting a shared lock on the entity, which will prevent other transactions from acquiring an exclusive lock on the same entity. This lock mode is used to ensure that data is not modified while a transaction is reading it, and to ensure that any changes made to the data are visible to the transaction.
Now, let's go through each of the answer choices to see which one is true:
A. LockModeType.PESSIMISTIC_READ is the synonym of LockModeType.READ This statement is false. LockModeType.PESSIMISTIC_READ and LockModeType.READ are not synonyms. LockModeType.READ is a weaker lock mode than LockModeType.PESSIMISTIC_READ, and it does not prevent other transactions from acquiring an exclusive lock on the same entity.
B. This operation will force serialization among transactions attempting to read the entity data. This statement is partially true. When an entity is locked with LockModeType.PESSIMISTIC_READ, it does force serialization among transactions attempting to modify the same entity data, but not among transactions attempting to read the entity data. However, the lock can still cause other transactions to wait for the lock to be released before they can proceed.
C. This operation will result in a TransactionRolledbackException if the lock cannot be obtained. This statement is false. If the lock cannot be obtained, the transaction will block until the lock can be obtained, or until a timeout occurs.
D. If the application updates the entity later, and the changes are flushed to the database, the lock will be converted to an exclusive lock. This statement is false. LockModeType.PESSIMISTIC_READ is a shared lock mode, which means that it will not be converted to an exclusive lock when the entity is updated later. If the application wants to obtain an exclusive lock on the entity, it must request a lock with LockModeType.PESSIMISTIC_WRITE.
In summary, the correct answer is B. LockModeType.PESSIMISTIC_READ does not force serialization among transactions attempting to read the entity data, but it does force serialization among transactions attempting to modify the same entity data.