声明式事务
回顾事务transaction
- 事务在项目开发中十分重要,涉及到数据的一致性
- 事务ACID原则
- 原子性
- 一致性
- 隔离性
- 多个业务可能操作同一个资源,防止数据损坏
- 持久性
- 事务一旦提交,无论系统发生什么问题,结果都不会被影响
这里举了一个例子,进行insert操作和delete操作,insert操作成功,delete操作失败,根据ACID原则,这一组业务应该一起成功或者一起失败
在官方文档中介绍了两种事务的实现类型
http://mybatis.org/spring/zh/transactions.html#configuration
- 交由容器管理事务
- 编程式事务管理
- 根据AOP的特性,最好以横切关注点的方式将切片切入,而不是修改已经写好的业务代码,所以编程式事务管理了解下就行,少用
交由容器管理事务
-
创建一个
DataSourceTransactionManager
对象的,传入DateSource-
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dataSource" /> </bean>
-
-
添加spring的事务命名空间
-
<tx:jta-transaction-manager />
-
-
找一个容器配置事务,平常不这么丢进去
-
<property name="transactionFactory"> <bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" /> </property>
-
但是举例是结合AOP进行的事务管理
-
添加事务的命名空间
-
可以手打,会自动添加依赖支持
-
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
-
<tx:jta-transaction-manager/>
-
检查下支持是否进来了
-
然后给入数据源创建DataSourceTransactionManager对象,这句是写死的
-
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dateSource" /> </bean>
-
<!--结合AOP实现事务织入--> <!--配置事务的传播特性,这个属性在晚上搜,事务的传播特性--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add" propagation="REQUIRED"/><!--这个属性默认的--> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="query" read-only="true"/><!--只读--> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="txPointCut" expression="execution(* com.haoyun.mapper.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config>
-
<select id="selectUser" resultType="user"> select * from user </select> <insert id="insertUser" parameterType="user"> insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd}) </insert> <delete id="deleteUser" parameterType="int"> deletes from user where id = #{id} </delete>
-
这里的delete是故意写错的,测试会不会insert会不会回滚
-
public interface UserMapper { List<User> selectUser(); int insertUser( User user); int deleteUser( int id); }
-
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{ /*省去了注入的过程*/ public List<User> selectUser() { User user = new User(); user.setId(14); user.setName("haoyun"); user.setPwd("123"); UserMapper mapper = getSqlSession().getMapper(UserMapper.class); mapper.insertUser(user); mapper.deleteUser(5); return getSqlSession().getMapper(UserMapper.class).selectUser(); } public int insertUser(User user) { return getSqlSession().getMapper(UserMapper.class).insertUser(user); } public int deleteUser(int id) { return getSqlSession().getMapper(UserMapper.class).deleteUser(id); } }
-
selectUser中放了insert和delete操作,delete操作失败后,会回滚,不会插入进去