spring的事务管理,本文的例子是:比如你需要网购一本书,卖书的那一方有库存量以及书的价格,你有账户余额。回想我们在编程中要实现买书这样的功能,由于你的账户表和书的库存量表肯定不是同一张数据库表,所以必定会有一个先后,要么先将账户余额扣除书的价格,紧接着将书的库存量减一,要么反过来。那么问题来了,假如我们先将你的账户余额减掉,然后发现书的库存不足,这时怎么办呢,这就需要事务了,当我们发现书的库存不足时就要回滚事务,将你的余额返回去。只要配置了事务,发生了异常,就回滚。这就是事务的回滚。注:新人理解,如有错误,望指正,谢谢。

配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 导入资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="spring"></context:component-scan> <!-- 配置spring 的JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/> </beans>

applicationContext.xml

配置文件jdbc.propertices:

user=root
password=123
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\:///spring?encoding\=UFT-8
initPoolSize=5
maxPoolSize=20

BookShopDao.java:

package spring.tx;

public interface BookShopDao {

    public int findBookPriceByIsbn(String isbn);
public void updataBookStock(String isbn);
public void updataUserAccount(String username,int price);
}

BookShopDaoImp.java:

 package spring.tx;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; @Repository("bookShopDao")
public class BookShopDaoImp implements BookShopDao { @Autowired
private JdbcTemplate jdbcTemplate; @Override
public int findBookPriceByIsbn(String isbn) {
String sql = "select price from book where isbn=?";
return jdbcTemplate.queryForObject(sql, Integer.class, isbn);
} @Override
public void updataBookStock(String isbn) {
// 检查书的库存是否不够
String sql1 = "select stock from book_stock where isbn=?";
if (jdbcTemplate.queryForObject(sql1, Integer.class, isbn) <= 0) {
throw new BookStockException("库存不足");
}
String sql = "update book_stock set stock=stock-1 where isbn=?";
jdbcTemplate.update(sql, isbn);
} @Override
public void updataUserAccount(String username, int price) {
// 检查余额是否足够
String sql1 = "select balance from account where username=?";
if (jdbcTemplate.queryForObject(sql1, Integer.class, username) < price) {
throw new UserAccountException("余额不足");
}
String sql = "update account set balance = balance - ? where username=?";
jdbcTemplate.update(sql, price, username);
} }

BookShopDaoImp

BookShopService.java:

package spring.tx;

public interface BookShopService {
public void purchase(String username,String isbn);
}

BookShopServiceImpl.java:

 package spring.tx;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; @Repository("bookShopDao")
public class BookShopDaoImp implements BookShopDao { @Autowired
private JdbcTemplate jdbcTemplate; @Override
public int findBookPriceByIsbn(String isbn) {
String sql = "select price from book where isbn=?";
return jdbcTemplate.queryForObject(sql, Integer.class, isbn);
} @Override
public void updataBookStock(String isbn) {
// 检查书的库存是否不够
String sql1 = "select stock from book_stock where isbn=?";
if (jdbcTemplate.queryForObject(sql1, Integer.class, isbn) <= 0) {
throw new BookStockException("库存不足");
}
String sql = "update book_stock set stock=stock-1 where isbn=?";
jdbcTemplate.update(sql, isbn);
} @Override
public void updataUserAccount(String username, int price) {
// 检查余额是否足够
String sql1 = "select balance from account where username=?";
if (jdbcTemplate.queryForObject(sql1, Integer.class, username) < price) {
throw new UserAccountException("余额不足");
}
String sql = "update account set balance = balance - ? where username=?";
jdbcTemplate.update(sql, price, username);
} }

BookShopServiceImpl.java

Cashier.java:

package spring.tx;

import java.util.List;

public interface Cashier {
public void checkout(String username,List<String> isbns);
}

CashierImpl.java:

 package spring.tx;

 import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; @Service("cashier")
public class CashierImpl implements Cashier { @Autowired
private BookShopService bookShopService; @Transactional
@Override
public void checkout(String username, List<String> isbns) {
for (int i = 0; i < isbns.size(); i++) {
bookShopService.purchase(username, isbns.get(i));
}
} }

CashierImpl

定义两个异常类:

 package spring.tx;

 public class BookStockException extends RuntimeException{

     /**
*
*/
private static final long serialVersionUID = 1L; public BookStockException() {
super();
// TODO Auto-generated constructor stub
} public BookStockException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
} public BookStockException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public BookStockException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public BookStockException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} }

BookStockException

package spring.tx;

public class UrAccountExceptionse extends RuntimeException {

    /**
*
*/
private static final long serialVersionUID = 1L; public UserAccountException() {
super();
// TODO Auto-generated constructor stub
} public UserAccountException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
} public UserAccountException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public UserAccountException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public UserAccountException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} }

UserAccountExceptionse

测试方法:

package spring.tx.test;

import java.util.ArrayList;
import java.util.List; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.tx.BookShopDao;
import spring.tx.BookShopService;
import spring.tx.Cashier; public class SpringTransactionTest { private ApplicationContext ctx=null;
private BookShopDao bookShopDao;
private BookShopService bookShopService;
private Cashier cashier;
{
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
bookShopDao =(BookShopDao) ctx.getBean("bookShopDao");
bookShopService=(BookShopService) ctx.getBean("bookShopService");
cashier =(Cashier) ctx.getBean("cashier");
} @Test
public void testTransactionPropagation(){
List<String > isbns=new ArrayList<String>();
isbns.add("1001");
isbns.add("1002");
cashier.checkout("aaa", isbns);
} @Test
public void testBookShopService() {
bookShopService.purchase("aaa", "1001");
} @Test
public void testupdataBookStock(){
bookShopDao.updataBookStock("1001");
} @Test
public void testUpdataUserAccount(){
bookShopDao.updataUserAccount("aaa", 200);
} @Test
public void testBookShopDao(){
int price=bookShopDao.findBookPriceByIsbn("1001");
System.out.println(price); }
}

最新文章

  1. nginx之location匹配优先级和安全问题
  2. bin/sh^M: bad interpreter: No such file or directory解决
  3. 中控考勤仪IFace302多线程操作时无法订阅事件
  4. 计时函数 clock() in c and c++
  5. 每天一个linux命令(20):linux chmod命令
  6. php get set方法深入理解
  7. ipython and bpython
  8. 微信连WiFi关注公众号流程更新 解决ios微信扫描二维码不关注就能上网的问题
  9. 【随记】VS异常:HRESULT: 0x80070057 (E_INVALIDARG)) 解决方案
  10. 我对Stub和Mock的理解
  11. JavaScript中对事件简单的理解(2)
  12. 常用路径 URL 中的斜杠与反斜杠
  13. PHP能引起安全的函数
  14. 通过一个color创建一个image
  15. Polly 熔断策略
  16. odoo8 和odoo10区别
  17. JAVA核心技术I---JAVA基础知识(数据结构基础)
  18. 区间dp板子题:[noi1995]石子合并
  19. 03Hadoop的TopN的问题
  20. 与数论的厮守02:整数的因子分解—Pollard_Rho

热门文章

  1. CSU-1632 Repeated Substrings[后缀数组求重复出现的子串数目]
  2. hihoCoder 1549 或运算和
  3. 浅谈Spring框架注解的用法分析
  4. 阿里云 如何减少备份使用量? mysql数据库的完整备份、差异备份、增量备份
  5. junit5荟萃知识点(一):junit5的组成及安装
  6. 检测当前的语言环境是否使用了 UTF-8 编码(三篇文章:先用setlocale()设置编码,再用nl_langinfo()进行检测。locale对象可以使用langLocale.name() == &quot;zh_CN&quot;判断)
  7. 洛谷 P2523 [HAOI2011]Problem c
  8. MySQL中事务的分类
  9. 解释一下python的and-or语法
  10. 老铁,这年头不会点git真不行