简体   繁体   English

事务管理Spring 3-Hibernate 3.5

[英]Transaction management spring 3 - hibernate 3.5

I'm using spring 3 with hibernate 3.5.4 我正在将Spring 3与Hibernate 3.5.4结合使用

1- I want to create an object in transaction and save it to DB ( which passes successfully ). 1-我想在事务中创建一个对象并将其保存到数据库(成功通过)。 2- I want to update some fields in that object (same object) and updates in in DB in another transaction (and here is the problem). 2-我想更新该对象(同一对象)中的某些字段,并在另一个事务中在DB中更新(这就是问题所在)。

The problem is, is saves the object successfully in the first transaction but it doesn't update it in DB in the second one. 问题是,在第一个事务中成功保存了对象,但是在第二个事务中却没有更新该对象。

here is code example: 这是代码示例:

public String entry(String str){
    Bill b = create(str);
    b = update(b);
    b = updateAgain(b);

    return "DONE";
}

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public Bill create(String num){
    Bill bill = new Bill();
    bill.setBillNumber(num);
    baseDao.saveObject(bill);
    return bill;
}

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public Bill update(Bill bill){
    bill.setRetailAmount(152.0);
    baseDao.saveObject(bill);
    return bill;
}

NOTE: I don't want to put the @transactional annotation on method "entry". 注意:我不想将@transactional注释放在方法“ entry”上。

Thanks, 谢谢,

The annotation will not take affect if called on a method within the same class. 如果在同一类中的方法上调用,则注释不会生效。 AOP cannot intercept that through proxy. AOP无法通过代理对其进行拦截。 Move your entry method outside the class. 将输入方法移到类之外。

EDIT: Spring enables the Transactional annotation via annotation-driven AOP with proxies or sub-classing. 编辑:Spring通过带有代理或子类的注释驱动的AOP启用事务注释。 When enabled with proxies, your proxy is out of the picture in a local method call. 与代理一起启用时,您的代理不在本地方法调用中。 This blog post has a good explanation with pictures. 这篇博客文章对图片有很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM