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描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. ...
随机推荐
- Yours 的博客开张啦!
虽然申请博客已经1个月了,但是一直没有来写,没办法,题都刷不完,哪有心思写啊``` 现在集训终于完了,有了属于自己的时间了.所以该把以前做的题,学的算法好好的整理整理了.一来顺顺思路,二来也可以给后来 ...
- new jQuery.common
var $c=null;jQuery(function(){$c=new jQuery.common(); $c.methods.init(); }); (function($) { $.common ...
- HTML5给我们带来了什么?
HTML5初探 传说中的HTML标准已经超过10年没有更新了,如今HTML5席卷全球,那么到底什么是HTML5呢?都在讲HTML5是web的新一代标准,它有着很多之前浏览器没有的新特性,可以说HTML ...
- Codeforces Round #230 (Div. 2) C Blocked Points
题目链接 题意 : 给你一个半径为n的圆,圆里边还有圆上都有很多整点,让你找出与圆外的任意一个整点距离等于1的点. 思路 :这个题可以用枚举,画个图就发现了,比如说先数第一象限的,往下往右找,还可以找 ...
- 编码问题 关于hibernate jdbc数据库连接在xml配置与在properties文件配置的差异
在properties中,&字符不需要转义,因此在连接数据库的时候使用编码的地方直接使用&即可: driverClass=com.mysql.jdbc.Driver jdbcUrl=j ...
- Error -27791: Server xx has shut down the connection prematurely
最近在进行一次性能测试中遇到一个问题,并发较大的时候会出现LR出现Error -27791: Server xx has shut down the connection prematurely的ER ...
- [线段树]HDOJ5091 Beam Cannon
题意:给n, w, h (1 <= N <= 10000,1 <= W <= 40000,1 <= H <= 40000) $w\times h$是可以射到的范围 ...
- Java 数组倒序
String s[] = {,,,,,,}; ; i < s.length; i ++){ System.-i] +","); }
- linux PCI设备初始化过程
linux PCI设备初始化过程 start_kernel->rest_init 这个函数会启动一个核心线程0, 核心线程然后调用init -> do_basic_setup. 然后我们开 ...
- 【Linux】理解setuid()、setgid()和sticky位
详见: http://blog.csdn.net/m13666368773/article/details/7615125 Linux SETUID机制 (1)进程运行时能够访问哪些资源或文件,不取决 ...