Lets say i have the following method:
@Transactional
public void transactionCreatingMethod(){
myJpaRepository.findAll();
}
Next i enable transaction logging by adding the following to my application.properties
logging.level.org.springframework.orm.jpa.JpaTransactionManager=TRACE
logging.level.org.springframework.transaction.interceptor = TRACE
When i execute the method i would get the following statements.
[TRACE] 2021-11-09 18:05:31.312 [main] TransactionInterceptor - Getting transaction for [config.StartupChecks$$EnhancerBySpringCGLIB$$192e2f3.transactionCreatingMethod]
[DEBUG] 2021-11-09 18:05:32.394 [main] JpaTransactionManager - Participating in existing transaction
[TRACE] 2021-11-09 18:05:32.394 [main] TransactionInterceptor - Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll]
The first line states that transactionCreatingMethod
is getting a transaction opened for it.
The second line states that the jpaRepository inside will participates in the existing transaction, so far so good.
But then in the third line it says that it opens a new transaction for findAll
!
This is very confusing and when i first saw these statements i thought i had configured something wrong. But after doing some research it turns out that the transaction for findAll
is actually a logical transaction
while the one created for transactionCreatingMethod
is actually a physical transaction
. This is also explained here.
However in the logs both types of transactions are simply referred to as transaction
. Could these logs be changed so that transactions that participate in an existing transaction be referred to as logical transaction
and the first transaction that was opened is referred to as physical
or database
transaction? That would make the logs much less confusing.
Comment From: jhoeller
We differentiate between "new" transactions (which we "create") and "existing" transactions (which we "participate in") in the log statements. There's one line missing in the log quote above as far as I can tell: A "Creating new transaction" line from JpaTransactionManager
on top. In combination with the "Participating in existing transaction" line, this should become clearer.
"Getting transaction" refers to what the AOP method interceptor does for a particular method invocation, determining the transaction to apply. Point taken, this wording might not be very obvious, but it's definitely wrong to interpret "getting" as "opening new". Also, part of the problem here is that you're reading transaction manager and interceptor logging side by side.
In practice, I wouldn't enable trace logging for the interceptor unless you got an AOP issue; there is a reason that this is trace logging. It should be entirely sufficient to enable debug logging for the transaction manager in use, providing a complete picture of resource-related transaction management steps.