当前位置: 代码迷 >> VC/MFC >> SpringMVC搭建中事务无法回滚的有关问题
  详细解决方案

SpringMVC搭建中事务无法回滚的有关问题

热度:170   发布时间:2016-05-02 03:37:44.0
SpringMVC搭建中事务无法回滚的问题

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:context="http://www.springframework.org/schema/context"	xmlns:aop="http://www.springframework.org/schema/aop"	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/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           http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">           	<context:annotation-config />	<context:component-scan base-package="com.myland.*" >	</context:component-scan>	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">		<property name="locations">			<value>classpath:jdbc.properties</value>		</property>	</bean>		<bean id="dataSource1"	class="org.apache.commons.dbcp.BasicDataSource"	destroy-method="close">						<property name="driverClassName"  value="${mysql.driverClassName}"></property>   		<property name="url" value="${mysql.url}"></property>    		<property name="username" value="${mysql.username}"></property>   		<property name="password" value="${mysql.password}"></property>   		<property name="initialSize" value="${mysql.initialSize}"></property>   		<property name="maxActive" value="${mysql.maxActive}"></property>     		<property name="maxIdle" value="${mysql.maxIdle}"></property>  		<property name="timeBetweenEvictionRunsMillis" value="${mysql.timeBetweenEvictionRunsMillis}"></property>  		<property name="minEvictableIdleTimeMillis" value="${mysql.minEvictableIdleTimeMillis}"></property>  	</bean>		<!-- 阿里连接池 -->	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  init-method="init" destroy-method="close">   		<!-- 驱动名称   -->        <property name="DriverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="${mysql.url}" />         <!-- 数据库用户名称   -->        <property name="username" value="root" />          <!-- 数据库密码 -->          <property name="password" value="root" />          <!-- 连接池最大使用连接数量 -->          <property name="maxActive" value="20" />          <!-- 初始化大小 -->          <property name="initialSize" value="5" />          <!-- 获取连接最大等待时间 -->          <property name="maxWait" value="60000" />          <!-- 连接池最小空闲 -->          <property name="minIdle" value="2" />          <!-- 逐出连接的检测时间间隔 -->          <property name="timeBetweenEvictionRunsMillis" value="3000" />          <!-- 最小逐出时间  -->         <property name="minEvictableIdleTimeMillis" value="300000" />          <!-- 测试有效用的SQL Query -->          <property name="validationQuery" value="SELECT 'x'" />          <!-- 连接空闲时测试是否有效 -->          <property name="testWhileIdle" value="true" />          <!-- 获取连接时测试是否有效 -->          <property name="testOnBorrow" value="false" />          <!-- 归还连接时是否测试有效  -->         <property name="testOnReturn" value="false" />        <!-- 统计监控信息 -->        <property name="filters" value="stat,wall,log4j" />   	</bean>   	    <!-- define the SqlSessionFactory, notice that configLocation is not needed when you use MapperFactoryBean -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />                   <property name="configLocation" value="classpath:mybatis-config.xml"/>             <property name="mapperLocations">			<list>				<value>classpath*:com/myland/myapp/**/*Mapper.xml</value>			</list>		</property>                                   </bean>        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" >         <constructor-arg ref="sqlSessionFactory" />    </bean>        	<!-- 事务处理  begin -->	<bean id="txManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>        <tx:advice id="txAdvice" transaction-manager="txManager">        <tx:attributes>            <tx:method name="select*" read-only="true" propagation="REQUIRED" />            <tx:method name="find*" read-only="true" propagation="REQUIRED" />            <tx:method name="save*" propagation="REQUIRED"  rollback-for="Exception"/>            <tx:method name="update*" propagation="REQUIRED"  rollback-for="Exception"/>            <tx:method name="add*" propagation="REQUIRED"  rollback-for="Exception"/>            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />        </tx:attributes>    </tx:advice>        <aop:config>        <aop:pointcut id="baseServiceMethods" expression="execution(* com.myland.demo.service.*.*(..))" />        <aop:advisor advice-ref="txAdvice" pointcut-ref="baseServiceMethods" />    </aop:config>		<aop:aspectj-autoproxy proxy-target-class="true"/>     	<!-- 事务处理  end -->		<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">    </bean>    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">        <property name="patterns">            <list>                <value>com.myland..Service.*</value>            </list>        </property>    </bean>    <aop:config>        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />    </aop:config>		</beans>

?springmvc-servlet.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"    xmlns:aop="http://www.springframework.org/schema/aop"     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/mvc         http://www.springframework.org/schema/mvc/spring-mvc-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/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  default-autowire="byName">    <context:component-scan base-package="com.myland.*" >    	    </context:component-scan>    <mvc:annotation-driven />    <!-- 必须添加否则拦截器不管作用 -->    <aop:aspectj-autoproxy proxy-target-class="true" />   	<bean id="OperLogInterceptor" class="com.myland.framework.log.OperLogInterceptor"></bean>        <!-- 视图解析器 -->    <bean id="viewResolver"        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass"            value="org.springframework.web.servlet.view.JstlView"></property>        <property name="prefix" value="/WEB-INF/views/"></property>        <property name="suffix" value=".jsp"></property>    </bean></beans>

?以上是工程中原有spring配置文件。在测试事务时,事务不能回滚。原因是applicationContext.xml是主容器在扫描类中service,发现service进行事务处理。在主容器初始化后,springmvc-servlet在进行一次扫描,[email protected][email protected][email protected]是作为普通bean使用。

确认事务无法回滚的几个问题。

1.确认mysql表类型innodb类型

2.确认配置文件配置是否正确,包括:service中路径正确

3.在配置注解扫描时,[email protected][email protected]

主容器

<context:component-scan base-package="com.myland.*" >
??? ??? <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
??? </context:component-scan>

mvc容器

<context:component-scan base-package="com.myland.*" >
??? ??? <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
??? ??? <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
??? </context:component-scan>

?

?

  相关解决方案