Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @TransactionAttribute(value = TransactionAttributeType.NEVER) on a method

Can you call a method that requires a transaction inside a method that does not?

@TransactionAttribute(value = TransactionAttributeType.NEVER)
public void DoSomething(final List<Item> items) {

//can you call a method that requires a transaction here ?
for (Item i : items) {
    methodCall(item);

}

@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void methodCall(final Item item) {
    // access lazily loaded item properties
    item.getSalesOrder();
    item.getAllocation();

    //throws org.hibernate.LazyInitializationException: could not initialize proxy - no Session

}

The .NEVER attribute says it will guarantee the method does not run inside a transaction but what about calls to other methods inside that method

like image 280
Luke Avatar asked Jun 23 '11 13:06

Luke


1 Answers

The annotation only defines the required transaction state which must exist when the annotated method is invoked (in this case a transaction must not exist). It does not restrict what may occur within the execution of the annotation method. So within this method you could start a new transaction without any problem.

In the example that you provided, you may call a method which requires a transaction from within a method that has a transactional setting of NEVER. In this situation, a new transaction will be created for the method call that requires the transaction. If the inner method is marked with a MANDATORY setting, then the inner method call will fail as an existing transaction does not exist and the MANDATORY setting does not automatically create one for you.

like image 151
Kris Babic Avatar answered Nov 02 '22 07:11

Kris Babic