spring tranaction 事务入门

一、事务四个属性

原子性(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)、整体结构、

spring tranaction 事务入门

(2)、jar 包

spring tranaction 事务入门

(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

上一篇:Logstash 介绍


下一篇:Java中的反射和注解