3

In our java project we are using ORM with hibernate and spring. I had problems in deleting persistent objects. For example this sample method gets entities by ids and then delete them:

@Transactional
public void remove(List<Long> ids) {
    SearchTemplate template = new SearchTemplate();
    template.addParameter("milestoneId",ids);
    List <InvoiceQueue> items = this.findByCriteria(template);
    ...
    this.delete(items);
}

Method executes Ok without any exception but doesn't actually delete the items from the DB.

Adding the following annotation to the method definition @Transactional(propagation = Propagation.REQUIRES_NEW) solves the problem.

Can anyone explain why it doesn't work with the default propagation type PROPAGATION_REQUIRED.

Thanks in advance.

Environment details :

hibernate.version 3.5.5-Final, spring.version 3.0.5.RELEASE

4

1 回答 1

2

Really just repeating what @PeterBagyinszki said in his comment, but the reason quite probably is that the transaction within which your delete occurs gets rolled back due to some other part throwing an exception, and all the changes made during the transaction get canceled. With Propagation.REQUIRES_NEW, the delete is done within it's own separate nested transaction. The outcome of the nested transaction (committed or rolled back) won't affect the "outer" transaction and vice versa.

Check your logs to see what is causing the transaction to be rolled back, note that even something like a simple SELECT -query failing with something like NoResultException will cause the transaction to roll back, unless you explicitly state in the @Transactional-annotation it not to roll back on certain exceptions.

于 2012-05-22T07:58:49.653 回答