Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

482
Vistas
How to disable optimistic locking in Spring Data JPA

Optimistic Locking annotation does not work.

@OptimisticLocking(type = OptimisticLockType.NONE)
public class TestEntity {
    ....
}

and @Lock annotation does not work either:

public interface TestRepository<TestEntity, Long> extends JpaRepository<Version, Long> {
    @Lock(LockModeType.NONE)
    TestEntity findByName(String name);
}

So, I try to call:

entityManager.refresh();

It works, but it's a workaround.

EntityManager em = sharedEntityManagerBean.getObject();
em.refresh(testEntity, LockModeType.OPTIMISTIC);
testRepository.save(testEntity);

Could you tell me why optimistic lock annotation does not work and what's the best way to update DB row(entity) with the latest data?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The OptimisticLockType.NONE disables the default optimistic locking mechanism for the TestEntity.

However, that would only be useful if you inherited the @Version property from a base class which is annotated with @MappedSuperclass or @Inheritance.

In your case, you could simply remove the @Version property if you don't want optimistic locking for this entity. However, that's usually a bad idea since it could lead to lost updates.

Maybe you want to use the versionless optimistic locking which can lower the rate of conflicts generated by non-overlapping property chanages.

Again, the @Lock(LockModeType.NONE) is useless since it's implied by default. You can remove that as well. That's only meant for acquiring an explicit logical or physical lock.

You are drawing the wrong conclusions thinking that optimistic locking is causing an issue which you didn't even describe.

Therefore, you need to formulate the question in a proper manner so that it's clear:

  1. What is the actual problem that you are trying to solve?
  2. Do you want optimistic locking or not?
  3. Do you get a particular exception when doing the save?
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda