aop事务
一、spring封装事务管理代码
1、事务操作:打开事务、提交事务、回滚事务
2、事务操作对象
因为在不同平台,操作事务的代码各不相同。spring提供了一个接口: PlatformTransactionManager 接口。
2.1 PlatformTransactionManager 接口:
DataSourceTransactionManager
HibernateTransitionmanager
注意:在spring中玩事务管理,最为核心的对象就是TransactionManager对象
2.2 spring管理事务的属性介绍
事务的隔离级别:1 读未提交、2 读已提交、4 可重复读、8 串行化
是否只读:true 只读、false 可操作
事务的传播行为:
二、spring管理事务方式
通知(事务管理) 和 目标对象(业务逻辑)
配置成 --> 代理对象(事务管理、业务逻辑)
1、编码式(了解即可)
1.1 将核心事务管理器配置到spring容器
<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" ></property> </bean>
1.2 配置TransactionTemplate模板
<!-- 事务模板对象 --> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" > <property name="transactionManager" ref="transactionManager" ></property> </bean>
连接池和Dao:
<!-- 1.将连接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property> <property name="driverClass" value="${jdbc.driverClass}" ></property> <property name="user" value="${jdbc.user}" ></property> <property name="password" value="${jdbc.password}" ></property> </bean> <!-- 2.Dao--> <bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl" > <property name="dataSource" ref="dataSource" ></property> </bean>
1.3 将事务模板注入Service
<!-- 3.Service--> <bean name="accountService" class="cn.shop.service.AccountServiceImpl" > <property name="ad" ref="accountDao" ></property> <property name="tt" ref="transactionTemplate" ></property> </bean>
1.4 在Service中调用模板
2、xml配置(aop)--重点
2.1 导包
aop、aspect aop联盟、weaving织入包
2.2 导入新的约束(tx)
context:读取properties配置
aop:配置aop
tx:配置事务通知
2.3 配置通知
<!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager" > <tx:attributes> <!-- 以方法为单位,指定方法应用什么事务属性 isolation:隔离级别 propagation:传播行为 read-only:是否只读 --> <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> </tx:attributes> </tx:advice>
2.4 配置将通知织入目标
<!-- 配置织入 --> <aop:config > <!-- 配置切点表达式 --> <aop:pointcut expression="execution(* cn.shop.service.*ServiceImpl.*(..))" id="txPc"/> <!-- 配置切面 : 通知+切点 advice-ref:通知的名称 pointcut-ref:切点的名称 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" /> </aop:config>
3、注解配置(aop)--重点
3.1 导包
aop、aspect aop联盟、weaving织入包
3.2 导入新的约束(tx)
context:读取properties配置
aop:配置aop
tx:配置事务通知
3.3 开启注解管理事务
<!-- 开启使用注解管理aop事务 --> <tx:annotation-driven/>
3.4 使用注解
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true) public class AccountServiceImpl implements AccountService {
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false) public void transfer(final Integer from,final Integer to,final Double money) {