SSH、IBatis等框架相关知识
Spring的事务
事务使用ACID特性来衡量事务的质量。这些特性包括原子性、一致性
隔离性和持久性。
a.原子性:事务必须是原子的。不可分割的,在事务结束的时候,事务中的所有任务必须全部成功完成或者全部任务失败,事务回滚到事务开始之前的状态
b.一致性:数据库中的所有数据必须和现实保持一致。
c.隔离性:事务与事务之间的屏障。每个事务必须与其他事务的执行结果隔离开,直到该事务执行完毕。
d.持久性:如果事务成功执行,无论系统发生什么情况,事务的持久性必须保证事务的执行结果是永存的。
在事务处理中有违反ACID特性的三个问题:脏读、不可重复读和幻读。
脏读:当一个事务读取了一个事务尚未提交的更新,就叫作脏读取。
不可重复读:在一个事务中执行多次同样的查询操作,但每次查询的结果都不相同,叫不可重复读
幻读:一个事务的更新结果影响到了另一个事务的问题。
Spring的事务管理器有5个,也即DataSourceTransactionManager(JDBC事务管理器),HibernateTransactionManager(Hibernate事务管理器),JdoTransactionManager(JDO事务管理器),JtaTransactionManager(Jta事务管理器)以及Apache的OJB事务管理器。
Spring支持声明式事务,并建议这样做。因为Spring中的事务是基于AOP实现的,而Spring的AOP是以方法为单位的,所以Spring的事务属性就是对事务应用到方法的策略描述,这些属性分别为:传播行为、隔离级别、只读和超时属性。
事务的传播行为是事务应用于方法的边界,它定义了事务的建立、暂停等行为。
事务的隔离级别:为了解决事务之间的3个缺陷,必须在事务之间建立隔离关系来保障事务的完整性。
事务的只读属性:在数据库的操作中,查询是使用最频繁的操作,每次执行查询操作时都要从数据库中重新读取数据,有时多次读取的数据都是相同的,这样的操作浪费了系统资源和影响了系统速度。如果将食物声明为只读,那么数据库可以根据事务的特性优化事务的读取操作。事务的只读属性需要配合事务的PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW和PROPAGATION_NESTED传播行为共同设置。
事务的超时属性:设置了事务的超时时间。
Spring的声明式事务不涉及组建依赖关系,它通过AOP实现事务管理。在使用Spring的声明式事务时不需要编写任何代码。使用事务代理工厂来管理事务。事务代理工厂TransactionProxyFactoryBean包含了事务拦截器、目标代理和事务的属性设置。
Spring使用注解式进行事务管理,配置示例
步骤一、在Spring配置文件中引入<tx:>命名空间
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
步骤二、具有@Transactional注解的bean自动配置为声明式事务
<!--事务管理--><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"></property></bean><!--配置事务增强器--><tx:adviceid="transactionAdvice"transaction-manager="transactionManager"><!--配置详细事务处理语义--><tx:attributes><tx:methodname="insert*"propagation="REQUIRED"/><tx:methodname="update*"propagation="REQUIRED"/><tx:methodname="delete*"propagation="REQUIRED"/><tx:methodname="save*"propagation="REQUIRED"/><tx:methodname="add*"propagation="REQUIRED"/><tx:methodname="saveOrUpdate*"propagation="REQUIRED"/><tx:methodname="get*"propagation="SUPPORTS"/><tx:methodname="find*"propagation="SUPPORTS"/><tx:methodname="load*"propagation="SUPPORTS"/><tx:methodname="select*"propagation="SUPPORTS"/><!--其他采用默认方式--><tx:methodname="*"/></tx:attributes></tx:advice><!--SpringAOP事务管理--><aop:config><aop:pointcutexpression="execution(*com.jf.service..*impl.*(..))"id="transactionPointCut"/><!--指定txAdvice切入点应用txAcvice食物增强处理--><aop:advisoradvice-ref="transactionAdvice"pointcut-ref="transactionPointCut"/></aop:config>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。