Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set springframework @Transactional with AspectJ

I want to use spring-aspects to make my methods transactional, but without using Spring AOP (Spring AOP works just fine with: <tx:annotation-driven/>). I'm using Maven to manage my project.

Is there a way to do compile time weaving on my project classes so "they are Transactional". I was trying to use the Mojo's AspectJ Maven Plugin, but without any good results.

Please help.

like image 770
Mariusz Zawadzki Avatar asked Feb 03 '10 14:02

Mariusz Zawadzki


People also ask

What are the default @transactional settings?

The default @Transactional settings are: The propagation setting is PROPAGATION_REQUIRED. The isolation level is ISOLATION_DEFAULT. The transaction is read/write.

Can we use @transactional in controller?

The controller can be made @Transactional , but indeed it's a common recommendation to only make the service layer transactional (the persistence layer should not be transactional either). The reason for this is not technical feasibility, but separation of concerns.

Can we use @transactional in service layer?

You should use @Transactional at service layer, if you want to change the domain model for client B where you have to provide the same data in a different model,you can change the domain model without impacting the DAO layer by providing a different service or by creating a interface and implementing the interface in ...

Can we use @transactional on interface?

You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies.


2 Answers

I figured it out. Maven plugin works fine but the problem was with my spring config: I had:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>

What I needed was:

<bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf">
    <property name="transactionManager" ref="transactionManager"/>
</bean>

Now it works fine. And performace of my @Transactional methods improved and that what I was aming for.

Here is my maven aspectj plugin config:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.3</version>
    <configuration>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
       </aspectLibraries>
        <source>1.5</source>
        <showWeaveInfo>true</showWeaveInfo>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

hope this helps someone.

like image 96
Mariusz Zawadzki Avatar answered Nov 13 '22 06:11

Mariusz Zawadzki


maybe you can try this:

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
like image 45
YJiao Avatar answered Nov 13 '22 05:11

YJiao