Is it possible to use PESIMISTIC lock option using Spring JpaTemplate methods?
I know that PESIMISTIC lock can be performed using EntityManager’s methods e.g.
Account acc = em.find(Account.class, 123);
em.lock(acc, PESIMISTIC);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s nothing specifically on
JpaTemplatefor this, but if you need access to it, you can useJpaTemplate.execute(), which takes a callback which is supplied with theEntityManager, and you can do anything you like within that callback.A better solution, depending on your situation, might be to use Spring’s transaction layer. If you annotate your DAO with
@Transactional(see previous link), theJpaTransactionManagershould manage entity locking for you, depending on theisolationattribute of the@Transactionalattribute.