Spring 事务(一)

使用

1、pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.1.9.RELEASE</version>
</dependency>

2、事务配置

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.mystudy.dao"/>
    <context:component-scan base-package="com.mystudy.service"/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/study?useSSL=false&amp;serverTimezone=UTC&amp;CharacterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="3145tj"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>

    <!--1、配置一个事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>
    <!--2、配置事务的通知引用事务管理器-->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <!--3、配置事务的属性-->
        <tx:attributes>
            <!--实际开发中的配置-->
            <!--查询方法-->
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
            <!--更新方法-->
            <tx:method name="add*" propagation="REQUIRED" read-only="false"/>
            <!--标记存在默认属性值,可以省略不写-->
            <tx:method name="save*"/>
            <tx:method name="insert*"/>
            <tx:method name="delete*"/>
            <tx:method name="remove*"/>
            <tx:method name="drop*"/>
            <tx:method name="update*"/>
            <tx:method name="change*"/>

            <!--其他任意方法-->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <!--4、aop配置-->
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* com.mystudy.service.*.*(..)))"></aop:pointcut>
        <aop:advisor advice-ref="myAdvice" pointcut-ref="pt"></aop:advisor>
    </aop:config>
</beans>

事务核心配置

1、事务管理器

  • 如果基于Spring JDBCh或Mybatis开发:

org.springframework.jdbc.datasource.DriverManagerDataSource

构造方法参数为dataSource

  • 使用Hibernate版本进行持久化数据时使用:

org.springframework.orm.hibernate5.HibernateTransactionManager

  • 使用JPA版本进行持久化数据时使用:

org.springframework.orm.jpa.JpaTransactionManager

2、配置事务通知

<tx:advice id="myAdvice" transaction-manager="transactionManager"></tx:advice>

3、事务的属性

<tx:attributes>
	<tx:method></tx:method>
</tx:attributes>
  • 事务对象名称
  • 事务隔离级别
  • 事务传播行为
  • 事务超时时间
  • 事务是否只读

4、aop配置

<aop:config>
    <aop:pointcut id="pt" expression="execution(* com.mystudy.service.*.*(..)))"></aop:pointcut>
    <aop:advisor advice-ref="myAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>

将事务管理器与需要配置事务的方法关联起来

事务属性

1、事务对象名称:name属性

指定方法名称,是业务核心方法

2、事务隔离级别:isolation属性

ISOLATION_DEFAULT:默认级别

ISOLATION_READ_UNCOMMITTED:读未提交

ISOLATION_READ_COMMITTED:读已提交

ISOLATION_REPEATABLE_READ:可重复读

ISOLATION_SERIALIZABLE:可串行化的

3、传播行为:propagation属性

REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。(默认值,一般更新操作选择该行为)

SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务,一般查询操作选择该行为)

MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常

REQUERS_NEW:新建事务,如果当前在事务中,把当前事务挂起。

NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起

NEVER:以非事务方式运行,如果当前存在事务,抛出异常

NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行REQUIRED类似的操作。

4、超时时间:timeout属性

默认值是-1,没有超时限制。如果有,以秒为单位进行设置。

5、是否只读事务:read-only属性

默认false,不只读。只读表示锁表。建议查询时设置为只读。

6、指定异常

  • rollback-for:用于指定一个异常,当执行业务方法产生该异常时,事务回滚。产生其他异常,事务不回滚。没有默认值,任何异常都回滚。
  • no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,任何异常都回滚。
原文地址:https://www.cnblogs.com/heibaimao123/p/13793982.html