Reputation: 2975
In a webapp I'm using Spring 4.3.9, and Hibernate 5.2.10 as JPA provider.
In a bean I have the following class and method:
@Repository
public class VentaDAO {
@PersistenceContext
private EntityManager em;
@Transactional
public void tryAprobarPago(Decidir decidir) {
long idAlmuerzoVenta = Long.parseLong(decidir.getSistemaClave());
AlmuerzoVenta almuerzoVenta = em.find(AlmuerzoVenta.class, idAlmuerzoVenta);
Almuerzo almuerzo = almuerzoVenta.getAlmuerzo();
almuerzo.assignNroEntradas(almuerzoVenta);
almuerzoVenta.setEstado(AlmuerzoVenta.Estado.APROBADA);
almuerzoVenta = em.merge(almuerzoVenta);
// force flush to detect potential version conflics BEFORE execute costly SQL operations
em.flush();
methodWithCostlySQLOperations();
}
}
When using optimistic locking AND there is a version conflict, the @Transactional method throws Spring's ObjectOptimisticLockingFailureException
when the database flush happens automatically after the method finishes execution, however if I insert a manual flush() in the middle, Spring does NOT intercept the JPA's OptimisticLockException
?
This behavior makes optimistic locking handling problematic since the exception changes depending on how the method is codified.
I would prefer to handle only ObjectOptimisticLockingFailureException
.
Upvotes: 2
Views: 1246
Reputation: 5232
I think if you want Spring to do automatic error handling (exception translation) you need to annotate your class with @Repository
Upvotes: 1