求帮忙看下springmvc框架中注解事务失效问题 5C

web.xml配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/applicationContext*.xml</param-value>
    </context-param>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>zhihuiyi</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/drf-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>InitServlet</servlet-name>
        <servlet-class>com.developerForDrf.frame.init.InitServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
   <servlet>
        <servlet-name>WeixinServlet</servlet-name>
        <servlet-class>com.developerForDrf.wechat.servlet.WeixinServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet> 


    <servlet-mapping>
        <servlet-name>zhihuiyi</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
       <servlet-name>InitServlet</servlet-name>
       <url-pattern>/servlet/InitServlet.do</url-pattern>
    </servlet-mapping>
     <servlet-mapping>
       <servlet-name>WeixinServlet</servlet-name>
       <url-pattern>/WeixinServlet.do</url-pattern>
    </servlet-mapping>

</web-app>

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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-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/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    "> 
    <context:component-scan base-package="com.developerForDrf,com.developerForDrf.frame,com.developerForDrf.wechat,com.developerForDrf.business" >
        <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
    </context:component-scan>   
    <context:property-placeholder location="classpath:config/db.properties" />
    <bean name="dataSource"
        class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
        destroy-method="close">
         <property name="driverClassName" value="${driverClassName}" /> 
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="1" />
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="5" />
        <!-- 连接池最大空闲 
        <property name="maxIdle" value="10" />-->
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="1" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="60000" />
        <property name="validationQuery" value="SELECT 1 FROM DUAL" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />

        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />
        <!-- 开启Druid的监控统计功能 
        <property name="filters" value="stat" />-->
    </bean>
    <!-- 国际化的消息资源文件 -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:config/validator</value>
            </list>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="60"/>
    </bean>   

    <!-- 处理事务 -->
    <!-- 生成一个事务管理对象 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property> 
    </bean> 

    <!-- 生成默认事务定义对象 -->
    <bean id="def" class="org.springframework.transaction.support.DefaultTransactionDefinition"></bean>

    <!-- 声明使用注解式事务  
    <tx:annotation-driven transaction-manager="transactionManager"/> --> 

    <bean id= "namedParameterJdbcDaoSupport" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport">
        <property name="dataSource" ref="dataSource"></property>
    </bean>   

</beans>

对应的servlet的配置文件

 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 ,多个可以用逗号隔开-->
    <context:component-scan base-package="com.developerForDrf,com.developerForDrf.frame,com.developerForDrf.wechat,com.developerForDrf.business" >
        <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  -->     
    </context:component-scan>
    <mvc:annotation-driven />

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:order="100"
        p:viewClass="org.springframework.web.servlet.view.JstlView"
        p:prefix="/" p:suffix=".jsp" />


    <!-- 处理Mutipart文件上传的bean -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>

    <!-- 基于注释的事务,当注释中发现@Transactional时,使用id为“transactionManager”的事务管理器  -->
    <!-- 如果没有设置transaction-manager的值,则spring以缺省默认的事务管理器来处理事务,默认事务管理器为第一个加载的事务管理器 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.developerForDrf.frame.interceptors.AuthInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>   

    <mvc:resources location="/resources/" mapping="resources/**" />

</beans>

事务是在service层中做的,代码段:

 @Transactional(readOnly=false,propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
    public String test(String openid,int account)throws Exception{
        String flag = "0";
        int f = 0;
        f = memberDao.updateUserAccount(openid, account);
        memberDao.updateSubscribe(openid, 1);
        //抛异常
        if(f==0){
            throw new Exception("测试异常");    
        }
        return flag;
    }

test()方法中的两个调用dao层的动作,第一个是正常的,第二个是有意弄错的,但是执行的时候并没有回滚。

请各位路过的大神帮忙卡看!!!

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问