春天悲观锁 [英] Spring Pessimistic locking

查看:183
本文介绍了春天悲观锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何在Spring + Hibernate中做悲观锁定?



编辑:

  @Loggable(value = LogLevel .TRACE)
@Transactional
@Override
public void updateBalance(String id,BigDecimal amount){
Session session = sessionFactory.getCurrentSession();
sessionFactory.openSession();
事务tx = session.beginTransaction();
session.flush();
帐户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();
}

问题:



我想在方法中使用悲观锁,我从不同的方法调用这个方法。悲观工作正常,当我从第一个方法调用它,但它给出(事务不能提交)当我从第二个方法调用



异常:

 无法提交Hibernate事务;嵌套异常是org.hibernate。 
TransactionException:事务未成功启动


解决方案

a href =http://www.amicabile.com/hybernate/hybernate-chapter5.html =nofollow> http://www.amicabile.com/hybernate/hybernate-chapter5.html



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



< b>编辑:



请尝试:

  @覆盖
@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);
}


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

解决方案

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

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

Edit:

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);
}

这篇关于春天悲观锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆