Spring @Transactional没有创建事务

时间:2013-03-23 15:36:15

标签: spring spring-mvc transactional

我正在用Spring交易严厉打击我的头。我已经搜索了几天试图让@Transactional工作。任何帮助都会非常感激!

我使用Spring 3.2访问一个访问MySQL数据库的Web应用程序(我正在使用Spring MVC,JDBC和Security)。 mysql表正在使用InnoDB。我正在努力让声明式事务工作(我尝试使用程序化事务并且成功运行)。我有控制器调用使用@Transactional注释的服务方法。控制器在servlet xml文件中配置,services / dao在单独的应用程序xml文件中配置。我似乎找不到正确的配置@Transactional被选中并创建交易。没有错误 - 只是没有交易。以下是我的设置细节:

  1. 使用DispatcherServlet的Web应用程序,使用带注释的控制器配置servlet XML文件。控制器已注入服务接口。

  2. 服务方法使用@Transactional注释并调用注入的DAO类。

  3. 服务类使用@Named注释,并且应用程序配置为使用组件扫描在spring-app-context.xml中选择它们。

  4. 事务配置在spring-app-context.xml中设置(不在servlet配置文件中)。

  5. 的web.xml:

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        /WEB-INF/config/spring-app-context.xml
        /WEB-INF/config/spring-security-context.xml
      </param-value>
    </context-param>
    
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
      <servlet-name>appServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/spring-servlet-context.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    
    
    <servlet-mapping>
      <servlet-name>appServlet</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    弹簧servlet的context.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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
        <!-- Scans within the base package of the application for @Components to configure as beans -->
        <!-- @Controller, @Service, @Configuration, etc. -->
        <context:component-scan base-package="ewarrants.web" />
    
        <!-- Enables the Spring MVC @Controller programming model -->
        <mvc:annotation-driven />
    
        <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <context:property-placeholder location="/WEB-INF/config/ewarrants.properties"/>
    
    </beans>
    

    以下是控制器设置示例:

    @Inject
    IUserService userService;
    
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processRegistration(@Valid @ModelAttribute UserRegistrationVM incomingVM, BindingResult result, Model model) throws Exception {       
    
        ...
    
        userService.registerNewUser(newUser);
    
        return "redirect:<location>";
    }
    

    这是spring-app-context:

    <?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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    
    
        <!-- Scans within the base package of the application for @Components to configure as beans -->
        <!-- @Controller, @Service, @Configuration, etc. -->
        <context:component-scan base-package="ewarrants.core" />
        <tx:annotation-driven transaction-manager="txManager" />
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="<url>"/>
            <property name="username" value="<user>"/>
            <property name="password" value="<password>"/> 
        </bean>
    
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <context:property-placeholder location="/WEB-INF/config/ewarrants.properties"/>
    </beans>
    

    以下是一个示例服务实现:

    @Named
    public class UserService extends Service implements IUserService {
    
        @Inject
        private IUserDao userDao;
    
        @Override
        @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
        public void registerNewUser(User user) throws Exception {
            ...
    
            // add the user record
            int newUserID = userDao.insertUser(user);
    
            // carry out more database inserts
        }
    }
    

0 个答案:

没有答案