spring tranaction 事务入门
一、事务四个属性
二、事务的重要性
打个最简单的比方吧,A和B两人之间的支付宝转账问题,A向B转账50RMB,正常的结果是,A - 50 并且 B + 50; 但如果是下面这种情况,那就杯具了,A - 50 成功,而B + 50 失败。这样一来岂不是 A亏大了!谁还敢随意转账?就算是首富,也不敢随意转账O(∩_∩)O哈!
所以,在进行 A - 50 和 B + 50 需要添加事务管理。
三、先看下没有加事务的Demo, 看完就知道事务的重要性啦~
(1)、整体结构、
(2)、jar 包
(3)、SQL语句
创建数据库 create database spring; 建立表 create table countmoney(idCard int primary key auto_increment,name varchar(),money int); 插入两条记录 insert into countmoney(name,money)values('xx',);
insert into countmoney(name,money)values('++',); 结果 select * from countmoney;
+--------+------+-------+
| idCard | name | money |
+--------+------+-------+
| 1 | xx | 300 |
| 2 | ++ | 300 |
+--------+------+-------+
(4)、代码
model 层
package com.xpw.model; public class Count {
private int idCard;
private String name;
private int money; public Count(){ } public int getIdCard() {
return idCard;
}
public void setIdCard(int idCard) {
this.idCard = idCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
} }
dao 层
package com.xpw.dao; public interface TradeDao { public void outputMoney(int idCard, int money); public void inputMoney(int idCard, int money);
}
dao impl 层
package com.xpw.dao.impl; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource; import com.xpw.dao.TradeDao; public class TradeDaoImpl implements TradeDao { private NamedParameterJdbcTemplate namedParameterJdbcTemplate; public void setNamedParameterJdbcTemplate(
NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
} @Override
public void outputMoney(int idCard, int count) {
String sql = "update trademoney set money = money -:count where idCard = :idCard";
MapSqlParameterSource param = new MapSqlParameterSource();
param.addValue("count", count);
param.addValue("idCard", idCard);
this.namedParameterJdbcTemplate.update(sql, param);
} @Override
public void inputMoney(int idCard, int count) {
//我们故意在此出错,抛出异常,让 B + 50失败
System.out.println(1/0);
String sql = "update trademoney set money = money + :count where idCard = :idCard";
MapSqlParameterSource param = new MapSqlParameterSource();
param.addValue("count", count);
param.addValue("idCard", idCard);
this.namedParameterJdbcTemplate.update(sql, param);
}
}
Service层
package com.xpw.service; public interface TradeService {
public void trade(int fromIdCard, int toIdCard, int money);
}
Service impl 层
package com.xpw.service.impl; import com.xpw.dao.TradeDao;
import com.xpw.service.TradeService; public class TradeServiceImpl implements TradeService { private TradeDao tradeDao; public void setTradeDao(TradeDao tradeDao) {
this.tradeDao = tradeDao;
} @Override
public void trade(int fromIdCard, int toIdCard, int money) {
this.tradeDao.outputMoney(fromIdCard, money);
this.tradeDao.inputMoney(toIdCard, money);
}
}
(5)文件配置信息
beans.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:context="http://www.springframework.org/schema/context"
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"> <context:component-scan base-package="org.springframework.docs.test" />
<context:property-placeholder location="jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean> <bean id="tradeDao" class="com.xpw.dao.impl.TradeDaoImpl">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"/>
</bean> <bean id="tradeService" class="com.xpw.service.impl.TradeServiceImpl">
<property name="tradeDao" ref="tradeDao"></property>
</bean>
</beans>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root
(6)测试
package com.xpw.trade; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xpw.service.TradeService; public class TradeTest { private static ApplicationContext ac; @Before
public void init(){
ac = new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void testTrade(){
TradeService ts = (TradeService) ac.getBean("tradeService");
int fromIdCard = 1;
int toIdCard = 2;
int money = 50;
ts.trade(fromIdCard, toIdCard, money);
}
}
结果 select * from trademoney; +--------+------+-------+
| idCard | name | money |
+--------+------+-------+
| 1 | xx | 250 |
| 2 | ++ | 300 |
+--------+------+-------+
2 rows in set (0.00 sec) 由于,在 inputmoney()方法,我们故意 做1/0操作,也没有做try catch ,导致不会往下执行向B账户添加50的业务,所以 A亏了50。。。 从上面的结果我们知道了事务的重要性了吧。。A - 50 和 B + 50 必须同时成功,才可以称为一个成功的交易,一旦 谁出错,就必须回滚!即 不能 将 A - 50 , B 也不能 被 + 50
下面,我们就 添加事务管理吧。。当然,事务管理有两种,详情见如下
四、spring 事务分类
1、编程式事务管理
Spring 提供的事务模版类:org.springframework.transaction.support.TransactionTemplate
事务管理器:org.springframework.jdbc.datasource.DataSourceTransactionManager
service impl 层的代码有所改动(注意,便于阅者copy实践,我就把整个类的代码贴出来,下同)
package com.xpw.service.impl; import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate; import com.xpw.dao.TradeDao;
import com.xpw.service.TradeService; public class TradeServiceImpl implements TradeService { private TradeDao tradeDao;
private TransactionTemplate transactionTemplate; public void setTradeDao(TradeDao tradeDao) {
this.tradeDao = tradeDao;
} public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
} //编程事务管理
@Override
public void trade(final int fromIdCard, final int toIdCard, final int money) {
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
tradeDao.outputMoney(fromIdCard, money);
tradeDao.inputMoney(toIdCard, money);
}
});
}
}
beans.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:context="http://www.springframework.org/schema/context"
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"> <context:component-scan base-package="org.springframework.docs.test" />
<context:property-placeholder location="jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
</bean> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean> <bean id="tradeDao" class="com.xpw.dao.impl.TradeDaoImpl">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"/>
</bean> <bean id="tradeService" class="com.xpw.service.impl.TradeServiceImpl">
<property name="tradeDao" ref="tradeDao"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
</beans>
其它的代码都没有变
结果:
mysql> select * from trademoney;
+--------+------+-------+
| idCard | name | money |
+--------+------+-------+
| 1 | xx | 300 |
| 2 | ++ | 300 |
+--------+------+-------+
2 rows in set (0.00 sec) 从上面的结果可以知道,编程式事务管理已经成功了,在 B + 50 失败了,回回滚,所以 A 不会 - 50
2、声明式事务管理
使用annotation
service impl 层
package com.xpw.service.impl; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionTemplate; import com.xpw.dao.TradeDao;
import com.xpw.service.TradeService; @Transactional
public class TradeServiceImpl implements TradeService { private TradeDao tradeDao;
private TransactionTemplate transactionTemplate; public void setTradeDao(TradeDao tradeDao) {
this.tradeDao = tradeDao;
} public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
} @Override
public void trade(int fromIdCard, int toIdCard, int money) {
this.tradeDao.outputMoney(fromIdCard, money);
this.tradeDao.inputMoney(toIdCard, money);
} }
beans.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="org.springframework.docs.test" />
<context:property-placeholder location="jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean> <bean id="tradeDao" class="com.xpw.dao.impl.TradeDaoImpl">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"/>
</bean> <bean id="tradeService" class="com.xpw.service.impl.TradeServiceImpl">
<property name="tradeDao" ref="tradeDao"></property>
</bean>
</beans>
结果:
+--------+------+-------+
| idCard | name | money |
+--------+------+-------+
| 1 | xx | 300 |
| 2 | ++ | 300 |
+--------+------+-------+
2 rows in set (0.00 sec) 此方式成功 添加了事务管理
使用xml 方式
service impl 层
package com.xpw.service.impl; import com.xpw.dao.TradeDao;
import com.xpw.service.TradeService; public class TradeServiceImpl implements TradeService { private TradeDao tradeDao; public void setTradeDao(TradeDao tradeDao) {
this.tradeDao = tradeDao;
} @Override
public void trade(int fromIdCard, int toIdCard, int money) {
this.tradeDao.outputMoney(fromIdCard, money);
this.tradeDao.inputMoney(toIdCard, money);
}
}
beans.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="org.springframework.docs.test" />
<context:property-placeholder location="jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 事务通知器 -->
<tx:advice>
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 事务切面 -->
<aop:config>
<!-- 事务切点 -->
<aop:pointcut expression="execution(* com.xpw.service.*.*(..))" id="transactionPointcut"/>
<aop:advisor advice-ref="transactionPointcut"/>
</aop:config> <bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean> <bean id="tradeDao" class="com.xpw.dao.impl.TradeDaoImpl">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate" />
</bean> <bean id="tradeService" class="com.xpw.service.impl.TradeServiceImpl">
<property name="tradeDao" ref="tradeDao"></property>
</bean>
</beans>
结果 mysql> select * from trademoney;
+--------+------+-------+
| idCard | name | money |
+--------+------+-------+
| 1 | xx | 300 |
| 2 | ++ | 300 |
+--------+------+-------+
2 rows in set (0.00 sec)
五、总结
事务管理有编程式、声明式,本人推荐后者。因为前者,虽然实现了事务管理,但在一定程度上,非业务逻辑代码浸入了我们的业务逻辑代码,如果系统大型的话,也不可避免重复操作,代码看起来也不整洁了,也不方便后期维护。
【tip】转载请注明原文来自 :http://www.cnblogs.com/chenmo-xpw/p/3949264.html
spring tranaction 事务入门的更多相关文章
- 【Spring Framework】Spring入门教程(八)Spring的事务管理
事务是什么? 事务:指单个逻辑操作单元的集合. 在操作数据库时(增删改),如果同时操作多次数据,我们从业务希望,要么全部成功,要么全部失败.这种情况称为事务处理. 例如:A转账给B. 第一步,扣除A君 ...
- Spring Boot事务管理(下)
在上两篇 Spring Boot事务管理(上)和Spring Boot事务管理(中)的基础上介绍注解@Transactional. 5 @Transactional属性 属性 类型 描述 value ...
- Spring AOP初级——入门及简单应用
在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是 ...
- spring对数据库的操作、spring中事务管理的介绍与操作
jdbcTemplate的入门 创建maven工程 此处省略 导入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/s ...
- Spring Cloud 从入门到精通
Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量. 本 ...
- Spring MVC【入门】
Spring MVC[入门]就这一篇! MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Mod ...
- Spring Transaction 使用入门 (转)
Spring Transaction 使用入门 一.开篇陈述 1.1 写文缘由 最近在系统学习spring框架IoC.AOP.Transaction相关的知识点,准备写三篇随笔记录学习过程中的感悟.这 ...
- 快速开发架构Spring Boot 从入门到精通 附源码
导读 篇幅较长,干货十足,阅读需花费点时间.珍惜原创,转载请注明出处,谢谢! Spring Boot基础 Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计 ...
- Spring Boot从入门到精通(九)整合Spring Data JPA应用框架
JPA是什么? JPA全称Java Persistence API,是Sun官方提出的Java持久化规范.是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. ...
随机推荐
- bzoj 4127: Abs 树链剖分
4127: Abs Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 11 Solved: 5[Submit][Status][Discuss] Des ...
- PHP漏洞全解(五)-SQL注入攻击
本文主要介绍针对PHP网站的SQL注入攻击.所谓的SQL注入攻击,即一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据程序返 ...
- hdu 4815 Little Tiger vs. Deep Monkey
概率dp,有点像背包的做法: dp[i][j]代表前i个数组成的j数的概率为多少 #include<cstdio> #include<cstring> #define maxn ...
- UIWebView 与 JS 交互(1):Objective-C 调用 Javascript
众所周知,随着硬件水平的发展,HTML5 与原生 APP 性能差距不断缩小,正在互联网科技领域扮演者越来越重要的角色.作为一种能很大程度上节约成本的技术方案,通过 HTML5 及 JS 实现的跨平台技 ...
- [jobdu]二叉树的镜像
树的镜像,这里的做法就是先序遍历的反过来呗. #include <iostream> #include <vector> using namespace std; void p ...
- Linux下的绘图(流程图、UML、mindmap)工具
http://blog.csdn.net/piyajee/article/details/5902380
- Android 风格化的 Toggle Buttons
Android到默认UI比iOS到默认UI在美观程度上还是有一定到差距的,我们希望能够美化UI,并且替换掉系统默认的UI风格,使得程序在使用这些UI的时候都默认使用我们自定义到UI.本文以Toggle ...
- POJ_3061_Subsequence_(尺取法)
描述 http://poj.org/problem?id=3061 给定长度为n的数列整数以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. Subsequence Time ...
- Linux Kernel‘ieee80211_radiotap_iterator_init()’函数拒绝服务漏洞
漏洞名称: Linux Kernel‘ieee80211_radiotap_iterator_init()’函数拒绝服务漏洞 CNNVD编号: CNNVD-201312-041 发布时间: 2013- ...
- 《深度探索C++对象模型》学习笔记
1.转型其实是一种编译器指令, 大部分情况下它并不改变一个指针所含的真正地址,它只影响"被指出之内存的大小和内容"的解释方式. 2.Global objects的内存保证会在程序启 ...