Sobes.tech
Junior — Middle

What resources or operations can be blocked when using pessimistic locking in transactions?

sobes.tech AI

Answer from AI

Pessimistic locking in transactions typically locks resources that are accessed for reading or modification to prevent conflicts during concurrent access. Usually, the following are locked:

  • Database records (table rows) that the transaction reads or modifies.
  • Tables or pages in the database (depending on the lock level).
  • Other resources associated with the transaction, such as files or objects, if application-level locking is used.

In Java, when working with databases via JDBC or ORM (e.g., Hibernate), pessimistic locking can use SQL queries with SELECT ... FOR UPDATE, which lock the selected rows until the transaction completes. This prevents other transactions from modifying the data simultaneously but may lead to waiting and reduced concurrency.

What resources or operations can be blocked when… - sobes.tech