- 编程式事务:由程序员编程事务控制代码
- 声明式事务:事务控制代码已经由 spring 写好.程序员只需要声明出哪些方 法需要进行事务控制和如何进行事务控制
- 声明式事务都是针对于 ServiceImpl 类下方法的.
- 配置哪些方法需要spring进行管理,一般是对mapper接口中的方法进行管理
- 基于advice,底层会基于方法生成advice类,在这个类中事务进行之前启动事务,事务进行之后提交或者回滚这样的操作(代替了mybatis中filter openSeesionInview的功能)
<?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.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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--代替mybatis中的mybatis.xml的功能 -->
<!--数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--相当于mabatis中的工厂,spring帮助进行创建工厂
typeAliasesPackage标签相当于mybatis的typeAliases标签,类名可以简写-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="cn.wit.pojo"></property>
</bean>
<!--相当于mappers标签 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.wit.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<!-- <property name="sqlSessionFactory" ref="factory"></property> -->
</bean>
<!--spring进行事务管理的类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"></property>
</bean>
<!--配置声明式事务,本质上是切点的通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="ins*"/>
<tx:method name="del*"/>
<tx:method name="upd*"/>
<tx:method name="*" readonly="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.wit.serviceImpl.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>
name=" "
- 哪些方法需要有事务控制,支持*通配符
readonly=”boolean”
- 是否是只读事务,如果为 true,告诉数据库此事务为只读事务.数据化优化,会对 性能有一定提升,所以只要是查询的方法,建议使用此数据.如果为 false(默认值),事务需要提交的事务.建议新增,删除,修 改
propagation 控制事务传播行为
- 当一个具有事务控制的方法被另一个有事务控制的方法调用 后,需要如何管理事务(新建事务?在事务中执行?把事务挂起?报异 常?)
- REQUIRED (默认值): 如果当前有事务,就在事务中执行,如果当 前没有事务,新建一个事务.
- SUPPORTS:如果当前有事务就在事务中执行,如果当前没有事 务,就在非事务状态下执行
- MANDATORY:必须在事务内部执行,如果当前有事务,就在事务 中执行,如果没有事务,报错.
- REQUIRES_NEW:必须在事务中执行,如果当前没有事务,新建事 务,如果当前有事务,把当前事务挂起.
- NOT_SUPPORTED:必须在非事务下执行,如果当前没有事务,正 常执行,如果当前有事务,把当前事务挂起
- NEVER:必须在非事务状态下执行,如果当前没有事务,正常执行, 如果当前有事务,报错
- NESTED:必须在事务状态下执行.如果没有事务,新建事务,如果当前有事务,创建一个嵌套事务
- isolation=”” 事务隔离级别,在多线程或并发访问下如何保证访问到的数据具有完整性的.
事务隔离级别isolation
- DEFAULT: 默认值,由底层数据库自动判断应该使用什么隔离级别
- READ_UNCOMMITTED: 可以读取未提交数据,可能出现脏读,不 重复读,幻读
- READ_COMMITTED:只能读取已提交数据,不会出现脏读
- REPEATABLE_READ: 读取的数据被添加锁,防止其他事务修改此数据,可以防止不可重复读.脏读,可能出现幻读
- SERIALIZABLE: 排队操作,对整个表添加锁.一个事务在操作数 据时,另一个事务等待事务操作完成后才能操作这个表
脏读
事务A读取到了事务B还未提交的数据(READ_COMMITTED禁止读缓存就可以解决)
不可重复读
事务A读取数据后,该数据被事务B更改(修改操作,REPEATABLE_READ锁住对应数据就可以解决)
幻读
事务A查询数据,事务B新增或删除(幻读的消除只锁一条数据解决不了,要将整个表给锁住SERIALIZABLE)
事务回滚rollback-for
- 当出现什么异常时需要进行回滚
- rollback-for=”异常类型全限定路径”
- 建议:给定该属性值
- 手动抛异常一定要给该属性值.
- no-rollback-for=””
- 当出现什么异常时不滚回事务.