fooBar
fooBar

Reputation: 13

spring repository transactions in service transaction

I'd have 1 question:
the implicit methods in spring data jpa repositories (findAll, save, etc.) are @Transactional. So in this case:

@Transactional
public void atomicTransaction() {
    repository1.save(entity);
    repository2.save(entity);
}

if repository2#save fails and rollbacks, is it propagated to the atomicTransaction() so that reposotory1#save also rollbacks?

Upvotes: 0

Views: 559

Answers (1)

NiVeR
NiVeR

Reputation: 9786

The save method of jpa repository will open a micro-transcation unless it is already in a transactional context. In your case you have annotated the method with @Transactional, so what that means is that it will be seen as an atomic unit of work, that is it either executes completely or it rollbacks in case of errors.

Upvotes: 1

Related Questions