1.声明式事务

ACID 四大特性
A 原子性:原子性指的是 在一组业务操作下 要么都成功 要么都失败
在一组增删改查的业务下 要么都提交 要么都回滚
C 一致性:事务前后的数据要保证数据的一致性
在一组的查询业务下 必须要保证前后关联数据的一致性
I 隔离性:在并发情况下 事物之间要相互隔离。
D 持久性:数据一旦保存就是持久性的。

总结:在事务控制方面,主要有两个分类:
编程式事务:在代码中直接加入处理事务的逻辑,可能需要在代码中显式调用beginTransaction()、commit()、rollback()等事务管理相关的方法
connetion.autoCommit(false);
———
——
—-
connction.commint()
catch(){
connction.rollback();
}
声明式事务:在方法的外部添加注解或者直接在配置文件中定义,将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。spring的AOP恰好可以完成此功能:事务管理代码的固定模式作为一种横切关注点,通过AOP方法模块化,进而实现声明式事务。

2.声明式事务的简单配置

Spring从不同的事务管理API中抽象出了一整套事务管理机制,让事务管理代码从特定的事务技术中独立出来。开发人员通过配置的方式进行事务管理,而不必了解其底层是如何实现的。
Spring的核心事务管理抽象是PlatformTransactionManager。它为事务管理封装了一组独立于技术的方法。无论使用Spring的哪种事务管理策略(编程式或声明式),事务管理器都是必须的。
事务管理器可以以普通的bean的形式声明在Spring IOC容器中。下图是spring提供的事务管理器
1、在配置文件中添加事务管理器

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. https://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. https://www.springframework.org/schema/tx/spring-tx.xsd
  15. ">
  16. <context:component-scan base-package="cn.xixi"></context:component-scan>
  17. <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
  18. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  19. <property name="username" value="${jdbc.username}"></property>
  20. <property name="password" value="${jdbc.password}"></property>
  21. <property name="url" value="${jdbc.url}"></property>
  22. <property name="driverClassName" value="${jdbc.driverClassName}"></property>
  23. </bean>
  24. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  25. <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
  26. </bean>
  27. <!--事务控制-->
  28. <!--配置事务管理器的bean-->
  29. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  30. <property name="dataSource" ref="dataSource"></property>
  31. </bean>
  32. <!--开启基于注解的事务控制模式,依赖tx名称空间-->
  33. <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
  34. </beans>

@Transactional注解应该写在哪:
@Transactional 可以标记在类上面(当前类所有的方法都运用上了事务)
@Transactional 标记在方法则只是当前方法运用事务
也可以类和方法上面同时都存在, 如果类和方法都存在@Transactional会以方法的为准。如果方法上面没有@Transactional会以类上面的为准
建议:@Transactional写在方法上面,控制粒度更细, 建议@Transactional写在业务逻辑层上,因为只有业务逻辑层才会有嵌套调用的情况。

3.事务配置的属性

isolation:设置事务的隔离级别
propagation:事务的传播行为
noRollbackFor:那些异常事务可以不回滚
noRollbackForClassName:填写的参数是全类名
rollbackFor:哪些异常事务需要回滚
rollbackForClassName:填写的参数是全类名
readOnly:设置事务是否为只读事务
timeout:事务超出指定执行时长后自动终止并回滚,单位是秒

1、设置隔离级别(isolation)

用来解决并发事务所产生一些问题:
并发: 同一个时间,多个线程同时进行请求。
什么时候会生成并发问题:在并发情况下,对同一个数据(变量、对象)进行读写操作才会产生并发问题
并发会产生什么问题?
1.脏读
2.不可重复度
3.幻影读
概念: 通过设置隔离级别可解决在并发过程中产生的那些问题:image.png

image.png

image.png

image.png

image.png

2、事务的传播特性

事务的传播特性指的是当一个事务方法被另一个事务方法调用时,这个事务方法应该如何进行?
image.png
image.png
image.png

3、基于xml的事务配置

jdbcTemplate.xml

<?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:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      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/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop
      https://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx
      https://www.springframework.org/schema/tx/spring-tx.xsd
">
   <context:component-scan base-package="cn.xixi"></context:component-scan>
   <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
       <property name="username" value="${jdbc.username}"></property>
       <property name="password" value="${jdbc.password}"></property>
       <property name="url" value="${jdbc.url}"></property>
       <property name="driverClassName" value="${jdbc.driverClassName}"></property>
   </bean>
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
   </bean>
   <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
       <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
   </bean>
   <!--事务控制-->
   <!--配置事务管理器的bean-->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>
   </bean>
   <!--
   基于xml配置的事务:依赖tx名称空间和aop名称空间
       1、spring中提供事务管理器(切面),配置这个事务管理器
       2、配置出事务方法
       3、告诉spring哪些方法是事务方法(事务切面按照我们的切入点表达式去切入事务方法)
   -->
   <bean id="bookService" class="cn.xixi.service.BookService"></bean>
   <aop:config>
       <aop:pointcut id="txPoint" expression="execution(* cn.xixi.service.*.*(..))"/>
       <!--事务建议:advice-ref:指向事务管理器的配置-->
       <aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"></aop:advisor>
   </aop:config>
   <tx:advice id="myAdvice" transaction-manager="transactionManager">
       <!--事务属性-->
       <tx:attributes>
           <!--指明哪些方法是事务方法-->
           <tx:method name="*"/>
           <tx:method name="checkout" propagation="REQUIRED"/>
           <tx:method name="get*" read-only="true"></tx:method>
       </tx:attributes>
   </tx:advice>
</beans>