spring 中的事务管理

  1. 在 Spring 框架中事务管理有两种方式:一种是传统的编程式事务管理,即通过编写代码实现的事务管理,就是用try catch代码块来捕捉错误;另一种是基于 AOP 技术实现的声明式事务管理,Spring 的声明式事务管理在底层采用了 AOP 技术,其最大的优点在于无须通过编程的方式管理事务,只需要在配置文件中进行相关的规则声明,就可以将事务规则应用到业务逻辑中。

  2. Spring 实现声明式事务管理主要有两种方式:

    • 基于 XML 文件方式的声明式事务管理。
    • 通过 Annotation 注解方式的事务管理。
  3. 基于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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--从classpath的根路径去加载db.properties文件-->
        <context:property-placeholder location="classpath:db.properties"/>
    
        <!--配置一个druid的连接池-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
              init-method="init" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
        <!--配置dao-->
        <bean id = "accountDao" class="com.test.dao.impl.AccountDaoImpl">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!--配置service-->
        <bean id="accountService" class="com.test.service.impl.AccountServiceImpl">
            <property name="dao" ref="accountDao"/>
        </bean>
    
        <!-- 1: 配置JDBC事务管理器 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 2: 配置事务管理器增强 -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="trans"/>
            </tx:attributes>
        </tx:advice>
        
        <!-- 3: 配置切面 -->
        <aop:config>
            <aop:pointcut id="txPointcut" expression="execution(* com.test.service.*Service.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>
    </beans>
    
    • CRUD通用事务配置
    <!--配置一个CRUD的通用事务的配置-->
    <tx:advice id="crudAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!--service中的查询方法-->
            <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="list*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="query*" read-only="true" propagation="REQUIRED"/>
            <!--service中其他方法(非查询)-->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
  4. 基于注解配置声明式事务

    • 基于注解配置事务:在Service中,使用 @Transactional注解
    • xml配置
      <!--开启注解-->
      <context:annotation-config/>
      <!--配置IoC扫描包-->
      <context:component-scan base-package="com.test"/>
    
      <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"/>
      </bean>
      <!--配置TX注解解析器-->
      <tx:annotation-driven transaction-manager="txManager"/>
    
      <context:property-placeholder location="classpath:db.properties"/>
    
      <!--配置一个druid的连接池-->
      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
            init-method="init" destroy-method="close">
          <property name="driverClassName" value="${jdbc.driverClassName}"/>
          <property name="url" value="${jdbc.url}"/>
          <property name="username" value="${jdbc.username}"/>
          <property name="password" value="${jdbc.password}"/>
      </bean>
    
posted @ 2024-05-13 16:54  Hanyta  阅读(9)  评论(0编辑  收藏  举报