Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Transactional in super classes not weaved when using load time weaving

The project I am working on has a similar structure for the DAOs to the one bellow:

/** 
* Base DAO class
*/
@Transactional    
public class JPABase {

  @PersistenceContext
  private EntityManager entityManager;

  public void persist(Object entity) {
    entityManager.persist(entity);
  }
 //some more methods in here
}

and

/** 
* Generic DAO class implementation
*/
@Transactional 
public abstract class GenericDao extends JpaBase {
   //some methods in here
}

and

/** 
* Specialized DAO class
*/
@Repository
@Transactional
public class PersonDao extends GenericDao {
  //some methods in here
}

Until now, the project used compile time weaving, but the configuration has changed to use <context:load-time-weaver/> with -javaagent:/opt/tomcat7-1/lib/spring-instrument.jar.

Since this change has been applied, the JpaBase's and GenericDao's @Transactional annotations are not weaved anymore. Each time a service class calls the persist method on a PersonDao object, no transaction is started.

Noteworthy:

  • this used to work in the past, when using compile time weaving.
  • all the methods that are defined in the PersonDao are weaved correctly, but the ones inherited (e.g. persist(Object entity)) are NOT weaved.

Compile time weaving and load time weaving are supposed to do the same thing, just at different moments in time. Why has the weaving behaviour changed?

like image 483
Anca N Avatar asked Jul 01 '15 14:07

Anca N


1 Answers

Tomcat default classlLoader is WebappClassLoader, but you need `TomcatInstrumentableClassLoader.

There are two solutions:

  1. Modify WebappLoader.class

Change WebappLoader.java

private String loaderClass =  "org.apache.catalina.loader.WebappClassLoader";

Replace:

private String loaderClass = "org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader";  

Compile it and replaced the class file(catalina.jar), then it works.

Here is required dependency jars: catalina.jar,tomcat-coyote.jar,tomcat-util.jar(/bin),tomcat-juli.jar

  1. Modify context.xml:


<?xml version="1.0" encoding="UTF-8"?>  
<Context>  
    <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>  
</Context>

like image 112
Denny.Zhang Avatar answered Oct 23 '22 02:10

Denny.Zhang