Spring悲观锁
我在java下有spring项目,使用hibernate查询,我喜欢使用悲观锁定。
如何在 Spring + Hibernate 中进行悲观锁?
编辑:
@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();
}
问题:
我想在方法中使用悲观锁定,并且我从不同的方法调用此方法。当我从第一个方法调用它时,悲观工作正常,但当我从第二个方法调用它时,它会给出(事务无法提交)
异常:
Could not commit Hibernate transaction; nested exception is org.hibernate.
TransactionException: Transaction not successfully started
i have spring project under java, using hibernate query, i like to use pessimistic locking.
How to do 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 to methods. pessimistic works fine when i call it from the first method, but it gives (Transaction couldn't be commit) when i call it from the second method
Exception:
Could not commit Hibernate transaction; nested exception is org.hibernate.
TransactionException: Transaction not successfully started
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://www.amicabile.com/hybernate/hybernate-chapter5.html
http://javacompleteexamples.blogspot.com/2009/07/how-db-locking-system-works-in.html
编辑:
尝试:
http://www.amicabile.com/hybernate/hybernate-chapter5.html
http://javacompleteexamples.blogspot.com/2009/07/how-db-locking-system-works-in.html
Edit:
try: