一、事务四个属性

原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。
一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
持久性(durability)。持久性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。

二、事务的重要性

打个最简单的比方吧,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 事务入门的更多相关文章

  1. 【Spring Framework】Spring入门教程(八)Spring的事务管理

    事务是什么? 事务:指单个逻辑操作单元的集合. 在操作数据库时(增删改),如果同时操作多次数据,我们从业务希望,要么全部成功,要么全部失败.这种情况称为事务处理. 例如:A转账给B. 第一步,扣除A君 ...

  2. Spring Boot事务管理(下)

    在上两篇 Spring Boot事务管理(上)和Spring Boot事务管理(中)的基础上介绍注解@Transactional. 5 @Transactional属性 属性 类型 描述 value ...

  3. Spring AOP初级——入门及简单应用

      在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是 ...

  4. spring对数据库的操作、spring中事务管理的介绍与操作

    jdbcTemplate的入门 创建maven工程 此处省略 导入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/s ...

  5. Spring Cloud 从入门到精通

    Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量. 本 ...

  6. Spring MVC【入门】

    Spring MVC[入门]就这一篇! MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Mod ...

  7. Spring Transaction 使用入门 (转)

    Spring Transaction 使用入门 一.开篇陈述 1.1 写文缘由 最近在系统学习spring框架IoC.AOP.Transaction相关的知识点,准备写三篇随笔记录学习过程中的感悟.这 ...

  8. 快速开发架构Spring Boot 从入门到精通 附源码

    导读 篇幅较长,干货十足,阅读需花费点时间.珍惜原创,转载请注明出处,谢谢! Spring Boot基础 Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计 ...

  9. Spring Boot从入门到精通(九)整合Spring Data JPA应用框架

    JPA是什么? JPA全称Java Persistence API,是Sun官方提出的Java持久化规范.是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. ...

随机推荐

  1. [JavaScript] 怎么使用JS禁止复制粘贴

    1. 将彻底屏蔽鼠标右键,其实是禁止快捷菜单,因为不光右键可以弹出这个菜单,键盘上空格键右边的windows键也可以激活这个快捷菜单 <table border oncontextmenu=re ...

  2. PHP漏洞全解(一)-PHP网站的安全性问题

    本文主要介绍针对PHP网站常见的攻击方式,包括常见的sql注入,跨站等攻击类型.同时介绍了PHP的几个重要参数设置.后面的系列文章将站在攻击者的角度,为你揭开PHP安全问题,同时提供相应应对方案. 针 ...

  3. UNICODE并没有提供对诸如Braille, Cherokee, Ethiopic, Khmer, Mongolian, Hmong, Tai Lu, Tai Mau文字的支持

    UNICODE支持欧洲.非洲.中东.亚洲(包括统一标准的东亚象形汉字和韩国象形文字).但是,UNICODE并没有提供对诸如Braille, Cherokee, Ethiopic, Khmer, Mon ...

  4. 【原创】FPGA开发手记(二) VGA接口

    以下内容均以Xilinx的Nexys3作为开发板 1.VGA接口介绍 首先,先看电路图(3*5为例): 标准VGA一共15个接口,但是实际应用的接口信号只用五个:HSYNC,行同步信号:VSYNC,场 ...

  5. Visual Studio中一个解决方案设置多个启动项目

    在解决方案上右键,选择属性. 这样设置之后,点击开始运行之后,会同时启动2个项目. 适合一个项目既包含客户端也包含服务端,方便调试

  6. APP-FND-01706: Error Updating TABLE_NAME In FND_DOCUMENT_SEQUENCES (文档 ID 338026.1)

    In this Document Symptoms Cause Solution Applies to: Oracle Order Management - Version 11.5.10.0 and ...

  7. bzoj2259

    这道题很不错,首先读入方式有一种跳跃的既视感:读入Si之后,我们可以直接往后跳Si,可以想到最短路,设序列为a[],我们设n+1是终点如果i+a[i]<=n+1 那么i-->i+a[i] ...

  8. ASP.NET操作Word的IIS权限配置

    ASP.NET账号在默认情况下是没有权限操作Microsoft Office对象的,如果不进行权限的配置,代码会抛出类似以下的异常: 检索 COM 类工厂中 CLSID 为 {00024500-000 ...

  9. DOM(文本对象模型)简介

    DOM(文本对象模型)简介 在正式开始介绍jQuery处理XML前我们来了解一些必备的基础知识. DOM是HTML或者XML结构的一种展现形式,通过编程对DOM进行修改可以达到修改HTML/XML的目 ...

  10. LinkedHashMap的实现原理

    1. LinkedHashMap概述: LinkedHashMap是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映 ...