Spring 声明式事务的实现方式

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

    <context:component-scan base-package="cn.sivan"/>
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--通知-->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--name 方法名的匹配模式,通知根据该模式寻找匹配的方法。该属性可以使用asterisk (*)通配符
            propagation 设定事务定义所用的传播级别
            isolation 设定事务的隔离级别
            timeout 指定事务的超时(单位为秒)
            read-only 该属性为true指示事务是只读的(典型的,对于只执行查询的事务你会将该属性设为true, 如果出现了更新、插入或是删除语句时只读事务就会失败)
            no-rollback-for 以逗号分隔的异常类的列表,目标方法可以抛出 这些异常而不会导致通知执行回滚
            rollback-for 以逗号分隔的异常类的列表,当目标方法抛出这些 异常时会导致通知执行回滚。默认情况下,该列表为空, 因此不在no-rollback-for列表中的任何运行 时异常都会导致回滚
            -->
            <tx:method name="transfer"/>
            <!--<tx:method name="*service"/>-->
        </tx:attributes>
    </tx:advice>

    <!--配置切点,切面-->
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* cn.sivan.service.*.*(..))"/>
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pt"/>
    </aop:config>

</beans>

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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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 http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="cn.sivan"/>
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--开启事务注解的支持-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

使用:
在方法或类上加上@Transactional(传播特性,隔离级别,超时等)

@Transactional
public void transfer() {
    ...
}
  1. 使用配置类来实现事务,完全不是用xml
package cn.sivan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan(basePackages = "cn.sivan")
@EnableTransactionManagement
public class TxConfig {

    /**
     * 设置数据源
     * @return
     */
    @Bean
    public DriverManagerDataSource getDataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///db?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        return dataSource;
    }

    /**
     * JdbcTemplate
     * @param dataSource
     * @return
     */
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
        //return new JdbcTemplate(getDataSource());
    }

    /**
     * 事务管理器
     * @return
     */
    @Bean
    public DataSourceTransactionManager getTransactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

posted @ 2020-08-26 11:28  宁川  阅读(282)  评论(0)    收藏  举报