Spring事物管理--相关要点及配置事物管理器

事务的四大特征

1.原子性:一个事务中所有对数据库的操作是一个不可分割的操作序列,要么全做要么全不做

2.一致性:数据不会因为事务的执行而遭到破坏

3.隔离性:一个事物的执行,不受其他事务的干扰,即并发执行的事物之间互不干扰

4.持久性:一个事物一旦提交,它对数据库的改变就是永久的

五个隔离级别

1.default:默认的事务隔离级别,跟具体的数据有关,mysql默认的事务隔离级别是repeatable_read

2.read_uncommitted: 读未提交,一个事务可以感知或者操作另外一个未提交的事务,可能会出现脏读、不可重复读、幻读

3.read_committed:读已提交,一个事务只能感知或者操作另一个已经提交的事务,可能会出现不可重复读、幻读

4.repeatable_read:可重复读,能够避免脏读,不可重复读,不能避免幻读

4.serializable:串行化,隔离级别最高,消耗资源最低,代价最高,能够防止脏读, 不可重复读,幻读。

七个传播特性

1、Propagation.REQUIRED

调用方已经存在事务,则加入到同一个事务中运行,否则,自启一个事务    

2、Propagation.REQUIRES_NEW

无论何时自身都会开启新事务

3、Propagation.SUPPORTS

调用方存在事务,则加入到同一个事务中运行,若不存在事务,则以非事务的方式运行

4、Propagation.NOT_SUPPORTED

调用方存在事务,则会被挂起,直到被调用方运行完毕后,事务恢复。

5、Propagation.MANDATORY

调用方存在事务,则加入到同一个事务中运行,若不存在,则抛出异常

6、Propagation.NEVER

调用方存在事务,则抛出异常

7、Propagation.NESTED

若调用方存在事务,则运行一个嵌套事务,若调用方不存在事务,则以Propagation.REQUIRED的方式运行,即开启一个新的事务

事物配置及其相关要点

知识点

  DBCP:

    导入两个包

    核心类:BasicDataSource

  C3P0:

    导入一个包

    核心类:CombopooledDataSource

  C3P0代码实现:

<!-- 读取数据库配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- C3P0 用的最多的 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="DriverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

Spring事物管理

    事物:逻辑是上一套操作,针对这和操作的各个单元,要么全部成功,要么全部失败

    主要三个对象:

      事物管理平台:platfromTranscactionManager 接口

      事物定义信息:TranscactionDefinition

        定义事物的隔离级别,传播行为

      事物的状态:记录事物管理过程中,事物的状态的独享

  当我们Spring进行事物管理的时候,事物管理平台会根据事物定义的信息去进行管理,然后产生 的状态,将这些状态信息记录状态对象中。

XML方式:

  1、先导入包,然后加约束

  2、配置事物管理器

  <!-- 配置事务管理平台 -->

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

  3、增强配置

<!-- 增强配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- <tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="dele*" propagation="REQUIRED"/>-->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

   4、AOP配置

<!-- AOP配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.xxx.service.BankServiceImpl.*(..))"
id="bankPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="bankPointcut"/>
</aop:config>

spring需要的所有约束

<?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
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

原文地址:https://www.cnblogs.com/alomsc/p/10399628.html