Spring @Transactional and Hibernate LockMode class are different.
Spring Transaction Management
@Transactional is Spring's annotation for declarative transaction management, that is, determining which SQL statements are executed together within a database transaction. Using the readOnly attribute allows Spring to throw an exception if you try to insert rows within a read-only transaction, for example.
In regards to locking, most likely you will use a read / write transaction ( readOnly = false ), because you will try to modify the data.
Pessimistic blocking
LockMode used for pessimistic locking, for example. LockMode.UPGRADE actually executes the SELECT...FOR UPDATE and locks the row in the database corresponding to the entity.
Pessimistic locking assumes that parallel transactions will conflict with each other and requires that resources be locked after they are read and only unlocked after the application has finished using data.
Optimistic block
The optimistic concurrency control in Hibernate typically uses the version column or timestamp in the database. The idea here is that if several transactions try to change the row at the same time, all but the first committed transaction will find that the version number has changed and rolled back.
Optimistic locking suggests that multiple transactions can complete without affecting each other, and therefore transactions can continue without locking the data that they affect. Prior to completing a transaction, each transaction verifies that no other transaction has changed its data. If the check shows inconsistent modifications, transaction completion.
Quotes above: https://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html
source share