WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java JDBC · Advanced · question 50 of 100

What are the differences between optimistic and pessimistic locking strategies in JDBC, and when should each be used?

📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

Optimistic and pessimistic locking are two different strategies for managing concurrent access to data in a database, and both can be used in JDBC to ensure data consistency.

Pessimistic locking is a strategy where a database transaction acquires a lock on the data it accesses immediately, and holds it until the transaction completes. This can be achieved through the use of a SELECT ... FOR UPDATE statement in your JDBC code. While the lock is held, other transactions cant modify the data or read it for any purpose until the lock is released, which can lead to concurrency issues when many transactions are trying to access the same data. This strategy is particularly useful when data modifications are expensive, as it allows transacting systems to block and wait until they hold the resource exclusively.

On the other hand, optimistic locking involves assuming that there won’t be any concurrency issues and only checks for them at the last minute. Here, a transaction reads a particular database record and keeps a marker of the original version of the data. When the transaction is completed, the JDBC driver compares the original version marker with the current version of the data in the database. If they match, the transaction is considered successful and proceeds; otherwise, the transaction fails and rolls back. In optimistic locking, the database is not locked, which allows multiple transactions to read the same data concurrently, and only conflicts during writes will be detected.

The choice of which strategy to use depends on various factors like the frequency and complexity of data modifications and the frequency of concurrent users accessing your data. If concurrency issues are expected to be rare or if read-only data is frequently accessed, then optimistic locking is best suited. If, however, data modifications are intensive, and concurrency issues are expected, pessimistic locking is a better fit.

As an example, consider a banking application where customers make money transfers into their accounts via an online payment portal. In this case, pessimistic locking would be a good strategy to use since the database records are being modified, which would lead to possible concurrent conflicting transactions. The records’ modification could be extensive and lock the transaction until all the changes are made, ensuring data integrity. On the other hand, if you are implementing a search engine, optimism locking would be better since the amount of data being modified is minimal, a situation where multiple transactions can read the same data concurrently, and the risk of conflicting transactions is low.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java JDBC interview — then scores it.
📞 Practice Java JDBC — free 15 min
📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

All 100 Java JDBC questions · All topics