Spring学习笔记:声明式事务管理增删改查业务
一、关于是事务
以方法为单位,进行事务控制;抛出异常,事务回滚。
二、声明式事务:
三、案例:编程实现购买股票的业务
1.编写实体类
2.编写dao层接口和实现
3.编写service层接口和实现
public class StockServiceImpl implements IStockService{
private IStockDao stockDao;
private IAccountDao accountDao; public void setStockDao(IStockDao stockDao) {
this.stockDao = stockDao;
} public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
} @Override
public void buyStock(int sid, double scount, int aid, double abalance) {
boolean isBuy = true;
stockDao.updateStock(sid,scount,isBuy);
accountDao.updateAccount(aid,abalance,isBuy);
}
@Override
public void sellStock(int sid, double scount, int aid, double abalance) {
boolean isBuy = false;
stockDao.updateStock(sid,scount,isBuy);
accountDao.updateAccount(aid,abalance,isBuy);
}
}
4.编写applicationContext.xml引入架包
(1)没有加入事务的xml文件
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="database.properties"/>
</bean>
<!--配置jdbc数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--注册jdbcTemplate-->
<bean id="jbdcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注册dao实现-->
<bean id="accountDao" class="cn.bank.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jbdcTemplate"/>
</bean>
<bean id="stockDao" class="cn.bank.dao.impl.StockDaoImpl">
<property name="jdbcTemplate" ref="jbdcTemplate"/>
</bean>
<!--注册service实现-->
<bean id="stockService" class="cn.bank.service.impl.StockServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="stockDao" ref="stockDao"/>
</bean>
功能测试:
@Test
public void buyStock(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IStockService stockService = (IStockService) context.getBean("stockService");
stockService.buyStock(1001,1000,1204,10000);
}
(此处没有关联股票价格)
(2)加入异常测试
@Override
public void buyStock(int sid, double scount, int aid, double abalance) {
boolean isBuy = true;
stockDao.updateStock(sid,scount,isBuy);
if (1==1) {
int i = 5 / 0;
}
accountDao.updateAccount(aid,abalance,isBuy);
}
报错:
处理结果:
在执行一次:
现在我们可以看到,在增加股票和减少余额中间产生异常之后,股票增加而余额没有减少,这种现在肯定是错误的。
(3)添加事务管理
<?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">
<!--通过bean元素生命需要Spring创建的实例。该实例的类型通过class属性指定,
并通过id属性为该实例制定一个名称,以便于访问-->
<!--引入数据库配置文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="database.properties"/>
</bean>
<!--配置jdbc数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--注册jdbcTemplate-->
<bean id="jbdcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注册dao实现-->
<bean id="accountDao" class="cn.bank.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jbdcTemplate"/>
</bean>
<bean id="stockDao" class="cn.bank.dao.impl.StockDaoImpl">
<property name="jdbcTemplate" ref="jbdcTemplate"/>
</bean>
<!--注册service实现-->
<bean id="stockService" class="cn.bank.service.impl.StockServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="stockDao" ref="stockDao"/>
</bean>
<!--引入事务平台管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务拦截处理:使用代理工厂-->
<bean id="serviceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="stockService"/>
<property name="transactionManager" ref="transactionManager"/>
<!--事务参数的设定-->
<property name="transactionAttributes">
<props>
<prop key="buyStock">ISOLATION_DEFAULT,PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
测试(修改getBean()方法的参数):
@Test
public void buyStockTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IStockService stockService = (IStockService) context.getBean("serviceProxy");
stockService.buyStock(1001,1000,1204,10000);
}
异常发生,数据没有改变(是我们预期的结果,也是业务需要的):
最后,去掉异常,保证事务管理下业务功能正常:
四:业务实现正常
Spring学习笔记:声明式事务管理增删改查业务的更多相关文章
- Spring—SSJ集成&声明式事务管理
1. 课程介绍 1. SSJ集成;(掌握) 2. 声明式事务管理;(掌握) 什么是三大框架 2.1. ssh Struts/Struts2 Spring Hibernate 2.2. ss ...
- Spring学习之声明式事物管理
public List<Student> selectStudent() { Student s = new Student(); s.setName("zhengbin&quo ...
- spring+mybatis之声明式事务管理初识(小实例)
前几篇的文章都只是初步学习spring和mybatis框架,所写的实例也都非常简单,所进行的数据访问控制也都很简单,没有加入事务管理.这篇文章将初步接触事务管理. 1.事务管理 理解事务管理之前,先通 ...
- ASP.NET学习笔记(3)——用户增删改查(三层)
说明(2017-10-6 11:21:58): 1. 十一放假在家也没写几行代码,本来还想着利用假期把asp.net看完,结果天天喝酒睡觉,回去的票也没买到,惨.. 2. 断断续续的把用户信息的页面写 ...
- C学习笔记-结构体与二进制文件增删改查
使用结构体整理数据,然后利用二进制存储文件,这样存储的文件类似于数据库,可以实现文件的增删改查 定义结构体 struct student { unsigned int ID; char name[20 ...
- Android学习笔记_9_SQLiteOpenHelper对象之数据库增删改查以及事务回滚操作
一.SQLite数据库: 在Android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进 ...
- 【EF学习笔记04】----------EF简单增删改查
第一步:创建上下文对象 using(var db = new Entities()) { //数据操作 } 新增 UserInfo user = new UserInfo() { UserName = ...
- mongo学习笔记(一):增删改查
安装:我是按这篇来弄的 一.Insert 1.db.person.insert({"name":"jack","age":20}) 2.va ...
- ASP.NET学习笔记(2)——用户增删改查
说明(2017-7-4 11:48:50): 1. index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...
随机推荐
- 多线程DP
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- scrapy框架基于CrawlSpider的全站数据爬取
引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...
- jquery事件一 ---鼠标移入移出
比较一下几个jquery事件的区别 mouseover() 鼠标进入(进入子元素也触发) mouseout() 鼠标离开(离开子元素也触发) mouseenter() 鼠标进入(进入子元素不触发) m ...
- css中设置background属性
属性解释 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个设置项: backgrou ...
- 算法 PK 猫咪 | 章鱼保罗后继竟然是只猫?
简评:一只名叫阿喀琉斯(Achilles)的白猫一边小声叫着,一边慵懒地在分别插有俄罗斯和沙特阿拉伯国旗的食盆间踱步.这只看起来并不出众的小猫住在俄罗斯圣彼得堡埃尔米塔日博物馆(State Hermi ...
- CentOS 7 查看和设置防火墙状态
CentOS7 默认使用的是firewall作为防火墙 查看防火墙状态 firewall-cmd --state 停止firewall systemctl stop firewalld.service ...
- restful api上传文件(基础)-springboot
基于restful api格式的文件上传(只是上传到本地): package com.nxz.controller; import com.nxz.entity.FileInfo; import or ...
- 修改testng源码,添加beforeMethod和afterMethod中的日志到test中(可以不改源码,废弃)
在使用testng生成报告的时候,只会记录test方法中的日志,但是一般会在beforeMethod.beforeTest.afterMethod.afterTest中做一下数据的处理,这里面的日志没 ...
- 安装Termux的手机上运行Python
1. Termux 终端 Android是一个单用户图形化系统,功能主要以应用的形式呈现给用户,因此在系统上我们无法直接获取终端,更是无法直接调用系统自带的丰富指令.使用ADB是一个曲线救国的方法,打 ...
- BZOJ - 3263 三维偏序
题意:定义元素为有序组(a,b,c),若存在x组(a_i,b_i,c_i)分别小于等于(a,b,c),则该元素的等级为x,求[0,n-1]等级的个数 cdq分治练手题,对a简单排序并去重,对b进行分治 ...