Spring事务(2):声明式事务管理案例-转账(xml、注解)

news2024/9/25 15:23:31

1 编写转账案例,引出事务管理问题

需求:账号转账,Tom账号取出1000元,存放到Jack账号上

1.1 建表脚本(MySQL)

 CREATE TABLE t_account  (
 id INT(11) NOT NULL AUTO_INCREMENT,
 name VARCHAR(20) NOT NULL,
 money DOUBLE DEFAULT NULL,
 PRIMARY KEY (id)
)  

INSERT INTO `t_account` (`id`, `name`, `money`) VALUES ('1', 'tom', '1000');
INSERT INTO `t_account` (`id`, `name`, `money`) VALUES ('2', 'jack', '1100');
INSERT INTO `t_account` (`id`, `name`, `money`) VALUES ('3', 'rose', '2000');

1.2 新建工程

第一步:新建一个maven项目

第二步:引入依赖和applicationContext.xml配置文件和log4j.properties文件和db.properties文件:

pom.xml:

    <dependencies>
        <!-- junit测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

        <!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
        <!-- spring集成测试 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>

        <!-- spring事物管理 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>


        <!-- c3p0数据源 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <!-- 注解开发切面包 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>

    </dependencies>

applicationContext.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:p="http://www.springframework.org/schema/p"
       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">
</beans>

log4j.properties:

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=info, stdout

db.properties:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.222.156:3306/spring?characterEncoding=utf8&useSSL=false
jdbc.user=root
jdbc.password=123456

第三步:创建IAccountDao接口

创建AccounDaoImpl实现类,实现了IAccountDao接口

账户操作持久层

技术方案:jdbctempate

package com.example.demo.dao;

public interface IAccountDao {
    // 转出
    public void out(String name, Double money);

    // 转入
    public void in(String name, Double money);
}

第四步:建立service层,创建IAccountService接口,编写转账的业务代码:

package com.example.demo.service;

public interface IAccountService {
    //转账业务:
    public void transfer(String outName,String inName,Double money);
}

package com.example.demo.service.impl;

import com.example.demo.dao.IAccountDao;
import com.example.demo.service.IAccountService;

public class AccountServiceImpl implements IAccountService {
    // 注入dao
    private IAccountDao accountDao;

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

    // 转账业务
    public void transfer(String outName, String inName, Double money) {

        // 先转出
        accountDao.out(outName, money);

        // 再转入
        accountDao.in(inName, money);

    }
}

第五步: 将对象配置到spring工厂

applicationContext.xml文件添加配置

    <!-- 引入配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
    </bean>


    <!-- 管理dao和service -->
    <bean id="accountDao" class="com.example.demo.dao.impl.AccountDaoImpl">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="accountService" class="com.example.demo.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

第六步:使用SpringTest进行测试

package com.example.demo.service.impl;

import com.example.demo.service.IAccountService;
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;

//集成spring测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AccountServiceImplTest {

    //注入测试对象
    @Autowired
    private IAccountService accountService;


    @Test
    public void testTransfer() {
        accountService.transfer("tom", "jack", 1000d);
    }

}

但是发现问题:

事务管理问题:在Service层没有事务的情况下,如果出现异常,则会转账不成功,数据异常。

在转账方法中添加如下异常:

运行前:

运行后:

事务未生效。

注意:如果不配置事务,那么每一个数据库的操作都是单独的一个事务。

2 XML配置方式添加事务管理(tx、aop元素)

【操作思路】:aop三步走

  1. 确定目标:需要对AccountService 的 transfer方法,配置切入点
  2. 需要Advice (环绕通知),方法前开启事务,方法后提交关闭事务
  3. 配置切面和切入点

配置Advice通知:

Spring为简化事务的配置,提供了**<tx:advice>**来配置事务管理,也可以理解为该标签是spring为你实现好了的事务的通知增强方案。

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

	<!-- 配置事物通知 -->
	<!-- transaction-manager: 指定事物管理器的id,如果事物管理器的id为transactionManager的话 该属性可以省略(缺省值) -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 配置事物管理细则(事物定义信息) -->
		<tx:attributes>
			<!-- 需要被增强(事物 管理)的方法 -->
			<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" timeout="-1" />
		</tx:attributes>
	</tx:advice>

	<!-- 配置切入点和切面 -->
	<aop:config>
		<aop:pointcut expression="bean(*Service)" id="mycut" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="mycut" />
	</aop:config>

使用AccountServiceImplTest.java测试:数据正常!

事物添加的前后对比

没有添加事务:

两个方法分属不同事务。

添加事务后:

分属同一事务

【注意】如果不配置,则走默认的事务(默认事务是每个数据库操作都是一个事务,相当于没事务),所以我们开发时需要配置事务。

3 注解配置方式添加事务管理 @Transactional

步骤:

  1. 在需要管理事务的方法或者类上面 添加@Transactional 注解
  2. 配置注解驱动事务管理(事务管理注解生效的作用)(需要配置对特定持久层框架使用的事务管理器)

创建项目spring_transaction_anntx:

替换applicationContext.xml中的<bean> 配置为注解

改造dao:

package com.example.demo.dao.impl;

import com.example.demo.dao.IAccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;

@Repository("accountDao")
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {

    //将数据源注入给父类,父类中需要通过数据源创建jdbctemplate
    @Autowired
    public void setSuperDataSource(DataSource dataSource){
        super.setDataSource(dataSource);
    }

    public void out(String name, Double money) {
        String sql = "update t_account set money = money-? where name = ?";
        super.getJdbcTemplate().update(sql, money, name);

    }

    public void in(String name, Double money) {
        String sql = "update t_account set money = money+? where name = ?";
        super.getJdbcTemplate().update(sql, money, name);
    }
}

改造service:

package com.example.demo.service.impl;

import com.example.demo.dao.IAccountDao;
import com.example.demo.service.IAccountService;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    // 注入dao
    @Autowired
    private IAccountDao accountDao;

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

    // 转账业务
    public void transfer(String outName, String inName, Double money) {

        // 先转出
        accountDao.out(outName, money);

        // 再转入
        accountDao.in(inName, money);

    }
}

在applicationContext.xml中配置注解扫描:

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.example.demo" />

测试方法是否能正常运行

以上步骤全部没问题后,开始配置注解方式的事物管理

第一步:配置 事物管理器:

在applicationContext.xml中,根据选用的持久层框架配置事物管理器:

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

第二步: 在需要管理事物的方法上添加@Transactional注解,表示对该方法进行事物管理

第三步:在applicationContext.xml中开启事物注解驱动,让@Transactional注解生效

<?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:p="http://www.springframework.org/schema/p"
       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" />
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.example.demo" />

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

    <!--配置事务注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

第四步:测试事物是否正常

提示:

如果 @Transactional 标注在 Class 上面, 那么将会对这个 Class 里面所有的 public 方法都包装事务方法。等同于该类的每个公有方法都放上了@Transactional。

如果某方法需要单独的事务定义,则需要在方法上加@Transactional来覆盖类上的标注声明。记住:方法级别的事务覆盖类级别的事务(就近原则)

package com.example.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.dao.IAccountDao;
import com.example.demo.service.IAccountService;

@Service("accountService")
@Transactional
// 放置在类上表示对该类中所有的方法都进行事物管理
public class AccountServiceImpl implements IAccountService {

   // 注入dao
   @Autowired
   private IAccountDao accountDao;

   // 转账业务
   @Transactional
   public void transfer(String outName, String inName, Double money) {

   	// 先转出
   	accountDao.out(outName, money);

   	// 发生异常
   	int i = 1 / 0;

   	// 再转入
   	accountDao.in(inName, money);

   }

   @Transactional(readOnly = true)
   // 当方法上的事物定义信息和类上的冲突时,就近原则使用方法上的配置
   public Double queryMoney(String name) {
   	// TODO Auto-generated method stub
   	return null;
   }

}

4 小结-xml和注解的选择

XML配置方式和注解配置方式进行事务管理 哪种用的多?

XML方式,集中式维护,统一放置到applicationContext.xml文件中,缺点在于配置文件中的内容太多。

使用@Transactional注解进行事务管理,配置太分散,使用XML进行事务管理,属性集中配置,便于管理和维护

注意:以后的service的方法名字的命名,必须是上面规则,否则,不能被spring事务管理。!!!!

即以save开头的方法,update开头的方法,delete开头的方法,表示增删改的操作,故事务为可写

以find开头的方法,表示查询,故事务为只读

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1364480.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Arduino开发实例-欧姆龙E3Z-D61光电传感器

欧姆龙E3Z-D61光电传感器 文章目录 欧姆龙E3Z-D61光电传感器1、E3Z-D61光电传感器介绍2、硬件准备及接线3、代码实现1、E3Z-D61光电传感器介绍 Omran 光电传感器可用于检测 5 至 100 毫米距离内的障碍物和物体。 传感器上有一个 LED,它始终熄灭,并在检测到障碍物时亮起。 您…

Plantuml之nwdiag网络图语法介绍(二十九)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

IDEA[Debug]简单说明

目录 &#x1f95e;1.打断点 &#x1f32d;2.第一组按钮 &#x1f9c2;3.第二组按钮 &#x1f953;4.参数查看 1.打断点 1.在需要断点处打上断点&#xff0c;然后点击debug运行 2.执行debug&#xff0c;直接执行到断点处 2.第一组按钮 共有8按钮&#xff0c;从左往右依…

【普中开发板】基于51单片机的简易密码锁设计( proteus仿真+程序+设计报告+讲解视频)

基于51单片机的简易密码锁设计 1.主要功能&#xff1a;资料下载链接&#xff1a; 实物图&#xff1a;2.仿真3. 程序代码4. 设计报告5. 设计资料内容清单 【普中】基于51单片机的简易密码锁设计 ( proteus仿真程序设计报告讲解视频&#xff09; 仿真图proteus8.16(有低版本) 程…

Defi安全-Monox攻击事件Foundry复现

其它相关内容可见个人主页 Mono攻击事件的介绍见&#xff1a;Defi安全–Monox攻击事件分析–phalconetherscan 1. 前情提要和思路介绍 Monox使用单边池模型&#xff0c;创建的是代币-vCash交易对&#xff0c;添加流动性时&#xff0c;只需添加代币&#xff0c;即可进行任意代…

秋招复习之堆

目录 前言 堆 堆的常用操作 堆的实现&#xff08;大根堆&#xff09; 1. 堆的存储与表示 2. 访问堆顶元素 3. 元素入堆 4. 堆顶元素出堆 Top-k 问题 方法一&#xff1a;遍历选择 方法二&#xff1a;排序 方法三&#xff1a;堆 总结 前言 秋招复习之堆。 堆 「堆 heap…

分布式系统——广播Broadcasts

1 广播抽象(Broadcast Abstractions)在进程中的两种方法 在分布式系统中广播抽象的概念。广播抽象允许系统中的进程使用两种基本方法进行通信&#xff1a; 1.1 Broadcast(m) 当一个进程 i 使用这个方法时&#xff0c;它会将消息 m 发送给系统中的所有其它进程。 1.2 …

数据结构实验1:栈和队列的应用

目录 一、实验目的 二、实验原理 1.1栈的基本操作 1.1.1 栈的定义 1.1.2 初始化栈 1.1.3 压栈&#xff08;Push&#xff09; 1.1.4 出栈&#xff08;Pop&#xff09; 1.1.5 判空&#xff08;isEmpty&#xff09; 1.1.6 查看栈顶元素&#xff08;Top&#xff09; 1.1…

【好书推荐】我的第一本科技漫画书:漫画区块链

王杰&#xff0c;南京理工大学物理电子学硕士&#xff0c;曾担任乐视VR技术总监&#xff0c;现为北京米唐科技有限公司CEO&#xff0c;知乎“区块链”领域知名作者&#xff0c;北京信息科技大学、北京建筑大学、北京信息职业技术学院客座教授。 郑巍&#xff0c;擅长绘制钢笔淡…

application.properties 如何改成 application.yml

Convert YAML and Properties File 右键直接转换即可 Further Reading &#xff1a; idea 常用插件

14_IO_其他流

文章目录 数据流DataOutputStream数据输出流DataInputStream数据输入流 打印流PrintStream字节打印流PrintWriter字符打印流 标准输入输出流标准输入流标准输出流 对象流(序列化与反序列化流)ObjectOutputStream序列化流ObjectInputStream反序列化流 RandomAccessFile随机访问文…

【Harmony OS - 网络请求】

在一个应用开发中&#xff0c;网络请求是必不可少的&#xff0c;我们一般用的fetch、axios来进行http请求&#xff0c;在鸿蒙中也可以通过createHppt来发生一个http请求&#xff0c;它们都是异步请求返回的Promise&#xff0c;下面我们将介绍’ohos.net.http’和axios这两种方式…

(21)Linux的文件描述符输出重定向

一、文件描述符 1、文件描述符的底层理解 在上一章中&#xff0c;我们已经把 fd 的基本原理搞清楚了&#xff0c;知道了 fd 的值为什么是 0,1,2,3,4,5... 也知道了 fd 为什么默认从 3 开始&#xff0c;而不是从 0,1,2&#xff0c;因为其在内核中属于进程和文件的对应关系。 …

Mysql SQL审核平台Yearning本地部署

文章目录 前言1. Linux 部署Yearning2. 本地访问Yearning3. Linux 安装cpolar4. 配置Yearning公网访问地址5. 公网远程访问Yearning管理界面6. 固定Yearning公网地址 前言 Yearning 简单, 高效的MYSQL 审计平台 一款MYSQL SQL语句/查询审计工具&#xff0c;为DBA与开发人员使用…

Python的基础练习题之学生管理系统

需求 使用Python基础写一个基于控制台的学生管理平台&#xff0c;里面功能分别是&#xff1a;1.录入学生信息2.查找学生信息3.删除学生信息4.修改学生信息5.排序6.统计学生总人数7.显示所有学生信息&#xff0c;要求数据存储在文件里。 代码 代码资源地址可以直接下载 效果图…

05 Ciso模拟器连接腾讯云物联网开发平台

Ciso声明&#xff1a;本篇文章基于使用腾讯云物联网平台连接自定义esp8266物联网设备(腾讯连连控制开关实现) - CSDN App改编 一、总体概览 功能描述&#xff1a; 使用腾讯连连小程序进行控制&#xff0c; Alarm&#xff08;警铃&#xff09;&#xff1a;开的时候&#xff…

【AI视野·今日Robot 机器人论文速览 第六十九期】Wed, 3 Jan 2024

AI视野今日CS.Robotics 机器人学论文速览 Wed, 3 Jan 2024 Totally 5 papers &#x1f449;上期速览✈更多精彩请移步主页 Daily Robotics Papers NID-SLAM: Neural Implicit Representation-based RGB-D SLAM in dynamic environments Authors Ziheng Xu, Jianwei Niu, Qingf…

实现在一个文件夹中找到特定名称特点格式的文件

当你要在一个文件夹中查找特定名称和格式的文件时&#xff0c;你可以使用 Python 的 os 和 fnmatch 模块。以下是一个简单的脚本示例&#xff0c;它可以在指定目录中查找文件&#xff1a; import os import fnmatchdef find_files(directory, pattern):"""在指…

C#使用栈方法遍历二叉树

步骤一&#xff1a;定义一个二叉树的节点类 定义一个二叉树的节点类&#xff0c;其中包含节点的值、左子节点和右子节点。 // 二叉树节点定义public class TreeNode{public int Value { get; set; } // 节点的值public TreeNode Left { get; set; } // 左子节点public TreeN…

Java Base64简单介绍

1. Base64工具 工具链接 2. Base64示例代码 public class Base64Demo {// 请注意&#xff0c;在处理二进制数据时&#xff08;例如图片或文件&#xff09;&#xff0c;不需要将字节数组转换为字符串再进行编码或解码&#xff0c;// 可以直接对字节数组进行Base64操作。上述…