spring框架 事务 注解配置方式
user=LF
password=LF
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
driverClass=oracle.jdbc.driver.OracleDriver initialPoolSize=15
maxPoolSize=30
minPoolSize=5
<?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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.zr.trasaction"></context:component-scan> <!-- 引用外部文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 配置dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="minPoolSize" value="${minPoolSize}"></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>
package com.zr.trasaction.entity; public class Amount { private String user;//用户名
private double money;//金额
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Amount() {
super();
}
public Amount(String user, double money) {
super();
this.user = user;
this.money = money;
}
@Override
public String toString() {
return "Amount [user=" + user + ", money=" + money + "]";
} }
package com.zr.trasaction.entity; public class BalanceException extends RuntimeException { private static final long serialVersionUID = 1L; public BalanceException() {
super();
} public BalanceException(String message) {
super(message);
} public BalanceException(String message, Throwable cause) {
super(message, cause);
} public BalanceException(Throwable cause) {
super(cause);
} protected BalanceException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
package com.zr.trasaction.dao; public interface AmountDao { /**
* 转出钱
* @return
*/
public boolean decreaseMoney(String username,double money) ; /**
* 转入钱
* @return
*/
public boolean increaseMoney(String username,double money); }
package com.zr.trasaction.daoImpl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; import com.zr.trasaction.dao.AmountDao; @Repository
public class AmountDaoImpl implements AmountDao {
@Autowired(required=true)
private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} @Override
public boolean decreaseMoney(String username, double money) { String sql = "UPDATE TEST SET MONEY=MONEY-? WHERE USERNAME=?";
int i = -1;
i = jdbcTemplate.update(sql,money, username);
if(i>0){
return true;
}
return false;
} @Override
public boolean increaseMoney(String username, double money) {
String sql = "UPDATE TEST SET MONEY=MONEY+? WHERE USERNAME=?";
int i = -1;
i = jdbcTemplate.update(sql,money, username);
if(i>0){
return true;
}
return false;
} }
package com.zr.trasaction.Service; import com.zr.trasaction.entity.Amount; public interface AmountService { /**
* 转账业务
* @param desAmount 扣钱的账户
* @param incAmount 增加的账户
* @param money 转账的金额
* @return
*/
public boolean transferMoneyService(Amount desAmount,Amount incAmount,double money); }
package com.zr.trasaction.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
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.zr.trasaction.Service.AmountService;
import com.zr.trasaction.daoImpl.AmountDaoImpl;
import com.zr.trasaction.entity.Amount;
import com.zr.trasaction.entity.BalanceException; @Service("amountServiceImpl")
public class AmountServiceImpl implements AmountService {
@Autowired(required=true)
private AmountDaoImpl amountDaoImpl;
@Autowired(required=true)
private JdbcTemplate jdbcTemplate; public void setAmountDaoImpl(AmountDaoImpl amountDaoImpl) {
this.amountDaoImpl = amountDaoImpl;
} public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* REQUIRED(默认值):在有transaction状态下执行;如当前没有transaction,则创建新的transaction
* 只读事务属性readOnly=true: 表示这个事务只读取数据但不更新数据, 这样可以帮助数据库引擎优化事务
* 超时事务属性timeout=3: 事务在强制回滚之前可以保持多久. 这样可以防止长期运行的事务占用资源.单位为秒,一般不设置,使用默认的
* noRollbackForClassName={"BalanceException"} 发生BalanceException异常不回滚
*/
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED,readOnly=true,timeout=3/*,noRollbackForClassName={"BalanceException"}*/)
@Override
public boolean transferMoneyService(Amount desAmount, Amount incAmount,
double money) {
String sql = "SELECT MONEY FROM TEST WHERE USERNAME=?";
double balance = jdbcTemplate.queryForObject(sql, Double.class, desAmount.getUser()); boolean success1 = amountDaoImpl.decreaseMoney(desAmount.getUser(), money);
boolean success2 = amountDaoImpl.increaseMoney(incAmount.getUser(), money); if(balance < money){
throw new BalanceException("余额不足");
}
return success1 && success2;
} }
package com.zr.trasaction.test; import javax.sql.DataSource; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate; import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zr.trasaction.Service.AmountService;
import com.zr.trasaction.ServiceImpl.AmountServiceImpl;
import com.zr.trasaction.daoImpl.AmountDaoImpl;
import com.zr.trasaction.entity.Amount; public class TestA { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Amount desAmount = new Amount("lf", 0);
Amount incAmount = new Amount("tl", 0);
AmountService impl = (AmountService) ac.getBean("amountServiceImpl");
boolean isSuccess = impl.transferMoneyService(desAmount, incAmount, 10); } }
spring框架 事务 注解配置方式的更多相关文章
- spring框架 事务 xml配置方式
user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- 跟着刚哥学习Spring框架--事务配置(七)
事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...
- spring,mybatis事务管理配置与@Transactional注解使用[转]
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...
- spring,mybatis事务管理配置与@Transactional注解使用
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...
- 五(一)、spring 声明式事务注解配置
一.事务概述: 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用:比如 用户购买图书:购买动作之前需要确认 ①图书的数量是否足够:②用户账号余额是否足够 ...
- Spring中事务的XML方式[声明方式]
事务管理: 管理事务,管理数据,数据完整性和一致性 事务[业务逻辑] : 由一系列的动作[查询书价格,更新库存,更新余额],组成一个单元[买书业务], 当我们动作当中有一个错了,全错~ ACID 原子 ...
- Spring框架事务支持模型的优势
全局事务 全局事务支持对多个事务性资源的操作,通常是关系型数据库和消息队列.应用服务器通过JTA管理全局性事务,API非常烦琐.UserTransaction通常需要从JNDI获取,意味着需要与JND ...
- spring mvc 基于注解 配置默认 handlermapping
spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...
随机推荐
- 《DSP using MATLAB》示例Example7.22
代码: h = [1, 2, 3, 4, 3, 2, 1]/15; M = length(h); n = 0:M-1; [Hr, w, a, L] = Hr_Type1(h); a L amax = ...
- Robot Framework接口测试(3)--http请求之post
http请求更多的是post请求,我们可以:查看说明:很多网站在登录的时候需要加上头部信息即headers,这个信息可以通过抓包工具获得——fiddler,一个轻量级的抓包工具,大神用了都说好~这里模 ...
- WPF 自定义绕圈进度条(转)
在设计界面时,有时会遇到进度条,本次讲解如何设计自定义的绕圈进度条,直接上代码: 1.控件界面 <UserControl x:Class="ProgressBarControl&quo ...
- Git 的 cherry-pick 功能
简而言之,cherry-pick就是从不同的分支中捡出一个单独的commit,并把它和你当前的分支合并.如果你以并行方式在处理两个或以上分支,你可能会发现一个在全部分支中都有的bug.如果你在一个分支 ...
- 简述MVC模式
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- C#枚举Enum[轉]
枚举是一个指定的常数,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.定义默认基数从O开始, ...
- eclipse “”base revision” vs. “latest from repository”
base revision(基本版本):代表的是最近一次从svn服务器上面获取的版本内容:本质还是本地版本,只不过这个版本是上次从服务器上面获取的. lastest from resource(资源库 ...
- python一个简单的web服务器和客户端
服务器: 当客户联系时创建一个连接套接字 从这个连接接收HTTP请求(*) 解释该请求所请求的特定文件 从服务器的文件系统获取该文件 并发送文件内容 ...
- VS2010环境下MFC使用DataGrid绑定数据源
如果MFC的软件中 使用DataGrid控件后,在别的电脑上不能运行行,需要拷贝一个 MSDATGRD.ocx 和msstdfmt.dll 文件在软件的目录中,并写一个批处理文件 reg.dat 文 ...
- shell编程中变量的运算 (shell 06)
主要包括以下3种 字符串操作数学运算浮点运算 一.字符串操作 字符串的连接 连接字2个字符串不需要任何连接符,挨着写即可 长度获取 expr length "hello" expr ...