Mybatis + SpringMVC事务管理

SpringMVC支持声明式和编程式事务管理,这里我讲的是声明式事务,也即通过注解@Transactional来使用事务。

这里是我在这个工程中所使用的jar包:http://download.csdn.net/detail/liujan511536/9050079

这里是我这个工程的源码:http://download.csdn.net/detail/liujan511536/9050093

这是一个dynamic web project。

首先看配置文件web.xml,这个文件在WebContent/WEB-INF/目录下:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   <display-name>SpringMVC_Mybatis</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.     <welcome-file>default.html</welcome-file>  
  9.     <welcome-file>default.htm</welcome-file>  
  10.     <welcome-file>default.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12.     
  13.   <listener>  
  14.     <listener-class>  
  15.         org.springframework.web.context.ContextLoaderListener  
  16.     </listener-class>  
  17.   </listener>  
  18.     
  19.   <!-- 读取mybatis配置文件 -->  
  20.   <context-param>  
  21.     <param-name>contextConfigLocation</param-name>  
  22.     <param-value>classpath:mybatis-context.xml</param-value>  
  23.   </context-param>  
  24.   
  25.   <servlet>  
  26.     <servlet-name>springmvc</servlet-name>  
  27.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  28.     <init-param>  
  29.         <!-- 加载springmvc的xml到spring的上下文容器中 -->  
  30.         <param-name>contextConfigLocation</param-name>  
  31.         <param-value>  
  32.             <!-- /WEB-INF/classes/application-context.xml -->  
  33.             classpath:application-context.xml  
  34.         </param-value>  
  35.     </init-param>  
  36.     <load-on-startup>1</load-on-startup>  
  37.   </servlet>  
  38.     
  39.   <servlet-mapping>  
  40.     <servlet-name>springmvc</servlet-name>  
  41.     <url-pattern>*.html</url-pattern>  
  42.   </servlet-mapping>  
  43. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC_Mybatis</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <listener>
  	<listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  
  <!-- 读取mybatis配置文件 -->
  <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:mybatis-context.xml</param-value>
  </context-param>

  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<!-- 加载springmvc的xml到spring的上下文容器中 -->
  		<param-name>contextConfigLocation</param-name>
  		<param-value>
  			<!-- /WEB-INF/classes/application-context.xml -->
  			classpath:application-context.xml
  		</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

mybatix-context.xml ( /WebContent/WEB-INF/classes/目录下):
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"   
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xmlns:aop="http://www.springframework.org/schema/aop"  
  9.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  10.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  11.         http://www.springframework.org/schema/context  
  12.         http://www.springframework.org/schema/context/spring-context.xsd    
  13.         http://www.springframework.org/schema/mvc  
  14.         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  15.         http://www.springframework.org/schema/tx  
  16.         http://www.springframework.org/schema/tx/spring-tx.xsd  
  17.         http://www.springframework.org/schema/aop  
  18.         http://www.springframework.org/schema/aop/spring-aop.xsd">  
  19.      
  20.     <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  21.         <property name="locations">  
  22.             <list>  
  23.                 <value>classpath:db-config.properties</value>  
  24.             </list>  
  25.         </property>  
  26.     </bean>  
  27.     <!-- 配置数据库dataSource -->  
  28.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  29.         <property name="driverClassName" value="${jdbc.driverClass}" />  
  30.         <property name="username" value="${jdbc.username}" />  
  31.         <property name="url" value="${jdbc.url}" />  
  32.     </bean>  
  33.       
  34.     <bean id="transactionManager"  
  35.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  36.         <property name="dataSource" ref="dataSource" />  
  37.     </bean>  
  38.     <tx:annotation-driven transaction-manager="transactionManager" />  
  39.       
  40.       
  41.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  42.         <property name="dataSource" ref="dataSource" />  
  43.         <property name="typeAliasesPackage" value="com.liujan.entity" />  
  44.     </bean>  
  45.       
  46.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  47.         <property name="basePackage" value="com.liujan.mapper" />  
  48.     </bean>  
  49.       
  50. </beans>  
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
   
    <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 		<property name="locations">
 			<list>
 				<value>classpath:db-config.properties</value>
 			</list>
 		</property>
 	</bean>
	<!-- 配置数据库dataSource -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}" />
		<property name="username" value="${jdbc.username}" />
		<property name="url" value="${jdbc.url}" />
	</bean>
	
	<bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
	
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesPackage" value="com.liujan.entity" />
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.liujan.mapper" />
	</bean>
	
</beans>

application-context.xml (/WebContent/WEB-INF/classes/目录下):

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xmlns:aop="http://www.springframework.org/schema/aop"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.         http://www.springframework.org/schema/context  
  11.         http://www.springframework.org/schema/context/spring-context.xsd    
  12.         http://www.springframework.org/schema/mvc  
  13.         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  14.         http://www.springframework.org/schema/aop  
  15.         http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  16.           
  17.     <context:component-scan base-package="com.liujan">  
  18.     </context:component-scan>  
  19.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  20.         p:prefix="/WEB-INF/page/" p:suffix=".jsp">  
  21.     </bean>  
  22. </beans>  
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
        
	<context:component-scan base-package="com.liujan">
	</context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/page/" p:suffix=".jsp">
	</bean>
</beans>


 容器装配顺序造成的事务无法回滚
在加载这个web.xml这个文件时,Spring会去加载在这个文件中引入的其他文件,这里有mybatis-context.xml和application-context.xml这两个文件,两个文件都在/WebContent/WEB-INF/classes/目录下。
但是这里有个问题,就是这两个文件的加载是有先后顺序的。Spring会优先加载在context-param标签中引入的文件,也就是这里的mybatis-context.xml,然后再加载application-context.xml。在加载mybatis-context.xml时,Spring就会装配@Controlle和@Service的容器,并把他们放到Spring的容器中来;接着加载application-context.xml,这时Spring也会把@Service和@Controller注解的实例装配到Spring容器中,但是这时由于先前在加载mybatis-context.xml时已经装配过@Service和@Controller的容器,所以这时新装配的容器会覆盖掉先前的容器,所以Spring容器中就只剩下后来装配的容器了。
这种装配顺序就会引来一个问题,由于我的事务是在mybatis-context.xml文件中声明的,所以这个文件中的Service容器是带有事务能力的;但是这里装配的Service容器会被后来application-context.xml中的Service容器替换掉,而application-context.xml中的Service容器是没有事务能力的,所以最后造成在Spring容器中的Service是没有事务能力的。
这是很容易就陷入的一个陷阱;很多初学者在照着网上的教程配置好后,却发现无论如何都不能回滚或者其他事务的问题,很多时候就是由于这种情况造成的。
要解决这个问题,只需只在mybatis-context.xml中生成Service的带事务的容器,而在application-context.xml中就不生成Service容器了。又由于Service是Controller类中的一个属性,所以在装配Controller前要先装配好Service。
为了达到以上目的,由于是先加载myabtis-context.xml,后加载application-context.xml,所以我们只需在myabtis-context.xml装配Service,不装配Controller;然后在application-context.xml中装配Controller,不装配Service就可以了。这样就得修改mybatis-context.xml和application-context.xml这两个文件,修改后的文件 如下:
mybatis-context.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"   
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xmlns:aop="http://www.springframework.org/schema/aop"  
  9.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  10.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  11.         http://www.springframework.org/schema/context  
  12.         http://www.springframework.org/schema/context/spring-context.xsd    
  13.         http://www.springframework.org/schema/mvc  
  14.         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  15.         http://www.springframework.org/schema/tx  
  16.         http://www.springframework.org/schema/tx/spring-tx.xsd  
  17.         http://www.springframework.org/schema/aop  
  18.         http://www.springframework.org/schema/aop/spring-aop.xsd">  
  19.     <!-- 不装配Controller -->  
  20.     <context:component-scan base-package="com.liujan">  
  21.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  22.     </context:component-scan>  
  23.       
  24.     <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  25.         <property name="locations">  
  26.             <list>  
  27.                 <value>classpath:db-config.properties</value>  
  28.             </list>  
  29.         </property>  
  30.     </bean>  
  31.     <!-- 配置数据库dataSource -->  
  32.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  33.         <property name="driverClassName" value="${jdbc.driverClass}" />  
  34.         <property name="username" value="${jdbc.username}" />  
  35.         <property name="url" value="${jdbc.url}" />  
  36.     </bean>  
  37.       
  38.     <bean id="transactionManager"  
  39.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  40.         <property name="dataSource" ref="dataSource" />  
  41.     </bean>  
  42.     <tx:annotation-driven transaction-manager="transactionManager" />  
  43.       
  44.       
  45.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  46.         <property name="dataSource" ref="dataSource" />  
  47.         <property name="typeAliasesPackage" value="com.liujan.entity" />  
  48.     </bean>  
  49.       
  50.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  51.         <property name="basePackage" value="com.liujan.mapper" />  
  52.     </bean>  
  53.       
  54. </beans>  
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 不装配Controller -->
    <context:component-scan base-package="com.liujan">
    	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 		<property name="locations">
 			<list>
 				<value>classpath:db-config.properties</value>
 			</list>
 		</property>
 	</bean>
	<!-- 配置数据库dataSource -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}" />
		<property name="username" value="${jdbc.username}" />
		<property name="url" value="${jdbc.url}" />
	</bean>
	
	<bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
	
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesPackage" value="com.liujan.entity" />
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.liujan.mapper" />
	</bean>
	
</beans>

application-context.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xmlns:aop="http://www.springframework.org/schema/aop"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.         http://www.springframework.org/schema/context  
  11.         http://www.springframework.org/schema/context/spring-context.xsd    
  12.         http://www.springframework.org/schema/mvc  
  13.         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  14.         http://www.springframework.org/schema/aop  
  15.         http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  16.     <!-- 装配Controller,不装配Service -->  
  17.     <context:component-scan base-package="com.liujan">  
  18.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  19.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  
  20.     </context:component-scan>  
  21.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  22.         p:prefix="/WEB-INF/page/" p:suffix=".jsp">  
  23.     </bean>  
  24. </beans>  
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    <!-- 装配Controller,不装配Service -->
	<context:component-scan base-package="com.liujan">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/page/" p:suffix=".jsp">
	</bean>
</beans>

web.xml内容不变。

2. 我们把事务控制在Service层,当一个函数(假设函数名为funA)前加上 @Transactional标签时,就表示这个函数具有事务能力。那么什么情况下事务会commit,什么情况下事务会rollback?
如果要事务回滚,则funA函数内必须得抛出一个uncheck异常(比如RuntimeException),而且这个异常不能在funA内被try...catch,比如,在UserService类里有一个函数叫saveUser,是用来往数据库中插入一条数据的:
  1. @Transactional(propagation=Propagation.REQUIRED)  
  2. public void saveUser(User u) throws Exception{  
  3.     userMapper.insert(u);  
  4.       
  5.     throw new RuntimeException("hehe");  
  6. }  
@Transactional(propagation=Propagation.REQUIRED)
public void saveUser(User u) throws Exception{
	userMapper.insert(u);
	
	throw new RuntimeException("hehe");
}
当运行这个函数时,是先往数据库中插入数据u,然后再抛出RuntimeException异常,而且这个异常没有被saveUser函数捕捉,所以这个异常会被Spring检测到,这时事务就会回滚,数据插入失败;
但是,如果这个异常被saveUser捕捉了,那么Spring就不会检测到这个异常,事务就不会回滚,数据就会插入成功,如下:
  1. @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)  
  2. public void saveUser(User u) throws Exception{  
  3.     userMapper.insert(u);  
  4.     try {  
  5.         throw new RuntimeException("hehe");  
  6.     } catch (Exception e) {  
  7.         // TODO: handle exception  
  8.     }  
  9.           
  10. }  
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void saveUser(User u) throws Exception{
	userMapper.insert(u);
	try {
		throw new RuntimeException("hehe");
	} catch (Exception e) {
		// TODO: handle exception
	}
		
}

但是,这个异常如果被调用saveUser的函数catch的话,事务也会回滚的,比如在UserController中有一个方法 addUser,在这个方法里会调用userService的saveUser方法:
  1. public void addUser(String name, int stuId) {  
  2.     User u = new User();  
  3.     u.setName(name);  
  4.     u.setStuid(stuId);  
  5.     try {  
  6.         userService.saveUser(u);  
  7.     } catch (Exception e) {  
  8.         // TODO: handle exception  
  9.         e.printStackTrace();  
  10.     }  
  11.           
  12. }  
public void addUser(String name, int stuId) {
	User u = new User();
	u.setName(name);
	u.setStuid(stuId);
	try {
		userService.saveUser(u);
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
		
}
这时事务也会回滚。
综上所述,只要异常不在异常抛出的地方被catch,那么即使该异常在后面的函数中被catch,事务也会回滚。

这里又有一个问题,就是抛出的异常只能是uncheck类的,如果是check类的事务就不会回滚,如下:
  1. @Transactional(propagation=Propagation.REQUIRED)  
  2. public void saveUser(User u) throws Exception{  
  3.     userMapper.insert(u);  
  4.       
  5.     throw new Exception("hehe");  
  6.           
  7. }  
@Transactional(propagation=Propagation.REQUIRED)
public void saveUser(User u) throws Exception{
	userMapper.insert(u);
	
	throw new Exception("hehe");
		
}
这时事务是不会回滚的。
如果想在跑出check类异常时事务也会回滚,则可以在 @Transactional中声明当遇上check类异常时回滚,如下:
  1. @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)  
  2. public void saveUser(User u) throws Exception{  
  3.     userMapper.insert(u);  
  4.           
  5.     throw new Exception("hehe");  
  6.           
  7. }  
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void saveUser(User u) throws Exception{
	userMapper.insert(u);
		
	throw new Exception("hehe");
		
}


最后讲下
@Transactional标签中 propagation的含义:

REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 

SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。 

MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。 

REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。 

NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 

NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。 

NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。







5
0
 
 
查看评论
1楼 qq4769291622015-08-29 15:56发表 [回复] [引用][举报]
弱弱的问下 为啥要2个xml ,直接用默认的 mvc-dispatcher-servlet.xml 配置 所有bean 不行么?
Re: sysu安仔2015-09-01 15:58发表 [回复] [引用][举报]
回复qq476929162:也可以,这样只是为了将不同模块的配置分开了,以后方便管理。
发表评论
  • 用 户 名:
  • xlnwrnmdbb
  •   
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
快速回复 TOP
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值