Spring中的事务管理

来源:互联网 发布:知乎离线下载 编辑:程序博客网 时间:2024/05/17 20:40

事务:本质上是一个加锁的概念,用来确保一组操作的时候       数据的完整性和一致性

一个完整的事物需要满足以下的特性
 原子性(atomicity):事务的原子性确保动作要么全部完成,要么完全不起作用
 一致性(consistency):一旦所有事务动作完成,事务就被提交。也就是说要么都成功,要么都不成功
 隔离性(isolation):多个事物同时处理一个数据的时候,每个事物都应该与其他事务隔离开来,防止数据损坏
 持久性(durability):一旦事务完成,无论发生什么系统错误,它的结果都不应该受到影响。应该被持久化到数据库中




这就是事务的四个关键属性(ACID)

Spring既支持编程式事务管理,也支持声明式的事务管理


1:编程式事务管理:将事务管理代码嵌入到业务方法中来控制事务的提交和回滚,在编程式事务中,必须在每个业务操作中包含额外的事务管理代码


2:声明式事务管理:大多数情况下比编程式事务管理更好用。它将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。事务管理作为一种横切关注点,可以通过AOP方法模块化。Spring通过Spring AOP框架支持声明式事务管理。

配置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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop 
 http://www.springframework.org/schema/aop/spring-aop-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.1.xsd">


<!-- 启动@AspectJ框架的支持,如果你的项目中,
没有使用JAVA代码所写的切面,就不需要配置 -->
<aop:aspectj-autoproxy/>
<!-- 配置自动扫包规则 -->
<context:component-scan base-package="com.lovo.sm">

</context:component-scan>

<!-- 配置数据源 DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- &amp;在配置文件中代表& -->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis117?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="lovo"/>
</bean>


<!-- 配置Session工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>


<!-- 将Mapper配置文件与Session进行关联 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lovo.sm.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
</bean>


<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>


<!-- 事务管理方式一,适合于单数据库,使用注解的方式来管理事务 
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
-->

<!-- 事务管理方式二,采用springAOP来声明式的处理事务,
声明式就是脱离你的JAVA代码,通常是声明在配置中 -->
<!-- 声明一个通知 -->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="true"></tx:method>
<tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="search*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>

<!-- 这里是配置AOP事务关联 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut id="serviceMethod" expression="execution(* com.lovo.sm.service.impl.*ServiceImpl.*(..))"></aop:pointcut>
<!-- 将我们定义的通知,交给AOP来关联 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"></aop:advisor>
</aop:config>

</beans>
然后在需要加入事务管理的业务中连接spring的事务管理AOP即可

0 0