美文网首页
Spring 事务

Spring 事务

作者: tingshuo123 | 来源:发表于2018-07-20 22:19 被阅读7次

一些 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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd"
        > 

    <!-- ============== 基本Bean ============== -->
    <bean id="BankServiceImpl" class="com.project.service.impl.BankServiceImpl">
        <property name="dao" ref="IBankDao"></property>
    </bean>
    
    <!-- ============== 整合Mybatis ============== -->
    <!-- 配置连接数据库环境 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/mybaits"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value=""></property>
    </bean>
    
    <!-- 配置 mybatis 的 SqlSessionFactory 工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定环境 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 指定要加载的配置文件 -->
        <property name="configLocation" value="classpath:config/mybatisConfig.xml"></property>
        <!-- <property name="mapperLoactions" value="classpath:com/project/mapper/UserMapper.xml"></property> -->
    </bean> 
    
    <!-- 创建数据映射器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.project.dao"></property>
    </bean>
    
    <!-- 创建事务管理器:针对jdbc或mybatis -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 指定环境 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 使用spring控制事务,使用动态代理实现 -->
    <bean id="proxyTran" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!-- 指定目标对象, 通过id指定 -->
        <property name="target" ref="BankServiceImpl"></property>
        <!-- 注入事务管理器, 通过id指定 -->
        <property name="transactionManager" ref="transactionManager"></property>
        <!-- 设置事务属性 -->
        <property name="transactionAttributes">
            <props>
                <!-- 指定方法,并设置传播行为与隔离级别 -->
                <prop key="transferAccounts">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
            </props>
        </property>
    </bean>
</beans>

目标对象

package com.project.service.impl;

import com.project.dao.IBankDao;
import com.project.exception.MyException;
import com.project.service.IBankService;

public class BankServiceImpl implements IBankService {

    private IBankDao dao = null;
    // 转账
    @Override
    public void transferAccounts (int destId, int srcId, int money) {
        // 加钱
        dao.updateMoneyAdd(destId, money);
        // 模拟断电、死机情况引起的转账失败。
        System.out.println(10 / 0);
        // 减钱
        dao.updateMoneyMinus(srcId, money);
    }

    public IBankDao getDao() {
        return dao;
    }

    public void setDao(IBankDao dao) {
        this.dao = dao;
    }
}

测试

package com.project.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.project.exception.MyException;
import com.project.service.IBankService;

public class TestMain {
    
    public static void main(String[] args) {
        
        ApplicationContext context = new ClassPathXmlApplicationContext("config/springConfig.xml");
        // 获取 BankService 的动态代理对象
        IBankService service = (IBankService) context.getBean("proxyTran");
        
        service.transferAccounts(2, 1, 1000);
    }
}

小结:spring 的事务管理是通过动态代理实现的,所以我们应该获取BankService动态代理对象才会有事务,直接获取Bean是没有的。

用注解配置

xml 配置 片段

别的都跟上面一样


    <!-- 创建事务管理器:针对jdbc或mybatis -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 指定环境 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启注解扫描 -->
    <context:component-scan
        base-package="com.project.service"></context:component-scan>
    <!-- 注解方式实现事务: 指定注解方式实现事务 -->
    <tx:annotation-driven
        transaction-manager="transactionManager" />

注解

在要开启事务的方法上,添加注解就可以了,实现类,会把注解也继承过去。

public interface IBankService {
    
    @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT)
    public void transferAccounts(int destId, int srcId, int money);
}

相关文章

  • 关于事务的思考

    Spring对于事务的支持 Spring事务接口 Spring事务管理器 Spring并不直接管理事务,而是提供多...

  • Spring事务

    Spring 事务 分类 Spring可以支持编程式事务和声明式事务。 编程式事务 实现 Spring使用事务管理...

  • Spring中实现事务方式

    Spring 中实现事务的方式 Spring 并不直接支持事务,只有当数据库支持事务时,Spring 才支持事务,...

  • Spring事务的传播特性引发的一场血案

    Spring事务的传播特性是对于Spring事务管理的一项特殊配置;Spring事务基于Spring AOP特性,...

  • Spring 事务实现分析

    1. Spring 事务简介 Spring 本身并不实现事务,Spring事务 的本质 还是 底层数据库 对事务的...

  • spring04

    Spring JdbcTemplate学习 Spring 声明式事务 xml配置实现 Spring 声明式事务 注...

  • 程序员之Spring

    1. Spring事务 spring的事务实现原理Spring事务将connection放入到当前线程的threa...

  • 面前温习

    Spring事务传播特性的浅析——事务方法嵌套调用的迷茫 解惑 spring 嵌套事务

  • 分布式事务(2)Spring事务

    2.1 Spring事务原理 Spring支持编程式事务和声明式事务。编程式事务就是用个事务类Transactio...

  • spring事务的实现原理

    spring实现事务的原理 Spring事务 的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring...

网友评论

      本文标题:Spring 事务

      本文链接:https://www.haomeiwen.com/subject/liccmftx.html