Optimistic Locking — @Version — Concept Check
Questions on Optimistic Locking — @Version. @Version on an integer/timestamp field enables optimistic locking; Hibernate includes the version in UPDATE WHERE clauses and throws OptimisticLockException on concurrent modification.
Questions in this quiz
8 questions on Optimistic Locking — @Version — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer adds a @Version field to an entity in a Hibernate application. What happens if two transactions try to update the same record simultaneously?
- AOne transaction waits for the other to complete.
- BOne transaction succeeds, the other throws OptimisticLockException.
- CBoth transactions fail with OptimisticLockException.
- DBoth transactions succeed, updating the record twice.
2.In a Spring REST API, how should you handle an OptimisticLockException?
- AReturn HTTP 500 Internal Server Error.
- BIgnore the exception and continue processing.
- CReturn HTTP 409 Conflict to the client.
- DRetry the operation immediately without notifying the client.
3.A developer notices that their application frequently throws OptimisticLockException. What might be a reason for this?
- ADatabase not supporting transactions.
- BHigh conflict rate due to simultaneous updates.
- CIncorrect implementation of pessimistic locking.
- DIncorrect use of @Id annotation.
4.What is the primary advantage of using optimistic locking over pessimistic locking in web applications?
- AIt maximizes concurrency without holding database locks.
- BIt prevents all types of data conflicts.
- CIt always performs faster than pessimistic locking.
- DIt is easier to implement than pessimistic locking.
5.A transaction updates a BankAccount entity's balance. The SQL generated includes 'AND version = 0'. What does this indicate?
- AThe transaction will always succeed, ignoring the version check.
- BThe transaction will succeed only if the version is 0 in the database.
- CThe transaction is using pessimistic locking.
- DThe transaction is attempting to delete the record.
6.Why should you not manually set the @Version field in a Hibernate entity?
- AIt will lead to immediate data loss.
- BManually setting it will disable all database transactions.
- CIt will cause the application to switch to pessimistic locking.
- DHibernate automatically manages the version field for concurrency control.
7.Which scenario is most suitable for pessimistic locking instead of optimistic locking?
- AHigh conflict scenarios like seat booking.
- BRead-heavy operations with infrequent updates.
- CProduct description edits by multiple users.
- DUser profile updates with low conflict rate.
8.What happens when a Hibernate update query with optimistic locking affects 0 rows?
- AThe database automatically retries the update.
- BHibernate throws an OptimisticLockException.
- CThe transaction is silently retried.
- DThe transaction is committed without changes.