从来就没有救世主  也不靠神仙皇帝  要创造人类的幸福  全靠我们自己  

spring 事务管理

 

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

 

  ①指定数据源:c3p0、druid、DriverManagerDataSource

<!--数据源-->
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/testx?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf8"/>
    <property name="username" value="root"/>
    <property name="password" value="xxx"/>
</bean>

 

  ②配置事务管理器

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource1">  <!--注入数据源-->
    </property>
</bean>

  ③配置事务通知,设置事务属性

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--③配置事务属性
        isolation:事务隔离级别
        propagation:事务传播行为,默认REQUIRED
        read-only:
        timeout:默认-1,表示永不超时
        rollback-for:指定异常,发生此异常时事务回滚,其他异常不会滚;无默认值表示任何异常都回滚
        no-rollback-for:指定异常,发生此异常时事务不会滚,其他异常回滚;无默认值表示任何异常都回滚
    -->
    <tx:attributes>
        <tx:method name="*" read-only="false" propagation="REQUIRED"/>
        <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
    </tx:attributes>
</tx:advice>

  第一个method,对所有方法设置事务属性;第二个通过通配符对 findxxx 方法设置事务属性(优先使用这个)

  ④切入点表达式,确定通知和切入点的关系

<aop:config>
    <aop:pointcut id="pt1" expression="execution(public * com.database.service.impl.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>

 

 

 

注解形式的:

  添加对注解的支持:

<tx:annotation-driven transaction-manager="transactionManager"/>

  上面xml的③、④步可以略去,直接在类/接口/方法上加事务注解,配置事务属性:

  放在接口上,表示对该接口的所有实现类都有事务支持;放在类上,表示对类的所有方法实现此事务支持;放在方法上,表示对本方法实现事务支持(优先级:方法>类>接口)

  注解:例

@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)

 

 

关于事务属性:

(1)隔离级别

ISOLATION_READ_UNCOMMITTED   可读取未提交数据,查询没有加锁,可能导致脏读、不可重复读、幻读
ISOLATION_READ_COMMITTED     只能读取已提交数据,解决了脏读问题(oracle、sql server默认)
ISOLATION_REPEATABLE_READ    可重复读(mysql默认)
ISOLATION_SERIALIZABLE       



脏读:读取了别的事务未提交的数据
不可重复读:在一个事务多次以同样条件读取数据期间,有别的事务修改数据,导致多次读取的结果不一样
幻读:在一个事务两次查询期间,另一个事务新增数据,致使第二次读比前一次读多了结果

 

(2)事务传播

REQUIRED:如果当前没有事务,就新建一个事务;如果已经在一个事务中,就加入到这个事务(默认)
SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行

 

(3)只读型事务

  读写型事务:增、删、该会开启事务

  只读型事务:执行查询也开启事务

posted @ 2020-06-24 23:26  T,X  阅读(127)  评论(0编辑  收藏  举报