注解的形式与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> & ...
随机推荐
- springmvc04-文件上传-JSON数据
文件上传部分: 1, 导入commons-fileupload-1.2.2.jar commons-io-2.4.jar 两个jar包. 2, 在主配置文件中,添加如下信息 <!-- 文件上传- ...
- 在eclipse上使用github,向github中提交项目
1.下载egit插件 打开Eclipse,git需要eclipse授权,通过网页是无法下载egit的安装包的.在菜单栏依次打开eclipse→help→install new software→add ...
- 【SQL】- 基础知识梳理(四) - 存储过程
存储过程的概念 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行 存储过程的好处 A. 存储过程允许标准组件式编程 ...
- New Features and changes of Ninject 3.3
Ninject 3.3 beta1 has gone live. This release mainly focus on bug fix and platform update. Support . ...
- 翻译 | 使用A-Frame打造WebVR版《我的世界》
原文地址:Minecraft in WebVR with HTML Using A-Frame 原文作者:Kevin Ngo 译者:Felix 校对:阿希 我是 Kevin Ngo,一名就职于 Moz ...
- jquery对象和js对象
<ul id="ul1"> <li id="li_1">01</li> <li>02</li> ...
- Find 找规律,递推
Find Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus P ...
- 移动HTNL5前端框架—MUI
前 言 JRedu 鉴于之前的很多前端框架(特别是响应式布局的框架),UI控件看起来太像网页,没有原生感觉,因此追求原生UI也是MUI的重要目标.MUI以iOS平台UI为基础,补充部分Andro ...
- input输入中文时,拼音在输入框内会触发input事件的问题。
问题描述: 监听文本输入框的input事件,在拼写汉字(输入法)但汉字并未实际填充到文本框中(选词)时会触发input事件,如图: 需要完成的需求就是在输入阶段不触发input中的事件,选词之后文字落 ...
- Google Authenticator 如何集成(U盾的实现原理相同)
Google Authenticator是一个类似U盾的二次验证工具,Google提供了它的开源客户端(https://github.com/google/google-authenticator)里 ...