Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring@Transactional不创建事务_Spring_Spring Mvc_Transactional - Fatal编程技术网

Spring@Transactional不创建事务

Spring@Transactional不创建事务,spring,spring-mvc,transactional,Spring,Spring Mvc,Transactional,我正在用弹簧把我的头撞到墙上。我已经搜索了好几天试图让@Transactional开始工作。任何帮助都将不胜感激 我使用Spring3.2作为访问MySQL数据库的web应用程序(我使用SpringMVC、JDBC和安全性)。mysql表正在使用InnoDB。我正在努力让声明性事务正常工作(我尝试使用编程事务,并且成功地工作了)。我有控制器调用带有@Transactional注释的服务方法。控制器在servlet xml文件中配置,服务/dao在单独的应用程序xml文件中配置。我似乎找不到@Tr

我正在用弹簧把我的头撞到墙上。我已经搜索了好几天试图让@Transactional开始工作。任何帮助都将不胜感激

我使用Spring3.2作为访问MySQL数据库的web应用程序(我使用SpringMVC、JDBC和安全性)。mysql表正在使用InnoDB。我正在努力让声明性事务正常工作(我尝试使用编程事务,并且成功地工作了)。我有控制器调用带有@Transactional注释的服务方法。控制器在servlet xml文件中配置,服务/dao在单独的应用程序xml文件中配置。我似乎找不到@Transactional的正确配置,无法拾取并创建事务。没有错误-只是没有事务处理。以下是我的设置的详细信息:

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

  • 服务方法使用@Transactional和注入调用的DAO类进行注释

  • 服务类用@Named注释,应用程序配置为使用组件扫描在spring-app-context.xml中获取这些服务类

  • 事务处理配置在spring-app-context.xml中设置(不在servlet配置文件中)

  • 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>
    

    你说的“没有交易”是什么意思?你如何诊断代码不是事务性的?你是否会因为不在事务中而得到任何异常??嘿,JB Nizet-我可以通过日志和数据在数据库中的显示方式来诊断它。使用编程事务时,日志会非常清楚地显示DataSourceTransactionManager日志条目-@Transactional不会。我还向代码中添加了一个有意的异常来测试它,并且数据库中的条目没有回滚。shazintlc-不幸的是,没有,我没有收到任何异常。您的类路径中是否有
    spring aspects
    <?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>";
    }
    
    <?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
        }
    }