Spring Pessimistic Lock

I have a spring project under java using a sleeping request, I like to use pessimistic locking.

How to make pessimistic locking in spring + Hibernate?

Edit:

@Loggable(value = LogLevel.TRACE) @Transactional @Override public void updateBalance(String id, BigDecimal amount) { Session session = sessionFactory.getCurrentSession(); sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.flush(); Account acc = (Account) session.get(Account.class, id, LockMode.UPGRADE); acc.setName("New Account"); acc.setBalance(acc.getBalance().subtract(amount)); save(acc); try{ tx.commit(); }catch (TransactionException e){ tx.rollback(); } session.close(); } 

Problem:

I want to use pessimistic locking in a method, and I call this method from different methods. pessimistic works fine when I call it from the first method, but it gives (the transaction cannot be committed) when I call it from the second method

An exception:

 Could not commit Hibernate transaction; nested exception is org.hibernate. TransactionException: Transaction not successfully started 
+4
source share
1 answer

http://www.amicabile.com/hybernate/hybernate-chapter5.html

http://javacompleteexamples.blogspot.com/2009/07/how-db-locking-system-works-in.html

Edit:

to try:

 @Override @Loggable(value = LogLevel.TRACE) @Transactional public void updateBalance(String id, BigDecimal amount) { Account acc = (Account) sessionFactory.getCurrentSession().get(Account.class, id, LockMode.UPGRADE); acc.setBalance(acc.getBalance().subtract(amount)); save(acc); } 
+3
source

All Articles