Question

I'm using spring 3 with hibernate 3.5.4

1- I want to create an object in transaction and save it to DB ( which passes successfully ). 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).

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".

Thanks,

Était-ce utile?

La solution

The annotation will not take affect if called on a method within the same class. AOP cannot intercept that through proxy. Move your entry method outside the class.

EDIT: Spring enables the Transactional annotation via annotation-driven AOP with proxies or sub-classing. 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top