1

I have a service which do some work:

@Service
@Transactional
public class FooServiceImpl implements FooService {

  @Override
  public void invoke() { 
      process();
  }

  public void process() {

    // possible that will throw exception
    elements.forEach -> processOne(..)

  }

  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public void processOne(..) {

  // possible that will throw exception

  }

}

The invoke() method is called from another component. As I understand this method is running within transaction (if exist - continute, if not - create new one). But what I expected - method processOne(..) is running in new transaction - so if in this one everything is ok transaction should be commited. But if error occur then whole process is rolled back. Not only from current transaction. What's wrong ?

kxyz
  • 802
  • 1
  • 9
  • 32

1 Answers1

2

processOne method call will not create a new transaction when it is called from the same class, only if it is called directly from an other class.

Also see this question and my answer on it.

From the spring reference manual:

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional. Also, the proxy must be fully initialized to provide the expected behaviour so you should not rely on this feature in your initialization code, i.e. @PostConstruct.

Community
  • 1
  • 1
niekname
  • 2,528
  • 1
  • 13
  • 27