Spring中使用事务搭建转账环境 转账操作一个账户要减少资金操作一个账户要增加资金操作,如果在两个操作间出现异常转账失败 所以要使用事务

本文主要是介绍Spring中使用事务搭建转账环境 转账操作一个账户要减少资金操作一个账户要增加资金操作,如果在两个操作间出现异常转账失败 所以要使用事务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

演示不使用事务出现异常情况

Dao层两个方法lessMoney()和moreMoney()

package com.swift;import org.springframework.jdbc.core.JdbcTemplate;public class AccountDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}//少钱的方法public void lessMoney(String from,double number) {String sql="update account set money=money-? where username=?";jdbcTemplate.update(sql, number,from);}//多钱的方法public void moreMoney(String to,double number) {String sql="update account set money=money+? where username=?";jdbcTemplate.update(sql, number,to);}
}

Service层调用两个方法

package com.swift;public class AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}public void moneyTransfer(String from,String to,double number) {accountDao.lessMoney(from,number);int i=10/0;accountDao.moreMoney(to,number);}
}

但是两个操作减与加之间,如果出现异常,则会导致转账钱已经转了,但对方却没有到账的bug,可能服务器突然故障等引起


 

解决添加事务,出现异常进行回滚操作

下面使用配置文件的方法进行事务管理

没有改变的测试类

package com.swift;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;@WebServlet("/test")
public class ServletTest extends HttpServlet {private static final long serialVersionUID = 1L;public ServletTest() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");response.getWriter().append("Served at: ").append(request.getContextPath());//使用JdbcTemplat的queryForObject方法ApplicationContext context=new ClassPathXmlApplicationContext("c3p0.xml");AccountService accountService= (AccountService) context.getBean("accountService");accountService.moneyTransfer("佣兵组织", "高扬", 100000);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}

使用配置文件进行事务处理步骤如下:

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- c3p0连接池得到dataSource --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property><property name="user" value="root"></property><property name="password" value="root"></property></bean><!-- 第一步 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 注入dataSource --><property name="dataSource" ref="dataSource"></property></bean><!-- 第二步 配置事务增强 --><tx:advice id="txadvice" transaction-manager="transactionManager"><!-- 做事务操作 --><tx:attributes><!-- 事务操作的方法匹配规则 --><tx:method name="money*" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 第三步 配置切面 --><aop:config><!-- 切入点 --><aop:pointcut expression="execution(* com.swift.AccountService.*(..))" id="pointcut1"/><!-- 切面 --><aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/></aop:config><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><bean id="accountDao" class="com.swift.AccountDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="accountService" class="com.swift.AccountService"><property name="accountDao" ref="accountDao"></property></bean></beans>

分三个步骤 管理 增强和切面

虽然还是会有异常,但是数据库中不会出错,不会钱转出了,却没有到账

工具类Account

package com.swift;public class Account {private int id;private String username;private String money;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getMoney() {return money;}public void setMoney(String money) {this.money = money;}public Account(int id, String username, String money) {this.id = id;this.username = username;this.money = money;}public Account() {super();// TODO Auto-generated constructor stub
    }@Overridepublic String toString() {return "Account [id=" + id + ", username=" + username + ", money=" + money + "]";}}

 

这篇关于Spring中使用事务搭建转账环境 转账操作一个账户要减少资金操作一个账户要增加资金操作,如果在两个操作间出现异常转账失败 所以要使用事务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/762193

相关文章

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数