Spring的事务控制-基于xml方式
介绍:该程序模拟了转账操作,即Jone减少500元,tom增加500元
1.导入坐标
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
2.创建Account实体类
public class Account {
private String name;
private String money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
}
3.创建AccountDao接口以及实现类AccountDaoImpl;
public interface AccountDao {
public void out(String outMan,double money);
public void in(String inMan,double money);
}
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
@Override
public void out(String outMan, double money) {
template.update("update account set money=money-? where name=?",money,outMan);
}
@Override
public void in(String inMan, double money) {
template.update("update account set money=money+? where name=?",money,inMan);
}
}
4.创建AccountService接口以及AccountServiceImpl实现类
public interface AccountService {
public void transfer(String outMan,String inMan,double money);
}
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outMan, String inMan,double money) {
accountDao.out(outMan,money);
accountDao.in(inMan,money);
}
}
5.编写spring配置文件(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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--将AccountDao注入到spring容器-->
<bean id="accountDao" class="com.hao.dao.impl.AccountDaoImpl">
<property name="template" ref="jdbcTemplate"/>
</bean>
<!--将AccountService注入到spring容器-->
<bean id="accountService" class="com.hao.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
<property name="user" value="root"/>
<property name="password" value="hao20001010"/>
</bean>
<!-- 将模板对象注入到spring容器-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
6.数据库表的创建

7.测试
public class AccountController {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService service= context.getBean(AccountService.class);
service.transfer("Jone","tom",500);
}
}
结果:

将Jone和tom的钱修改为5000元

当我们在业务方法transfer中手动加入错误代码,让其报错

再此运行,我们发现控制台报错,接着查看数据库中的数据

出现这样的原因就是,当程序执行完accountDao.out(outMan,money)时,Jone的钱减少500,但是当执行下一步int i=1/0时,程序出现错误,然后就不再执行下一步功能了,所有出现了这样的情况;
这样的问题该怎么解决呢?
利用aop思想,将事务提取出来交给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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--将AccountDao注入到spring容器-->
<bean id="accountDao" class="com.hao.dao.impl.AccountDaoImpl">
<property name="template" ref="jdbcTemplate"/>
</bean>
<!--将AccountService注入到spring容器,目标对象,内部的方法就是切点-->
<bean id="accountService" class="com.hao.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置平台事务管理器,当前的DataSourceTransactionManager是jdbc、mybatis技术,如果以后使用了其他技术,则此处需要修改-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知,事务的增强,引入事务的命名空间tx-->
<tx:advice id="tx" transaction-manager="transactionManager">
<!--设置属性信息的 -->
<tx:attributes>
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop织入,advice-ref引入通知(唯一标识id) pointcut(切入的方法)-->
<aop:config>
<aop:advisor advice-ref="tx" pointcut="execution(* com.hao.service.impl.*.*(..))"/>
</aop:config>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
<property name="user" value="root"/>
<property name="password" value="hao20001010"/>
</bean>
<!-- 将模板对象注入到spring容器-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
将金额修改为5000元
再次运行:

===================================================
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
1.name:切入点方法名称
2.isolation:事务的隔离级别
3.propogation:事务的传播行为
4.timeout:超时时间
5.read-only:是否只读
Spring的事务控制-基于xml方式的更多相关文章
- Spring的事务控制-基于注解的方式
模拟转账操作,即Jone减少500,tom增加500 如果有疑问请访问spring事务控制-基于xml方式 1.创建数据表 2.创建Account实体类 public class Account { ...
- Spring声明式事务管理(基于XML方式实现)
--------------------siwuxie095 Spring 声明式事务管理(基于 XML 方式实现) 以转账为例 ...
- spring的事务控制
1.事务介绍 (1)特性:ACID Atomicity(原子性):事务中的所有操作要么全做要么全不做 Consistency(一致性):事务执行的结果使得数据库从一个一致性状态转移到另一个一致性状态 ...
- 13 Spring 的事务控制
1.事务的概念 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必 ...
- 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...
- SpringMVC入门(基于XML方式实现)
----------------------siwuxie095 SpringMVC 入门(基于 XML 方式实现) (一)搭建 SpringMVC 环境 1.先下载相关库文件,下载链接: (1)ht ...
- Spring-注入方式(基于xml方式)
1.基于xml方式创建对象 <!--配置User类对象的创建 --> <bean id="user" class="com.at.spring5.Use ...
- spring与hibernate注解及XML方式集成
spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ...
- Spring 的 Bean 管理(XML 方式)
Spring 的 Bean 管理(XML 方式) 1. 三种实例化 Bean 的方式 使用类构造器实例化(默认无参数) 使用静态工厂方法实例化(简单工厂模式) 使用实例工厂方法实例化(工厂方法模式) ...
随机推荐
- Flink消费kafka
Flink消费Kafka https://blog.csdn.net/boling_cavalry/article/details/85549434 https://www.cnblogs.com/s ...
- 获取ajax动态加载的多个a标签中的 点击的那个a标签对应的值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C++设计模式 - 组合模式(Composite)
数据结构模式 常常有一-些组件在内部具有特定的数据结构,如果让客户程序依赖这些特定的数据结构,将极大地破坏组件的复用.这时候,将这些特定数据结构封装在内部,在外部提供统一的接口,来实现与特定数据结构无 ...
- JavaWeb 11_文件上传
一.操作步骤 1.要有一个form标签,method=post 请求2.form标签的encType属性值必须为multipart/form-data值3.在form标签中使用input type=f ...
- 给R语言RStudio添加阿里云镜像源
镜像下载.域名解析.时间同步请点击阿里云开源镜像站 方法一: 打开RStudio,输入options()$repos查看默认镜像源情况 options()$repos 打开tools工具栏,找到Glo ...
- DataStage中Transformer的函数大全
一. 类型转换函数 类型转换函数用于更改参数的类型. 以下函数位于表达式编辑器的"类型转换"类别中.方括号表示参数是可选的.缺省日期格式为 %yyyy-%mm-%dd. 以下示例按 ...
- CVE-2010-2861(Adobe ColdFusion 文件读取漏洞)
漏洞介绍 Adobe ColdFusion是美国Adobe公司的一款动态Web服务器产品,其运行的CFML(ColdFusion Markup Language)是针对Web应用的一种程序设计语言. ...
- Oracle问题解决记录
一.前言 oracle这么一个庞大的东西,出点问题真是太常见了.开个博客,用于记录遇到的问题吧. 持续更新. 二.问题列表 归档日志满,引起的问题. 一台服务器,用了很久了,某天,出现了磁盘空间占满的 ...
- 使用 rabbitmq 的场景?
1.服务间异步通信 2.顺序消费 3.定时任务 4.请求削峰
- 基本类型数组转List
基本类型数组转List 小数 double[] src = {1.1,2.1,3.1}; List<Double> list = Arrays.stream( src ).boxed(). ...