常见网站开发的语言四年级下册数学优化设计答案

当前位置: 首页 > news >正文

常见网站开发的语言,四年级下册数学优化设计答案,网站建设工资多少钱,举报网站赚钱Spring中的事务控制 说明#xff1a; JavaEE体系进行分层开发#xff0c;事务处理位于业务层#xff0c;Spring提供了分层设计业务层的事务处理解决方案。 Spring框架为我们提供了一组事务控制的接口。具体在后面的小节介绍。这组接口是在spring-tx.RELEASE.jar中。 spri…Spring中的事务控制 说明 JavaEE体系进行分层开发事务处理位于业务层Spring提供了分层设计业务层的事务处理解决方案。 Spring框架为我们提供了一组事务控制的接口。具体在后面的小节介绍。这组接口是在spring-tx.RELEASE.jar中。 spring的事务控制都是基于AOP的它既可以使用编程的方式实现也可以使用配置的方式实现。我们学习的重点是使用配置的方式实现。
PlatformTransactionManager 此接口是spring的事务管理器它里面提供了我们常用的操作事务的方法源代码如下 public interface PlatformTransactionManager { //开启事务 TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; //提交事务void commit(TransactionStatus status) throws TransactionException; //回滚事务void rollback(TransactionStatus status) throws TransactionException;
} 真正管理事务的对象 Spring为不同的orm框架提供了不同的PlatformTransactionManager接口实现类 DataSourceTransactionManager使用Spring JDBC或iBatis 进行持久化数据时使用HibernateTransactionManager使用Hibernate版本进行持久化数据时使用
TransactionDefinition TransactionDefinition接口包含与事务属性相关的方法源代码如下 public interface TransactionDefinition {int PROPAGATION_REQUIRED 0;int PROPAGATION_SUPPORTS 1;int PROPAGATION_MANDATORY 2;int PROPAGATION_REQUIRES_NEW 3;int PROPAGATION_NOT_SUPPORTED 4;int PROPAGATION_NEVER 5;int PROPAGATION_NESTED 6;int ISOLATION_DEFAULT -1;int ISOLATION_READ_UNCOMMITTED 1;int ISOLATION_READ_COMMITTED 2;int ISOLATION_REPEATABLE_READ 4;int ISOLATION_SERIALIZABLE 8;int TIMEOUT_DEFAULT -1;//传播行为int getPropagationBehavior();//隔离级别int getIsolationLevel();int getTimeout();boolean isReadOnly(); }TransactionDefinition 接口定义的事务规则包括事务隔离级别、事务传播行为、事务超时、事务的只读、回滚规则属性同时Spring 还为我们提供了一个默认的实现类DefaultTransactionDefinition该类适用于大多数情况。如果该类不能满足需求可以通过实现 TransactionDefinition 接口来实现自己的事务定义。 1.事务隔离级别 事务并发时的安全问题 问题描述隔离级别脏读一个事务读取到另一个事务还未提交的数据read-commited不可重复读一个事务内多次读取一行数据的内容其结果不一致repeatable-read幻读一个事务内多次读取一张表中的内容其结果不一致serialized-read Spring事务隔离级别比数据库事务隔离级别多一个default由低到高为 隔离级别ISOLATION_DEFAULT这是一个platfromtransactionmanager默认的隔离级别使用数据库默认的事务隔离级别。ISOLATION_READ_UNCOMMITTED这是事务最低的隔离级别会产生脏读不可重复读和幻像读。ISOLATION_READ_COMMITTED这种事务隔离级别可以避免脏读出现但是可能会出现不可重复读和幻像读。 Oracle数据库默认的隔离级别。ISOLATION_REPEATABLE_READ这种事务隔离级别可以防止脏读、不可重复读但是可能出现幻像读。MySQL数据库默认的隔离级别。ISOLATION_SERIALIZABLE这是花费最高代价但是最可靠的事务隔离级别事务被处理为顺序执行。除了防止脏读、不可重复读外还避免了幻像读。 2.事务的传播行为 什么是事务传播行为 事务传播行为propagation behavior指的就是当一个事务方法被另一个事务方法调用时这个事务方法应该如何进行。 例如methodA事务方法调用methodB事务方法时methodB是继续在调用者methodA的事务中运行呢还是为自己开启一个新事务运行这就是由methodB的事务传播行为决定的。 Spring定义了七种传播行为 事务传播行为类型说明PROPAGATION_REQUIRED如果当前没有事务就新建一个事务如果已经存在一个事务中加入到这个事务中。这是最常见的选择。PROPAGATION_SUPPORTS支持当前事务如果当前没有事务就以非事务方式执行。PROPAGATION_MANDATORY使用当前的事务如果当前没有事务就抛出异常。PROPAGATION_REQUIRES_NEW新建事务如果当前存在事务把当前事务挂起。PROPAGATION_NOT_SUPPORTED以非事务方式执行操作如果当前存在事务就把当前事务挂起。PROPAGATION_NEVER以非事务方式执行如果当前存在事务则抛出异常。PROPAGATION_NESTED如果当前存在事务则在嵌套事务内执行。如果当前没有事务则执行与REQUIRED类似的操作。 3.事务超时 timeout事务超时时间 当前事务所需操作的数据被其他事务占用则等待。 100自定义等待时间100秒。-1由数据库指定等待时间默认值。建议
4.读写性 readonly 读写性 true只读可提高查询效率适合查询 false可读可写适合增删改
5.回滚规则 TransactionAttribute TransactionAttribute 的默认实现类是DefaultTransactionAttribute 它同时继承了DefaultTransactionDefinition。在DefaultTransactionDefinition 的基础上增加了rollbackOn的实现DefaultTransactionAttribute的实现指定了当异常类型为unchecked exception 的情况下将回滚事务。 rollbackOn 回滚规则可省略或设置 rollbackOn“Exception” 如果事务中抛出 RuntimeException,则自动回滚 如果事务中抛出 CheckException不会自动回滚
TransactionStatus PlatformTransactionManager.getTransaction(…) 方法返回一个 TransactionStatus 对象该对象代表一个新的或已经存在的事务源代码如下 public interface TransactionStatus{boolean isNewTransaction();void setRollbackOnly();boolean isRollbackOnly(); }配置事务applicationContext.xml !–配置事物管理器–bean classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource//bean!–配置事物属性–bean classorg.springframework.transaction.support.DefaultTransactionDefinitionproperty namepropagationBehaviorName valuePROPAGATION_REQUIRED/property namereadOnly valuefalse/property/beanSpring AOP控制事务 1.导入schema约束 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:txhttp://www.springframework.org/schema/tx xmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd!–配置事物属性bean classorg.springframework.transaction.support.DefaultTransactionDefinitionproperty namepropagationBehaviorName valuePROPAGATION_REQUIRED/property namereadOnly valuefalse/property/bean配置service代理对象bean idproxyService factory-beanbeanFactory factory-methodgetUserService /bean– /beans 2.配置增强 !– 1、增强 –tx:advice idtxAdvice transaction-managertransactionManager!–事务属性–tx:attributes!– 指定方法名称是业务核心方法read-only是否是只读事务。默认false不只读。isolation指定事务的隔离级别。默认值是使用数据库的默认隔离级别。propagation指定事务的传播行为。timeout指定超时时间。默认值为-1。永不超时。rollback-for用于指定一个异常当执行产生该异常时事务回滚。产生其他异常事务不回滚。省略时任何异常都回滚。–tx:method name* read-onlyfalse propagationREQUIRED/tx:method nameselect* read-onlytrue propagationSUPPORTS/tx:method nameget* read-onlytrue propagationSUPPORTS//tx:attributes/tx:advice3.配置切点 aop:config!–2、切点–aop:pointcut expressionexecution(* com.by.service..(..)) idpointcut//aop:config4.配置切面 aop:config!–3、切面–aop:advisor advice-reftxAdvice pointcut-refpointcut/aop:advisor/aop:config5.测试 RunWith(SpringJUnit4ClassRunner.class) ContextConfiguration(classpath:applicationContext.xml)//加载配置文件 public class ServiceTest {Autowiredprivate UserService userService;/*** 转账业务*/Testpublic void testUpdate(){userService.updateUser(张三丰,宋远桥,1F);} }