Spring @Transactional没有在事务中执行

时间:2013-11-19 09:27:16

标签: java spring transactions annotations

我知道之前已经问过这个问题,这就是为什么我在过去的两天里试图让@Transactional在请求帮助之前没有运气。

在我的applicationContext.xml中:

  • 我创建了一个 JpaTransactionManager
  • 在我的< tx:annotation-driven ... 中,我明确设置了对我的事务管理器的引用
  • 我将我的服务类(具有事务支持)定义为bean。

当我(来自我的MVC层)调用服务注释方法时,它没有创建任何事务。 我尝试在 dispatcherServlet.xml 中添加< tx:annotation-driven ... ,但没有运气。

:我知道aspectj的东西是有效的,因为它适用于@Cachable注释方法。

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>

0 个答案:

没有答案