网站建设报价单-中英文版,链接点开网页表白的网站怎么做的,广州 网站 建设,网站流量50g声明式事务控制
编程式事务控制相关对象
PlatformTransactionManager
PlatformTransactionManager接口是spring的事务管理器#xff0c;它里面提供了常用的操作事务的方法
方法说明TransactionStatus getTransaction(TransactionDefaultion defination)获取事务的状态信息…声明式事务控制
编程式事务控制相关对象
PlatformTransactionManager
PlatformTransactionManager接口是spring的事务管理器它里面提供了常用的操作事务的方法
方法说明TransactionStatus getTransaction(TransactionDefaultion defination)获取事务的状态信息void commit(TransactionStatus status)提交事务void rollback(TransactionStatus status)回滚事务 注意PlatformTransactionManager是接口类型不同的Dao层技术则有不同的实现类。如Dao层技术是jdbc或mybatis时org.springframework.jdbc.datasource.DataSourceTransactionManager 。Dao层技术是hibernate时org.springframework.orm.hibernate5.HibernateTransactionManager TransactionDefination
方法说明int getIsolationLevel()获取事务的隔离级别int getPropogationBehavior()获得事务的传播行为int getTimeout()获得超时时间boolean isReadOnly()是否只读
事务隔离级别
设置隔离级别可以解决事务并产生的问题如脏读、不可重复读和虚度
ISOLATION_DEFAULTISOLATION_READ_UNCOMMITTEDISOLATION_READ_COMMITTEDISOLATION_REPEATABLE_READISOLATION_SERIALIZABLE
事务传播行为
REQUIRED如果当前没有事务就新建一个事务如果已经存在一个事务加入到这个事务中。一般的选择默认值SUPPORTS支持当前事务如果没有当前事务就以非事务方式执行MANDATORY使用当前的事务如果没有当前事务就抛异常REQUERS_NEW新建事务如果当前在事务中把当前事务挂起NOT_SUPPORTED以非事务方式执行操作如果当前存在事务就把当前事务挂起NEVER以非事务方式运行如果当前存在事务抛出异常NESTED如果当前存在事务则在嵌套事务内执行。如果没有当前事务则执行REQUIRED类似的操作超时时间默认值是-1没有超时限制。如果有以秒为单位进行设置是否只读建议查询时设置为只读
TransactionStatus
TransactionStatus接口提供的是事务具体的运行状态方法介绍如下
方法说明boolean hasSavepoint()是否存储回滚点boolean isCompleted()事务是否完成boolean isNewTransaction()是否是新事务boolean isRollbackOnly()事务是否回滚
基于xml的声明式事务控制
什么是声明式事务控制
Spring的声明式事务就是采用声明的方式来处理事务。这里所说的声明就是指在配置文件中声明用在Spring配置文件中声明式的处理事务来代替代码式的处理事务
声明式事务处理的作用
事务管理不侵入开发的组件。具体来说业务逻辑对象就不会意识到正在事务管理之中事实上也应该如此因为事务管理是属于系统层面的服务而不是业务逻辑的一部分如果想要改变事务管理策划的话也只需要在定义文件中重新配置即可。在不需要事务管理的时候只要在设定文件上修改一下即可移去事务管理服务无需改变代码重新编译这样维护起来极其方便 注意Spring声明式事务控制底层就是AOP 声明式事务控制的实现
声明式事务控制明确事项
谁是切点谁是通知配置切面
代码实现
1、引入tx命名空间
beans xmlnshttp://www.springframework.org/schema/beans
xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance
xmlns:contexthttp://www.springframework.org/schema/context
xmlns:aophttp://www.springframework.org/schema/aop
xmlns:txhttp://www.springframework.org/schema/tx
xsi:schemaLocation
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd2、配置事务增强
!--平台事务管理器--
bean idtransactionManager
classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource /property
/bean
!--事务增强配置--
tx:advice idtxAdvice transaction-managertransactionManagertx:attributestx:method name*//tx:attributes
/tx:advice3、配置事务AOP织入
!--事务的aop增强--
aop:configaop:pointcut idmyPointcut expressionexecution(* com.dc.service.impl.*.*(..))/aop:advisor advice-reftxAdvice pointcut-refmyPointcut/aop:advisor
/aop:config4、测试事务控制转账业务代码
Override
public void transfer(String outMan, String inMan, double money) {accountDao.out(outMan,money);int i 1/0;accountDao.in(inMan,money);
}切点方法的事务参数的配置
!--事务增强配置--
tx:advice idtxAdvice transaction-managertransactionManagertx:attributestx:method name*//tx:attributes
/tx:advice其中tx:method代表切点方法的事务参数的配置例如
tx:method name“transfer” isolation“REPEATABLE_READ” propagation“REQUIRED” timeout“-1” read-only“false”/
name切点方法名称isolation事务的隔离级别propogation事务的传播行为timeout超时时间read-only是否只读
基于注解的声明式事务控制
1、编写AccountDao
Repository(accountDao)
public class AccountDaoImpl implements AccountDao {Autowiredprivate JdbcTemplate jdbcTemplate;public void out(String outMan, double money) {jdbcTemplate.update(update account set moneymoney-? where name?,money,outMan);
}public void in(String inMan, double money) {jdbcTemplate.update(update account set moneymoney? where name?,money,inMan);}
}2、编写AccountService
Service(accountService)
Transactional
public class AccountServiceImpl implements AccountService {Autowiredprivate AccountDao accountDao;Transactional(isolation Isolation.READ_COMMITTED,propagation
Propagation.REQUIRED)public void transfer(String outMan, String inMan, double money) {accountDao.out(outMan,money);int i 1/0;accountDao.in(inMan,money);}
}3、编写applicatioContext.xml配置文件
!--之前省略datsSource、jdbcTemplate、平台事务管理器的配置--
!--组件扫描--
context:component-scan base-packagecom.dc/
!--事务的注解驱动--
tx:annotation-driven/注解配置声明式事务控制解析
使用Transactional在需要进行事务控制的类或是方法上修饰注解可用的属性同xml配置方式例如隔离级别、传播行为等注解使用在类上那么该类下的所有方法都使用在同一套注解参数配置使用在方法上不同的方法可以采用不同的事务参数配置xml配置文件中要开启事务的注解驱动tx:annotation-driven/