0

I know this has been asked before, thats why I've spent the past 2 days trying to get @Transactional to work with no luck before asking for help.

In my applicationContext.xml:

  • I create a JpaTransactionManager
  • In my <tx:annotation-driven... I explicitly set the reference to my transaction manager
  • I define my service class (with transactional support) as a bean.

When I (from my MVC layer) call the services annotated method no transaction it created. I've tried adding the <tx:annotation-driven... in my dispatcherServlet.xml as well with no luck.

BTW: I know the aspectj stuff works because its working for the @Cachable annotated methods.

ClientService.java

public class ClientService{
  ...
    @Transactional
    public Client editClient(Integer clientId, Client newClientState){
        logger.debug("Transaction active:::: {}", TransactionSynchronizationManager.isActualTransactionActive());
        Client dbClient = this.repository.getClient(clientId);
        //Copy new state into dbClient
        this.repository.save(dbClient);
        return dbClient;
    }
}

pom.xml

<properties>
    <hibernate.version>3.6.0.Final</hibernate.version>
    <hibernate.validator.version>4.1.0.Final</hibernate.validator.version>
    <spring.version>3.2.4.RELEASE</spring.version>
</properties>
<dependencies>
    <dependency><groupId>org.hibernate</groupId>                <artifactId>hibernate-core</artifactId>             <version>${hibernate.version}</version></dependency>
    <dependency><groupId>org.hibernate</groupId>                <artifactId>hibernate-entitymanager</artifactId>    <version>${hibernate.version}</version></dependency>
    <dependency><groupId>org.hibernate</groupId>                <artifactId>hibernate-validator</artifactId>        <version>${hibernate.validator.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-aop</artifactId>                 <version>${spring.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-aspects</artifactId>             <version>${spring.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-core</artifactId>                <version>${spring.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-context</artifactId>             <version>${spring.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-context-support</artifactId>     <version>${spring.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-orm</artifactId>                 <version>${spring.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-tx</artifactId>                  <version>${spring.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-web</artifactId>                 <version>${spring.version}</version></dependency>
    <dependency><groupId>org.springframework</groupId>          <artifactId>spring-webmvc</artifactId>              <version>${spring.version}</version></dependency>
    ...
</dependencies>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    ....
    <cache:annotation-driven cache-manager="cacheManager" mode="aspectj"/>
    <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
    ....
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="dataSource" ref="mainDataSource" />
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>
    <!-- Service -->
    <bean class="...ClientService" />
    ....
</beans>
NeilA
  • 1,450
  • 2
  • 12
  • 20
  • I only use my component scan on my MVC layer not my service layer. I'm autowiring the service into my mvc controller. Do you think that the service is autowired incorrectly? – NeilA Nov 19 '13 at 09:58
  • 1
    Check http://stackoverflow.com/a/10021874/2628911 – Sandhu Santhakumar Nov 19 '13 at 10:01
  • Just because is causing Cacheable annotations to be detected and used, does not imply that will detect and use your Transactional annotations. For one thing there are two types of Cacheable and the one above is for Spring method caches. – Steve Nov 19 '13 at 10:08
  • I only mentioned the @Cachable to indicate that the aspectj works in my project. As mensioned, I do the in the same context as the – NeilA Nov 19 '13 at 15:36

0 Answers0