spring框架(四)——Spring中的事务控制



注意:spring的jdbc模块笔者这里不做详解, 因为实际中用的不多,(不过还真有用的,笔者这里什么时候用什么时候在整理),那么这个模块的事务,要做一下整理。

编程式事务、这里不讲,就是将事务的开启关闭写在代码里。不做重点。

1 spring的声明式事务控制(重点)

编程式事务管理将数据层提交事务的代码加入到逻辑层,与Spring无侵入式编程的主思想有冲突,实际开发过程中,往往采用声明式事务管理形式
通过编程式事务管理的代码不难看出,在业务逻辑层对应的业务上添加某些代码即可完成整体事务管理的操作,使用SpringAOP的思想,将公共的代码加入后,即可完成对应的工作,这就是声明式事务管理的核心机制。

1.1 基于XML的声明式事务配置

a、导入spring事务支持的jar包spring-tx-3.2.0.RELEASE.jar

b、引入spring事务的名称空间


c、配置spring的事务支持

  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:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.                            http://www.springframework.org/schema/beans/spring-beans.xsd   
  8.                            http://www.springframework.org/schema/tx   
  9.                            http://www.springframework.org/schema/tx/spring-tx.xsd  
  10.                            http://www.springframework.org/schema/aop   
  11.                            http://www.springframework.org/schema/aop/spring-aop.xsd">  
  12.     <!-- 把Dao交给Spring管理 -->  
  13.     <bean id="accountDao" class="springTX.dao.impl.AccountDaoImpl">  
  14.         <!-- 为dao的父类JdbcDaoSupport注入一个数据源 -->  
  15.         <property name="dataSource" ref="driverManagerDataSource"></property>  
  16.     </bean>  
  17.       
  18.     <!-- 把业务层也交给Spring -->  
  19.     <bean id="accountService" class="springTX.service.impl.AccountServiceImpl">  
  20.         <property name="accountDao" ref="accountDao"></property>  
  21.     </bean>  
  22.   
  23.     <!-- 事务控制:声明式事务,重点。基于XML的。  
  24.          前期准备:  
  25.             1.导入aop的jar包  
  26.             2.确保spring-tx-xxxx.jar在你的工作空间中  
  27.          步骤:  
  28.             1.配置一个事务管理器,并为其注入数据源  
  29.             2.配置事务的通知.id是为其指定一个唯一标识。transaction-manager指定事务通知使用的管理器  
  30.             3.配置AOP,指定切面使用的通知。<aop:advisor advice-ref="" pointcut="切入点表达式"/>它就是指定已经配置好的通知类型。  
  31.     -->  
  32.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  33.         <property name="dataSource" ref="driverManagerDataSource"></property>  
  34.     </bean>  
  35.     <tx:advice id="txAdivce" transaction-manager="transactionManager">  
  36.         <!-- 配置事务的相关属性 -->  
  37.         <tx:attributes>  
  38.             <!-- tx:method 是用于指定方法名称和事务属性的关系。  
  39.                  name:指定方法的名称。可以使用通配符*,也可以部分通配  
  40.                  isolation:事务的隔离级别。默认值是:DEFAULT。当是default时是看数据库的隔离级别。   
  41.                  rollback-for:该属性用于配置一个异常,当产生该异常时回滚。  
  42.                  no-rollback-for:该属性用于配置一个异常,当产生该异常时不回滚。  
  43.                  propagation:指定事务的传播行为。 默认值是:REQUIRED  
  44.                  read-only:指定当前事务是否是只读事务。默认值是false,不是只读的。  
  45.                  timeout:指定超时时间。默认值是-1。以秒为单位  
  46.              -->  
  47.             <tx:method name="*" />  
  48.             <tx:method name="find*" read-only="true"/>  
  49.         </tx:attributes>  
  50.     </tx:advice>  
  51.     <aop:config>  
  52.         <aop:advisor advice-ref="txAdivce" pointcut="execution(* springTX..*.*(..))"/>  
  53.     </aop:config>  
  54.     <!-- 配置Spring的内置数据源 -->  
  55.     <bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  56.         <!-- 给数据源注入参数 -->  
  57.         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  58.         <property name="url" value="jdbc:mysql://localhost:3306/spring_tx"></property>  
  59.         <property name="username" value="root"></property>  
  60.         <property name="password" value="1234"></property>  
  61.     </bean>  
  62. </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: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.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">
	<!-- 把Dao交给Spring管理 -->
    <bean id="accountDao" class="springTX.dao.impl.AccountDaoImpl">
    	<!-- 为dao的父类JdbcDaoSupport注入一个数据源 -->
    	<property name="dataSource" ref="driverManagerDataSource"></property>
    </bean>
    
    <!-- 把业务层也交给Spring -->
    <bean id="accountService" class="springTX.service.impl.AccountServiceImpl">
    	<property name="accountDao" ref="accountDao"></property>
    </bean>

	<!-- 事务控制:声明式事务,重点。基于XML的。
		 前期准备:
		 	1.导入aop的jar包
		 	2.确保spring-tx-xxxx.jar在你的工作空间中
		 步骤:
		 	1.配置一个事务管理器,并为其注入数据源
		 	2.配置事务的通知.id是为其指定一个唯一标识。transaction-manager指定事务通知使用的管理器
		 	3.配置AOP,指定切面使用的通知。<aop:advisor advice-ref="" pointcut="切入点表达式"/>它就是指定已经配置好的通知类型。
	-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	<tx:advice id="txAdivce" transaction-manager="transactionManager">
		<!-- 配置事务的相关属性 -->
		<tx:attributes>
			<!-- tx:method 是用于指定方法名称和事务属性的关系。
				 name:指定方法的名称。可以使用通配符*,也可以部分通配
				 isolation:事务的隔离级别。默认值是:DEFAULT。当是default时是看数据库的隔离级别。 
				 rollback-for:该属性用于配置一个异常,当产生该异常时回滚。
				 no-rollback-for:该属性用于配置一个异常,当产生该异常时不回滚。
				 propagation:指定事务的传播行为。 默认值是:REQUIRED
				 read-only:指定当前事务是否是只读事务。默认值是false,不是只读的。
				 timeout:指定超时时间。默认值是-1。以秒为单位
			 -->
			<tx:method name="*" />
			<tx:method name="find*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:advisor advice-ref="txAdivce" pointcut="execution(* springTX..*.*(..))"/>
	</aop:config>
	<!-- 配置Spring的内置数据源 -->
	<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 给数据源注入参数 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/spring_tx"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
</beans>
解释如下图:

OK、基于xml的声明式事务、配置完毕。

2 基于注解的声明式事务配置

a、把spring容器管理改为注解配置的方式(开启要扫描的包)

b、开启注解事务的支持


配置结果如下:

  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:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:context="http://www.springframework.org/schema/context"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.                            http://www.springframework.org/schema/beans/spring-beans.xsd   
  9.                            http://www.springframework.org/schema/tx   
  10.                            http://www.springframework.org/schema/tx/spring-tx.xsd  
  11.                            http://www.springframework.org/schema/aop   
  12.                            http://www.springframework.org/schema/aop/spring-aop.xsd   
  13.                            http://www.springframework.org/schema/context   
  14.                            http://www.springframework.org/schema/context/spring-context.xsd">  
  15.     <!-- 事务控制:声明式事务,重点。基于注解的。  
  16.          前期准备:  
  17.             1.导入context的名称空间  
  18.             2.确保spring-tx-xxxx.jar在你的工作空间中  
  19.          步骤:  
  20.             1.设置Spring要扫描的包  
  21.             2.开始Spring对注解式事务的支持。并且指定事务管理器类  
  22.             3.把Dao和service都交给Spring管理  
  23.             4.由于我们使用了注解,所以不能用JdbcDaoSupport,这个时候我们需要定义JdbcTemplate,为其注入数据源  
  24.     -->  
  25.     <context:component-scan base-package="springTX"></context:component-scan>  
  26.       
  27.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  28.       
  29.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  30.         <property name="dataSource" ref="driverManagerDataSource"></property>  
  31.     </bean>  
  32.     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  33.         <property name="dataSource" ref="driverManagerDataSource"></property>  
  34.     </bean>  
  35.       
  36.     <!-- 配置Spring的内置数据源 -->  
  37.     <bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  38.         <!-- 给数据源注入参数 -->  
  39.         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  40.         <property name="url" value="jdbc:mysql://localhost:3306/srping_tx"></property>  
  41.         <property name="username" value="root"></property>  
  42.         <property name="password" value="1234"></property>  
  43.     </bean>  
  44. </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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
      					   http://www.springframework.org/schema/beans/spring-beans.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 
      					   http://www.springframework.org/schema/context 
      					   http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 事务控制:声明式事务,重点。基于注解的。
		 前期准备:
		 	1.导入context的名称空间
		 	2.确保spring-tx-xxxx.jar在你的工作空间中
		 步骤:
		 	1.设置Spring要扫描的包
		 	2.开始Spring对注解式事务的支持。并且指定事务管理器类
		 	3.把Dao和service都交给Spring管理
		 	4.由于我们使用了注解,所以不能用JdbcDaoSupport,这个时候我们需要定义JdbcTemplate,为其注入数据源
	-->
    <context:component-scan base-package="springTX"></context:component-scan>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	
	<!-- 配置Spring的内置数据源 -->
	<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 给数据源注入参数 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/srping_tx"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
</beans>
c、在需要事务支持的地方加入@Transactional注解(一般在service层控制事务)
d、@Transactional注解配置的位置

  • 用在业务实现类上:该类所有的方法都在事务的控制范围

  • 用在业务接口上:该接口的所有实现类都起作用

  • 通过注解指定事务的定义信息

OK、基于注解的事务也整理完毕!

注意:对于事务上还有什么不了解的地方,可以观看笔者整理的spring==注解事务问题以及xml事务的配置、

url:http://blog.csdn.net/sinat_38259539/article/details/77678557

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值