Reputation: 1
I have a strange behavior that i don't understand because is seems to not behave like in the documentation that i've read, i'm using springboot 3.2.4
I have a service class with a methodA marked with springboot @Transactional and witch save an entity by callling the entity repository (witch extends PagingAndSortingRepository), this methodA call a private methodB witch save another entity by callling the entity repository (witch extends JpaRepository) If at the end in methobB i throw a RuntimeException to test transactional behavior, the 2 insertions in database are rollbacked, but i've always thought that private method doesn't participate to a transaction, and in this case it seems to participate to the transaction of methodA and i don't know why ??
@Service public class MyService {
@Transactional
public void methodA() {
EntityA entityA = new EntityA();
entityARepository.save();
methodB();
}
private void methodB() {
EntityB entityB = new EntityB();
entityBRepository.save();
throw new RuntimeException();
}
}
The insertion of 2 entities are rollbacked, not only EnitytA but also EntityB
When i put log of JpaTransactionManager, in a beginning of methodA a new transaction was created, the call to save in methodA participates of active transaction already created, the call to save in methodB also participates of active transaction already created even if methodB is private.
Upvotes: 0
Views: 59
Reputation: 671
Transactional annotation creates a "proxy" for the object. According to specification it does not create proxy if applied to private method. But if proxy created it has full access to class fields and methods, be they private or not. Transaction starts when the method entered and committed on method exit if there is no exception on the way. If exception occurs elsewhere transaction does rollback as a method will exit abnormally
Upvotes: 0