Skip to content

Latest commit

 

History

History

HibernateSpringBootVersionedOptimisticLockingAndDettachedEntity

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Versioned Optimistic Locking And Detached Entities Sample

Description: This is a sample application that shows how versioned (@Version) optimistic locking and detached entity works. Running the application will result in an optimistic locking specific exception (e.g., the Spring Boot specific, OptimisticLockingFailureException).

Key points:

  • in a transaction, fetch an entity via findById(1L); commit transaction and close the Persistence Context
  • in a second transaction, fetch another entity via findById(1L) and update it; commit the transaction and close the Persistence Context
  • outside transactional context, update the detached entity (fetched in the first transaction)
  • in a third transaction, call save() and pass to it the detached entity; trying to merge (EntityManager.merge()) the entity will end up in an optimistic locking exception since the version of the detached and just loaded entity don't match

If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.