事物概念
完成一件事物需要执行多个操作,那么在之前操作进行后,后面的操作可能会产生异常,此时就需要一个事物管理的方法保证数据安全。
实例概述
本实例模拟一个买书的过程,在交易过程中如果发生问题,那么事物就会回滚以保证数据安全。
环境配置
- spring4所需jar [http://pan.baidu.com/s/1pJmgxlh]
- 完成 基本方法的编码
- 配置spring4事物管理方法
- 测试
源码
- BooksShopDao
package com.spring.xml.tx;
public interface BooksShopDao {
//1-根据书号获取单价
public double findBookPriceByIsbn(int isbn);
//2-更新书的库存使之 -1
public void updateBookStock(int isbn);
//3-更新用户余额
public void updateUserAccount(int userNo,double price);
}
-BookShopDaoImpl
package com.spring.xml.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
public class BookShopDaoImpl implements BooksShopDao{
private JdbcTemplate jdbcTemplate=null;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public double findBookPriceByIsbn(int isbn) {
String sql="select price from t_book where isbn=?";
double price= jdbcTemplate.queryForObject(sql,Double.class,isbn);
return price;
}
@Override
public void updateBookStock(int isbn) {
/*
* 检查库存是否足够
* */
String sql2="SELECT stock from t_book_stock where isbn=? ";
int stock=jdbcTemplate.queryForObject(sql2, Integer.class,isbn);
if(stock==0){
throw new BookShopException("库存不足");
}
String sql="update t_book_stock set stock=stock-1 where isbn=?";
jdbcTemplate.update(sql,isbn);
}
@Override
public void updateUserAccount(int userNo, double price) {
String sql="update t_account set balance=balance-? where user_NO=?";
jdbcTemplate.update(sql,price,userNo);
/*
* 检查余额
* */
String sql2="SELECT balance from t_account where user_no=? ";
double balance=jdbcTemplate.queryForObject(sql2, Double.class,userNo);
if(balance<0){
throw new BookShopException("余额不足");
}
}
}
- BookShopService
package com.spring.xml.tx;
public interface BookShopService {
public void purchase(int isdn,int userNo);
}
- BookShopServiceImpl
package com.spring.xml.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.mchange.v2.log.FallbackMLog;
public class BookShopServiceImpl implements BookShopService{
private BooksShopDao bookShopDao=null;
public void setBookShopDao(BooksShopDao booksShopDao) {
this.bookShopDao = booksShopDao;
}
//添加事物注解
//1-指定事物的传播行为,即当前的事物被另外的事物方法调用时
// 如何使用事物 ,默认 -REQUIRED 使用另外事物的事物控制
// REQUIRES_NEW 使用自己的事物
/*2-
* 默认情况下 spring的声明事物对所有的运行时异常进行回滚
* 也可以通过对应的属性进行设置 通过设置 noRollbackFor可以设置那些不回滚
* 通常情况下 取默认值即可
* 3-使用readonly 指定事物是否为只读 表示这个事物只读取但不更新数据
* 这样可以帮助数据库引擎优化事物 ,如果一个事物真的只有能读取数据库的操作,那么应该设置readOnly =true
*
* 4- 使用timeOut 指定事物强制回滚之前最常占用的时间
* */
@Override
public void purchase(int isdn, int userNo) {
/*
*
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
* */
// 1- 获取单价
double price= bookShopDao.findBookPriceByIsbn(isdn);
//2-更新库存
bookShopDao.updateBookStock(isdn);
//3-更新余额
bookShopDao.updateUserAccount(userNo, price);
}
}
- Cashier
package com.spring.xml.tx;
import java.util.List;
public interface Cashier {
public void checkOut(int userNo,List<Integer> isdn);
}
- CashierImpl
package com.spring.xml.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;
public class CashierImpl implements Cashier {
private BookShopService bookShopService;
public void setBookShopService(BookShopService bookShopService) {
this.bookShopService = bookShopService;
}
//指定事物的传播行为,即当前的事物被另外的事物方法调用时
// 如何使用事物 ,默认 -REQUIRED 使用另外事物的事物控制
// REQUIRES_NEW 使用自己的事物
public void checkOut(int userNo, List<Integer> isdn) {
for(int isdn1:isdn){
bookShopService.purchase(isdn1, userNo);
}
}
}
- BookShopException
package com.spring.xml.tx;
public class BookShopException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public BookShopException() {
super();
// TODO Auto-generated constructor stub
}
public BookShopException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public BookShopException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public BookShopException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public BookShopException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
-JunitTest
package com.spring.xml.tx;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mysql.fabric.xmlrpc.base.Array;
public class JunitTest {
private ApplicationContext ctx=null;
private BooksShopDao booksShopDao=null;
private BookShopService bookShopService=null;
private Cashier cashier=null;
@Before
public void setUp() throws Exception {
ctx=new ClassPathXmlApplicationContext("bean-xml-tx.xml");
booksShopDao= ctx.getBean(BooksShopDao.class);
bookShopService=(BookShopService) ctx.getBean("bookShopService");
cashier=(Cashier) ctx.getBean("cashier");
}
@After
public void tearDown() throws Exception {
}
@Test
public void testThransactionPropagation(){
cashier.checkOut(1001, Arrays.asList(1001,1002));
}
@Test
public void testBookShopService(){
bookShopService.purchase(1001, 1001);
}
@Test
public void testBookShopDaoGetPriceByIsdn() {
double price=booksShopDao.findBookPriceByIsbn(1002);
System.out.println(price);
}
//更新书店存量,存量-1
@Test
public void testBookShopDaoUpdateBookStock() {
booksShopDao.updateBookStock(1002);
}
//更新余额,
@Test
public void testBookShopDaoUpdateUserBalance() {
booksShopDao.updateUserAccount(1001, 100);
}
}
- db.properties
jdbc.user=root
jdbc.password=admin
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///springJdbc
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
- bean-jdbc.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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 导入资源文件 com/spring/jdbc/db -->
<context:property-placeholder location="com/spring/jdbc/db.properties" />
<!-- 配置 c3p0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置spring 的jdbctemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 自动扫描 -->
<context:component-scan base-package="com.spring"></context:component-scan>
<!-- 具名参数 -->
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用事物注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Notes
- 本例使用的是注解的方式配置事务管理 也可以通过配置文件的方式来实现
附录
通过配置文件来配置事物管理
bean-xml-tx.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:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 导入资源文件 com/spring/jdbc/db -->
<context:property-placeholder location="com/spring/jdbc/db.properties" />
<!-- 自动扫描 -->
<context:component-scan base-package="com.spring"></context:component-scan>
<!-- 配置 c3p0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置spring 的jdbctemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置Bean -->
<bean id="bookShopDao" class="com.spring.xml.tx.BookShopDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property></bean>
<bean id="bookShopService" class="com.spring.xml.tx.BookShopServiceImpl">
<property name="bookShopDao" ref="bookShopDao"></property></bean>
<bean id="cashier" class="com.spring.xml.tx.CashierImpl">
<property name="bookShopService" ref="bookShopService"></property></bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property></bean>
<!-- 配置事物属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="purchase" propagation="REQUIRED"/>
<tx:method name="get*" read-only="false"/>
<tx:method name="find*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 配置事物切点 ,以及把书屋切入点和事物属性关联起来-->
<aop:config>
<aop:pointcut expression="execution(* com.spring.xml.tx.BookShopService.*(..))"
id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>