You can obtain the present transaction using Spring PlatformTransactionManager by calling the static method TransactionAspectSupport.currentTransactionStatus() which will return the present transaction status.
The following is an example code:
@Service
public class TransactionService {
@Autowired
private PlatformTransactionManager transactionManager;
public void method1() {
// get the present transaction status
TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();
// use the transaction status
boolean isNewTransaction = status.isNewTransaction();
// ...
}
@Transactional
public void method2() {
// get the present transaction status
TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();
// use the transaction status
boolean isNewTransaction = status.isNewTransaction();
// ...
}
}
In this example, we obtain the present transaction status for method1()
and method2()
by calling TransactionAspectSupport.currentTransactionStatus()
within the method. Then we can use the transaction status to check whether it is a new transaction or commit/roll back the transaction, etc.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-01-08 11:00:00 +0000
Seen: 25 times
Last updated: Oct 31 '22
What is a multi module project in Spring-boot?
Can you rephrase the query about generating custom sequences using Spring Boot JPA?
The absolute view ID `/view` in Spring MVC Thymeleaf shows inconsistency, can you rephrase it?
What is the way to execute code once Spring Boot has started?
What is the process of redirecting to another host using Spring Cloud Gateway?
What could be the reason for having duplicate products in my requisition when using spring boot?