Berk Teke
Berk Teke

Reputation: 31

@Transactional method calling another method with @Transactional anotation?

I have two different methods in the same class. I get SonarLint error that
"Methods with Spring proxy should not be called via "this".

These two methods are marked with the @Transaction annotation. Even though I made the annotations '@Transactional(propagation = Propagation.REQUIRES_NEW)', but it didn't solve the SonarLint error. I cannot move methods to another class. What should I do?

@Transactional
    public void processEmails(SearchFilter searchFilter, ExchangeService exchangeService) throws Exception
    {
        Folder inbox = Folder.bind(exchangeService, WellKnownFolderName.Inbox);
....
....
...

 @Transactional
    public void readAndProcessProposalApprovalMails() throws Exception
    {
        try (ExchangeService exchangeService = new MicrosoftExchangeDomainServiceImpl(exchangeUsername, encryptionService.decryptWithGCMAlgorithm(exchangePassword), exchangeServiceUrl, exchangeServiceTimeout, exchangeDomain))
        {
            SearchFilter searchFilter = microsoftExchangeService.getSearchFilter(false, null, approvalSubjectPrefix, null);
            processEmails(searchFilter, exchangeService);
        }
    }

Upvotes: 1

Views: 66

Answers (1)

baha gam
baha gam

Reputation: 90

To address the SonarLint error you can use one of these two solutions:

  1. Refactor into Separate Classes: (Recommended)

Spring uses proxies to manage transactional boundaries. and when calling the method within the same class using "this' the transactional annotation is ignored because it bypasses the proxy.

  1. Using AopContext: (Not Recommended)

AopContext.currentProxy(): this mehtod will give you the current Spring AOP proxy for the bean, which you can use to call the methods with transactional annotations.

Upvotes: 0

Related Questions