Question

public class BusinessService {  //spring bean

  public dumpAllData(List){

    /* Complicated DB operation here
     * We dont want to be in transaction now (because of performance issues)
     */ 

    for(...){           //iterating through whole list
      **updateItem(item);**
    }

  }

  public updateItem(Entity e){
    //saves entity into DB
    //we want to be in transaction now
  }

}

Spring configuration :

<tx:advice id="txAdvice" transaction-manager="wsTransactionManager">
    <tx:attributes>           
      <tx:method name="dumpAllData" propagation="NOT_SUPPORTED" />
      <tx:method name="updateItem" propagation="REQUIRES_NEW" />
    </tx:attributes>
</tx:advice>

Is possible to have nested REQUIRED_NEW propagation which will be called from method with propagation NOT_SUPPORTED ?

Thing is we run an extensive DB operation (~ 100Mb) in dumpAllData() so we dont want to be in transaction (oterwise it would be performance issue). But we want to be in transaction (rollback/commit) in updateItem method (where we do just simple update of entities).

Was it helpful?

Solution

I fail to see how being inside a transaction or not has an incidence on performance. Have you measured a performance difference, or are you just guessing?

Anyway, if you really need to do this, then the updateItem method should be in another Spring bean, injected into the BusinessService bean.

Indeed, Spring is only able to start/commit a transaction when a bean method is called through a proxy. If you call a bean method from another method of the same bean, Spring can't intercept the call and do its transaction management.

OTHER TIPS

Your transaction annotation in update method will not be intercept by Spring transaction infrastructure if called from some method of same class. For more understanding on how Spring transaction works please refer to Spring Transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top