Multiple markers on this line when using @Transactional

14,191

Solution 1

The issue with multiple markers is not a problem at all; it's purely informational. (The method is part of the implementation of an interface or abstract method, which you probably already knew, and it is intercepted by AOP because of the @Transactional annotation. I hope this doesn't surprise you…)

The error is because the class you are annotating doesn't implement a suitable interface (or interfaces), which would be necessary to use the built-in JDK proxy mechanism to put the AOP interceptors in place in the bean. (Bean-level interceptors are done through a proxy object that applies the transactional behavior and then delegates to the real object.) The JDK proxy mechanism only works with interfaces; intercepting anything else requires a different approach.

The two possible fixes for this are:

  1. Make the bean class implement a suitable interface that has all the methods of your class that are marked as @Transactional.
  2. Add cglib as a dependency, which Spring uses to dynamically write the classes that do the interception. (This is clever stuff.)

You only need to use one of these fixes, and the second one is very easy if you're using a build system like Maven; just update the dependencies. (Also, avoid doing calls to intercepted methods via this, whether explicit or not. That side-steps the AOP interception.)

Solution 2

I had the same red markers next to method signature with @Transactional annotation above.

@Override
@Transactional
public void updateBook(Book book) {
    bookDao.updateBook(book);
}

The message of such markers was as following

Multiple markers at this line - implements bookmanager.service.BookService.updateBook - advised by org.springframework.transaction.interceptor.TransactionInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)

My fix is that I replaced this annotation @org.springframework.transaction.annotation.Transactional with the following annotation @javax.transaction.Transactional.

I use Hibernate framework to manage transactions in my app.

Hope it will help you too.

Solution 3

Your transactions will work, provided everything else is correct. "Advised by" here is purely informational and shows that your method is advised by spring's TransactionInterceptor.invoke method.

About multiple marker problem, it shows up when you implement something, because there are two informations: first is "advised by", and second that this method is implementing something.

If you are unsure, I suggest that you write an integration test and test the changes in the database. But you do not have to do anything else, your code will work as it is. (If, as I stated before, everything else is correct.)

Share:
14,191

Related videos on Youtube

Aubergine
Author by

Aubergine

Spring; Spring Boot; Java; Kotlin; Couchbase; Reactor; WebFlux

Updated on June 04, 2022

Comments

  • Aubergine
    Aubergine almost 2 years

    for example:

    @Transactional 
    public boolean addPersonToDb(Person p) { // message on this line
     //some logic
    }
    

    Code compiles and runs with no problems.

    Message itself: Multiple markers at this line

    • implements

    com.pname1.pname2.pname3.pname4.PersonDAO.addPersonToDb

    • advised by

    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)

    I can't really understand if it is an error or just a message, looking at other threads people get that as an error. I am just worrying if my transactions work.

    Ok, the class implements interface and its method annotated as transactional , anything wrong with that?

    Update: solved some minor errors, web app works but I still get that message(not in stack trace, but on the line breakpoint):

    advised by org.springframework.transaction.interceptor.TransactionInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)

    Current situation:

            @Transactional
       public void registerNewUser(Person p) { // this gives message on line breakpoint - advised by ...; AND this method is implemented by interface
    
        pd.addPersonToDb(p);
    
    
    }
    
    @Transactional
    public void blabla(Person p){ // this does not, as expected; AND it is not in interface
    
    }
    

    Do my transactions work or not? (I have no exceptions and web application runs and the methods work)

    I can't understand if this message error or not?

  • Aubergine
    Aubergine over 12 years
    I use maven, I added cglib, but now whenever I try to annotate interface implemented methods in implementation class I get this advised by, HOWEVER when I use transactional on other methods (not specified in interface that is implemented) everything works fine. Why I can't annotate methods implemented by interface?
  • Donal Fellows
    Donal Fellows over 12 years
    I have no idea what's wrong, but I know for sure that when I do that sort of thing it works just fine for me. I include CGLIB in my dependencies and let Spring work it all out…
  • Aubergine
    Aubergine over 12 years
    I must admit your description is difficult for me, so this message can be or cannot be? Now I don't have any exception only this message on line breakpoint. Please see updated answer :-)
  • ruhsuzbaykus
    ruhsuzbaykus over 12 years
    there is no need to have @Transactional at the implemented methods. this code will work as it is, provided everything else is correct.
  • Aubergine
    Aubergine over 12 years
    Thanks, please don't downvote previous answer, it is correct I had an error before which was solved and this is my plain stupid confusion. When something pop-ups for first time like these advice markers, it gets me crazy ( control freak) :-)
  • GreenTurtle
    GreenTurtle over 11 years
    Neither of both solutions works for me. Any other ideas how to get rid of this "Around Advice Markers" warnings?