Is Hibernate using pessimistic or optimistic locking?

This exception is due to Lock TimeOut.

Caused by: org.h2.jdbc.JdbcSQLException: Timeout trying to lock table ;

Check a solution here

Also hibernate provides mechanisms for implementing both types of locking in your applications.

Your locking strategy can be either optimistic or pessimistic.

Optimistic

Optimistic locking assumes that multiple transactions can complete without affecting each other, and that therefore transactions can proceed without locking the data resources that they affect. Before committing, each transaction verifies that no other transaction has modified its data. If the check reveals conflicting modifications, the committing transaction rolls back[1].

Pessimistic

Pessimistic locking assumes that concurrent transactions will conflict with each other, and requires resources to be locked after they are read and only unlocked after the application has finished using the data.

Details can be found here


You are using optimistic locking, as your UPDATE statement already indicates:

where recNo=? and version=?

The presence of the version column is what optimistic locking is all about.

You were misled by the PessimisticLockException which might be caused by an explicit or an implicit row-level lock.