当前位置: 首页 > article >正文

Spring框架的事务管理

目录

一、spring框架事务管理相关的类

1.PlatformTransactionManager接口

2.TransactionDefinition接口

二、spring框架声明式事务管理

1.配置文件的方式

(1)配置文件

(2)业务层

(3)持久层

(4)测试类

(5)运行

(6)查看数据库

(7)出现异常运行

2.半注解的方式

(1)配置文件

(2)db.properties

(3)持久层

(4)业务层

(5)测试类

(6)运行

(7)查看数据库

(8)加上异常

3.纯注解的方式

(1)持久层

(2)业务层

(3)配置类

(4)测试类

(5)运行

(6)查看数据库

(7)加上异常


一、spring框架事务管理相关的类

1.PlatformTransactionManager接口

平台事务管理器

2.TransactionDefinition接口

事务定义信息的接口

定义了事务隔离级别和事务传播行为

二、spring框架声明式事务管理

它有三种方式:配置文件的方式、半注解的方式(配置文件+注解)、纯注解的方式

每种方式都包括配置文件/配置类,业务层,持久层,测试类,运行结果及检验,还有模拟异常

1.配置文件的方式

(1)配置文件

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

">



    <!--第二种写法:使用提供标签的方式-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--加载属性的文件-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--配置平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="pay" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="find" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--配置AOP的增强-->
    <aop:config>
        <!--spring框架提供系统通知,是使用advisor标签-->
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.qcby.service.impl.AccountServiceImpl.pay(..))"/>
    </aop:config>

    <!--配置service-->
    <bean id="accountService" class="com.qcby.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <bean id="accountDao" class="com.qcby.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>



</beans>

(2)业务层

import com.qcby.dao.AccountDao;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;


public class AccountServiceImpl implements AccountService {


    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void pay(String out, String in, double money) {

        // 调用dao方法
        accountDao.outMoney(out,money);
        //异常
        //int a=1/0;
        accountDao.inMoney(in,money);
    }
}

(3)持久层

import com.qcby.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import javax.annotation.Resource;


public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {


    @Override
    public void outMoney(String out, double money) {
        this.getJdbcTemplate().update("update account set money = money - ? where name = ?",money,out);
    }

    @Override
    public void inMoney(String in, double money) {
        this.getJdbcTemplate().update("update account set money = money + ? where name = ?",money,in);
    }
}

(4)测试类

import com.qcby.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext.xml")
public class demo {
    @Autowired
    private AccountService accountService;

    @Test
    public void run(){
        accountService.pay("李四","张三",100);
    }
}

(5)运行

(6)查看数据库

原来

现在

 

(7)出现异常运行

查看数据库(数据没变)

 

2.半注解的方式

(1)配置文件

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

">



    <context:component-scan base-package="com.qcby"></context:component-scan>

    <!--第二种写法:使用提供标签的方式-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--加载属性的文件-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--配置事务平台管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置JDBC模板类-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--开启事务注解的支持-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>


</beans>

(2)db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_db
jdbc.username=root
jdbc.password=2020

(3)持久层

import com.qcby.dao.AccountDao;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void pay(String out, String in, double money) {
        accountDao.outMoney(out,money);
        //模拟异常
        //int a=1/0;
        accountDao.inMoney(in, money);
    }
}

(4)业务层

import com.qcby.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void outMoney(String out, double money) {
        jdbcTemplate.update("update account set money = money - ? where name = ?",money,out);
    }

    @Override
    public void inMoney(String in, double money) {
        jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);
    }
}

(5)测试类

import com.qcby.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class demo {

    @Autowired
    private AccountService accountService;


    @Test
    public void run(){
        accountService.pay("张三","李四",100);
    }
}

(6)运行

(7)查看数据库

原来是600  300

(8)加上异常

查看数据库

 

3.纯注解的方式

(1)持久层

import com.qcby.dao.AccountDao;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void pay(String out, String in, double money) {
        accountDao.outMoney(out,money);
        //模拟异常
        //int a=1/0;
        accountDao.inMoney(in, money);
    }
}

(2)业务层

import com.qcby.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public void outMoney(String out, double money) {
        jdbcTemplate.update("update account set money = money - ? where name = ?",money,out);
    }

    @Override
    public void inMoney(String in, double money) {
        jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);
    }
}

(3)配置类

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.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@ComponentScan("com.qcby")
@EnableTransactionManagement
public class SpringConfig {
    @Bean
    public DataSource createDataSource(){
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///spring_db");
        dataSource.setUsername("root");
        dataSource.setPassword("2020");
        return dataSource;
    }

    @Resource(name = "dataSource")
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    @Resource(name = "dataSource")
    @Bean(name = "transactionManager")
    public PlatformTransactionManager createTransactionManager(DataSource dataSource){
        DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource);
        return manager;
    }


}

(4)测试类

import com.qcby.config.SpringConfig;
import com.qcby.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
public class demo2 {

    @Autowired
    private AccountService accountService;


    @Test
    public void run(){
        accountService.pay("bbb","aaa",100);
    }
}

(5)运行

(6)查看数据库

原来

现在

(7)加上异常

查看数据库(数据没变)


http://www.kler.cn/a/380766.html

相关文章:

  • 868历年真题算法设计题+程序设计题
  • leetcode-3-无重复字符的最长子串
  • Pr 视频效果:过渡
  • 使用Python Flask实战构建Web应用
  • 告别传统营销,HubSpot AI分析工具带你玩转新潮流
  • BERT预训练的MLM和NSP任务的损失函数都是什么?
  • 一文快速预览经典深度学习模型(一)——CNN、RNN、LSTM、Transformer、ViT
  • 架构师之路-学渣到学霸历程-43
  • 只允许指定ip远程连接ssh
  • 【3】流程控制
  • HarmonyOS鸿蒙开发入门,常用ArkUI组件学习(一)
  • Spring cloud
  • QT下载安装
  • 为什么要使用Docker?
  • c# 值类型
  • 青少年编程与数学 02-003 Go语言网络编程 02课题、网络分层模型
  • RHCE selinux 和 防火墙(fireword|iptable)
  • 【里程计在激光雷达SLAM中的作用】【gmapping算法hector_mapping算法】
  • 基于 LR(1) 和 LALR 的 Parser Generator
  • (九)JavaWeb后端开发——Servlet