注解的形式与xml文件的形式完成事务管理及xml文件的配置
需要的jar包:
c3p0-0.9.2.1.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
mchange-commons-java-0.2.3.4.jar
mysql-connector-java-5.0.8-bin.jar
spring-aop-4.2.2.RELEASE.jar
spring-beans-4.2.2.RELEASE.jar
spring-context-4.2.2.RELEASE.jar
spring-core-4.2.2.RELEASE.jar
spring-expression-4.2.2.RELEASE.jar
spring-jdbc-4.2.2.RELEASE.jar
spring-tx-4.2.2.RELEASE.jar
1.注解方式的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/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"> <!-- 包扫描 -->
<context:component-scan base-package="com.eduask.liusheng"/> <!-- 引入属性文件 -->
<context:property-placeholder location="db.properties"/> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!-- 配置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>
public interface BookShopDao {
/**
* 根据isbn查找单价
* @param isbn 编号
* @return
*/
public int findBookPriceByIsbn(String isbn);
/**
* 修改库存
* @param isbn 编号
*/
public void updateBookStock(String isbn);
/**
* 修改账户余额
* @param username 用户名
* @param price 价格
*/
public void updateAccount(String username,int price);
}
BookShopDao.java
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; public int findBookPriceByIsbn(String isbn) {
String sql="select price from book where isbn=?";
int price=jdbcTemplate.queryForObject(sql, Integer.class, isbn);
return price;
} public void updateBookStock(String isbn) {
String sql="select stock from book_stock where isbn=?";
int stock=jdbcTemplate.queryForObject(sql, Integer.class, isbn);
if (stock>0) {
sql="update book_stock set stock=stock-1 where isbn=?";
jdbcTemplate.update(sql, isbn);
} else {
throw new RuntimeException("库存不足!");
}
} @Override
public void updateAccount(String username, int price) {
String sql="select balance from account where username=?";
int balance=jdbcTemplate.queryForObject(sql, Integer.class, username);
if (balance>=price) {
sql="update account set balance=balance-? where username=?";
jdbcTemplate.update(sql, price,username);
} else {
throw new RuntimeException("余额不足!");
}
} }
BookShopDaoImp.java
public interface BookShopService {
/**
* 购买功能
* @param username 用户名
* @param isbn 书编号
*/
public void purchase(String username,String isbn);
}
BookShopService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import com.eduask.liusheng.dao.BookShopDao;
@Service("bookShopService")
public class BookShopServiceImp implements BookShopService {
@Autowired
private BookShopDao bookShopDao; @Transactional(propagation=Propagation.REQUIRES_NEW,timeout=3)
public void purchase(String username, String isbn) { //1.根据编号查询价格
int price=bookShopDao.findBookPriceByIsbn(isbn); //2.修改库存
bookShopDao.updateBookStock(isbn);
//测试超时
// try {
// Thread.sleep(8000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } //3.修改余额
bookShopDao.updateAccount(username, price); } }
BookShopServiceImp.java
import java.util.List;
public interface BookShopCashier {
/**
* 结账
* @param isbns
* @param username
*/
public void checkOut(List<String> isbns,String username);
}
BookShopCashier.java
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("bookShopCashier")
public class BookShopCashierImp implements BookShopCashier {
@Autowired
private BookShopService bookShopService; @Transactional
public void checkOut(List<String> isbns, String username){
for (String isbn : isbns) {
bookShopService.purchase(username, isbn);
}
} }
BookShopCashierImp.java
@Transactional(属性)
传播行为propagation 常用REQUIRES_NEW与REQUIRED
隔离事务isolation 默认READ_COMMITTED 拓展并发事务引起的问题,脏读.不可重复度,幻读
rollbackFor 和noRollbackFor 设置回滚与不回滚 默认RunTimeException自动回滚,checked的异常不回滚
timeout 设置超时时间,单位s
2.xml方式的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"> <bean id="bookShopDao" class="com.eduask.liusheng.dao.BookShopDaoImp" autowire="byName"/> <bean id="bookShopService" class="com.eduask.liusheng.service.BookShopServiceImp" autowire="byName"/> <bean id="bookShopCashier" class="com.eduask.liusheng.service.BookShopCashierImp" autowire="byName"/> <!-- 引入属性文件 -->
<context:property-placeholder location="db.properties"/> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!-- 配置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:advice id="advice" transaction-manager="transactionManager">
<!--
name 方法名
propagation 传播属性:REQUIRES_NEW,REQUIRED 事务被挂起的时间不计入超时时间
isolation 隔离事务级别 MySQL4种,Oracle2种
timeout 超时时间 :-1永不超时,单位s;防止长期运行的事务占用资源
no-rollback-for 不回滚 (异常名)
rollback-for 回滚
任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚
read-only 只读属性:表示这个事务只读取数据但不更新数据, 这样可以帮助数据库引擎优化事务
-->
<tx:attributes>
<tx:method name="purchase" propagation="REQUIRED" isolation="READ_COMMITTED" />
<tx:method name="checkOut" propagation="REQUIRED" isolation="READ_COMMITTED" timeout="3"/>
<!-- 增删改方法 -->
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<!-- 查询方法只读 -->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice> <!-- 配置切点 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.eduask.liusheng.service.*.*(..))" id="pointcut"/>
<!-- 关联切点与通知 -->
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config> </beans>
import org.springframework.jdbc.core.JdbcTemplate;
public class BookShopDaoImp implements BookShopDao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int findBookPriceByIsbn(String isbn) {
String sql="select price from book where isbn=?";
int price=jdbcTemplate.queryForObject(sql, Integer.class, isbn);
return price;
}
public void updateBookStock(String isbn) {
String sql="select stock from book_stock where isbn=?";
int stock=jdbcTemplate.queryForObject(sql, Integer.class, isbn);
if (stock>0) {
sql="update book_stock set stock=stock-1 where isbn=?";
jdbcTemplate.update(sql, isbn);
} else {
throw new RuntimeException("库存不足!");
}
}
public void updateAccount(String username, int price) {
String sql="select balance from account where username=?";
int balance=jdbcTemplate.queryForObject(sql, Integer.class, username);
if (balance>=price) {
sql="update account set balance=balance-? where username=?";
jdbcTemplate.update(sql, price,username);
} else {
throw new RuntimeException("余额不足!");
}
}
}
BookShopDaoImp.java
import com.eduask.liusheng.dao.BookShopDao;
public class BookShopServiceImp implements BookShopService {
private BookShopDao bookShopDao;
public BookShopDao getBookShopDao() {
return bookShopDao;
}
public void setBookShopDao(BookShopDao bookShopDao) {
this.bookShopDao = bookShopDao;
}
public void purchase(String username, String isbn) {
//1.根据编号查询价格
int price=bookShopDao.findBookPriceByIsbn(isbn);
//2.修改库存
bookShopDao.updateBookStock(isbn);
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//3.修改余额
bookShopDao.updateAccount(username, price);
}
}
BookShopServiceImp.java
import java.util.List;
public class BookShopCashierImp implements BookShopCashier {
private BookShopService bookShopService;
public BookShopService getBookShopService() {
return bookShopService;
}
public void setBookShopService(BookShopService bookShopService) {
this.bookShopService = bookShopService;
}
public void checkOut(List<String> isbns, String username){
for (String isbn : isbns) {
bookShopService.purchase(username, isbn);
}
}
}
BookShopCashierImp.java
注解的形式与xml文件的形式完成事务管理及xml文件的配置的更多相关文章
- 9.Spring整合Hibernate_2_声明式的事务管理(Xml的方式)
使用xml的方式进行声明式的事务管理 推荐使用xml的方式,因为可以同时为多个方法进行声明 <!-- 开启Spring中的事务管理(声明式的事务管理) xml--> <!-- 不管是 ...
- Spring整合hibernate:3、使用XML进行声明式的事务管理
配置applicationContext.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...
- 0045 Spring中使用DataSourceTransactionManager进行事务管理的xml配置
在一个业务的实现过程中,可能需要多条sql完成对数据库的操作,比如账户登录,需要匹配用户名和密码,然后要增加积分,还要记录登录的ip和时间,这可能需要三个sql语句,这三个语句应当是一个整体,任意一个 ...
- Spring中使用DataSourceTransactionManager进行事务管理的xml配置
在一个业务的实现过程中,可能需要多条sql完成对数据库的操作,比如账户登录,需要匹配用户名和密码,然后要增加积分,还要记录登录的ip和时间,这可能需要三个sql语句,这三个语句应当是一个整体,任意一个 ...
- spring事务管理(xml配置)与spring自带连接数据库JdbcTemplate
什么是事务,很通俗的话来说就是,我们日常生活中总会出现在银行转账的业务,加入A向B转账100元,此时A的账户中应该减少100元,B的账户中增加100元,但是如果在A转完账B还没有接受的时候,服务器出现 ...
- sping 对 hibernate进行事务管理--Annotation, xml, 大多数使用XML
1. UserServiceTest.java: package com.bjsxt.service; import org.junit.Test; import org.springframewor ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
- Spring事务管理的xml方式
一个业务的成功: 调用的service是执行成功的,意味着service中调用的所有的dao是执行成功的. 事务应该在Service层统一控制. 如果手动去实现,则需要对dao进行代理,在方法前后进 ...
- Hive记录-配置支持事务管理hive-site.xml
<property> <name>hive.support.concurrency</name> <value>true</value> & ...
随机推荐
- JDBC在javaweb中的应用之分页数据查询
分页查询 分页查询是java web开发中经常使用到的技术.在数据库中数据量非常大的情况下,不适合将所有的数据全部显示到一个页面中,同时为了节约程序以及数据库的资源,就需要对数据进行分页查询操作. 通 ...
- MonoDeveloper 快捷键
注:环境是Unity3D 5.0.2f1自带的MonoDevelop Ctrl+X 剪切功能.另外,光标放在一行的任意位置(不选中任何内容),使用快捷键,将把这一行剪切并删除此行,这个特性非常好用 C ...
- GCD之线程挂起与恢复
我们可以使用dispatch_suspend函数暂停一个queue以阻止它执行block对象;使用dispatch_resume函数继续dispatch queue.调用dispatch_suspen ...
- LINUX通过PXE自动部署系统
原理介绍 TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP 协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂.开销不大的 ...
- DotNetCore跨平台~linux上还原自主nuget包需要注意的问题
问题的产生的背景 由于我们使用了jenkins进行部署(jenkins~集群分发功能和职责处理),而对于.net core项目来说又是跨平台的,所以对它的项目拉取,包的还原,项目的编译和项目的发布都是 ...
- 代码与编程(java基础)
代码与编程(面试与笔试java) 1.写一个Singleton出来 Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 一般Singleton模式通常有几种种 ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- SQL server2005学习笔记(一)数据库的基本知识、基本操作(分离、脱机、收缩、备份、还原、附加)和基本语法
在软件测试中,数据库是必备知识,假期闲里偷忙,整理了一点学习笔记,共同探讨. 阅读目录 基本知识 数据库发展史 数据库名词 SQL组成 基本操作 登录数据库操作 数据库远程连接操作 数据库分离操作 数 ...
- 在Pycharm中使用jupyter笔记本
在Pycharm中使用jupyter笔记本 我在Pycharm中使用jupyter笔记本,发现新的Jupyter更新中,增加了令牌. 随着创建的虚拟环境启动的所有设置,并将URL设置为127.0.0. ...
- 802.1Q VLAN技术原理
文章出处:http://hi.baidu.com/x278384/item/d56b0edfd4f56a4eddf9be79 在数据通信和宽带接入设备里,只要涉及到二层技术的,就会遇到VLAN.而且, ...