< 1 > 配置文件

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
> <!-- 配置 C3P0 连接池数据源, 作为 DAO 层的数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl">
<value>jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</value>
</property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="920619"></property>
</bean> <!-- 配置 DAO 层, 把 C3P0 连接池作为数据源注入到 DAO 层 -->
<bean id="accountDao" class="spring.things.AccountDao">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置 SERVICE 层, 把 DAO 层注入到 SERVICE 层 -->
<bean id="accountService" class="spring.things.AccountService">
<property name="accountDao" ref="accountDao"></property>
</bean> <!-- 配置事物管理类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置业务层的代理 -->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 注入被代理对象 -->
<property name="target" ref="accountService"></property>
<!-- 注入事物管理类 -->
<property name="transactionManager" ref="transactionManager"></property>
<!-- 注入事物属性 -->
<property name="transactionAttributes">
<props>
<!-- 被代理对象的方法名格式 可用通配符 *, 如 A* 标识以 A 开头的所有类 -->
<!--
PROPAGATION: 事物的传播行为
ISOLATION: 事物的隔离级别
readOnly: 只读
-Exception: 发生哪些异常回滚事物
+Exception: 发生哪些异常不会滚事物
-->
<prop key="transfer">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean> </beans>

事务传播行为类型 : PROPAGATION

说明

  PROPAGATION_REQUIRED

  如果当前没有事务,就新建一个事务;如果已经存在一个事务,加入到这个事务中。(这个是最常见的选择)

  PROPAGATION_SUPPORTS

  支持当前事务。如果当前没有事务,就以非事务方式执行

  PROPAGATION_MANDATORY

  使用当前的事务。如果当前没有事务,就抛出异常

  PROPAGATION_REQUIRES_NEW

  新建事务。如果当前存在事务,就把当前事务挂起

  PROPAGATION_NOT_SUPPORTED

  以非事务的方式执行操作。如果当前存在事务,就把当前事务挂起

  PROPAGATION_NEVER

  以非事务方式执行。如果当前存在事务,则抛出异常

  PROPAGATION_NESTED

  如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作

  隔离级别 :  ISOLATION

脏读

不可重复读

幻象读

第一类丢失更新

第二类丢失更新

  ISOLATION_READ_UNCOMMITED

允许

允许

允许

不允许

允许

  ISOLATION_READ_COMMITED

不允许

允许

允许

不允许

允许

  ISOLATION_REPEATABLE_READ

不允许

不允许

允许

不允许

不允许

  ISOLATION_SERIALIZABLE

不允许

不允许

不允许

不允许

不允许

< 二 > DAO 和 SERVICE 层接口

package spring.things;

public interface IAccountDao {
/**
* 转出钱的方法
* @param outName : 转出人
* @param money : 金额
*/
public void outMoney(String outName, double money); /**
* 收钱的方法
* @param inName : 收钱人
* @param money : 金额
*/
public void inMoney(String inName, double money);
}
package spring.things;

public interface IAccountService {
/**
* 转账的方法
* @param outUser: 出账人
* @param inUser : 收账人
* @param money : 金额
*/
public void transfer(String outUser, String inUser, double money);
}

< 三 > 接口的实现类 

package spring.things;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDao extends JdbcDaoSupport implements IAccountDao {

    @Override
public void outMoney(String outName, double money) {
String sql = "update User set money = money - ? where UserName = ?";
this.getJdbcTemplate().update(sql, money, outName);
} @Override
public void inMoney(String inName, double money) {
String sql = "update User set money = money + ? where UserName = ?";
this.getJdbcTemplate().update(sql, money, inName);
} }
package spring.things;

public class AccountService implements IAccountService {

    // 注入的 Dao 层
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
} // 这个方法在测试中是一个事物单元
@Override
public void transfer(String outName, String inName, double money) {
accountDao.outMoney(outName, money);
accountDao.inMoney(inName, money);
} }

< 四 > 测试类

package spring.things;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) // 注入 JUNIT 的字节码
@ContextConfiguration("classpath:spring/things/Things.xml") // 注入 SPORING 配置文件
public class ATest_01 { // 注入代理类, 注意, 被代理实现类的接收接口 而不是 实现类
@Resource(name = "accountServiceProxy")
private IAccountService accountService; @Test
public void demo_1(){
accountService.transfer("张三", "李四", 200);
} }

JAVA Spring 事物 ( 已转账为例 ) 基于 XML 配置,事务类型说明的更多相关文章

  1. JAVA Spring 事物 ( 已转账为例 ) 基于 AOP 注解

    <一> 配置为文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  2. 【Spring Framework】Spring入门教程(二)基于xml配置对象容器

    基于xml配置对象容器--xml 标签说明 alias标签 作用:为已配置的bean设置别名 --applicationContext.xml配置文件 <?xml version="1 ...

  3. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  4. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  5. Spring Boot 框架下使用MyBatis访问数据库之基于XML配置的方式

    MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...

  6. [刘阳Java]_Spring AOP基于XML配置介绍_第9讲

    基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件 ...

  7. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  8. Spring Aop(七)——基于XML配置的Spring Aop

    转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...

  9. Sping MVC不使用任何注解处理(jQuery)Ajax请求(基于XML配置)

    1. Spring Spring框架是一个轻量级的解决方案,是一个潜在的一站式商店,用于构建企业就绪的应用程序.Spring框架是一个Java平台,为开发Java应用程序提供全面的基础架构支持.Spr ...

随机推荐

  1. LG1801 【黑匣子_NOI导刊2010提高(06)】

    看到各路dalao用平衡树的做法,表示本人不才,并不会. 然而我会优先队列_huaji_,并且发现用堆解题的dalao们并没有基于在线的做法 于是我的showtime到了 评测结果:https://w ...

  2. 2017年最新cocoapods安装教程(解决淘宝镜像源无效以及其他源下载慢问题)

    首先,先来说一下一般的方法吧,就是把之前的淘宝源替换成一个可用的的源: 使用终端查看当前的源 gem sources -l gem sources -r https://rubygems.org/ # ...

  3. idea中,war 与 war exploded 区别

    idea中,war 与 war exploded 区别: war模式:将WEB工程以包的形式上传到服务器 : war exploded模式:将WEB工程以当前文件夹的位置关系上传到服务器:其实访问的是 ...

  4. centos 使用mutt发送邮件带附件

    1.安装mutt工具 yum install -y mutt 2.使用mutt发邮件并带附件echo "统计日志" | /usr/bin/mutt -s "统计日志&qu ...

  5. VT-x is not available. (VERR_VMX_NO_VMX) on windows 8

    've installed virtualbox on windows 8 x64, then when I open linux guest machine, I got this error me ...

  6. 导出pb模型之后测试的python代码

    链接:https://blog.csdn.net/thriving_fcl/article/details/75213361 saved_model模块主要用于TensorFlow Serving.T ...

  7. 如何在eclipse中安装angularjs插件

    1,首先,在eclipse中使用angularjs的时候,由于没有相应的提示,导致在开发的时候给我们带来了很多的不便,需要在这上面耗费一些时间.那么这时候我们都在想可不可以让eclipse也和一些前端 ...

  8. <<APUE>> 线程的分离状态

    在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死:在被其他线程回收之前,它的存储器资源(如栈)是不释放的.相反, ...

  9. 2013-7-28 802.11n帧聚合

    芯片开发阶段的帧聚合迥异于商用AP,前者更偏向实现过程,后者偏向结果.也就是说用户在使用商用设备时不会管你特性是如何实现的,他们只关心效果,开启这个功能能否实实在在的提高AP的吞吐量. 网上搜索了众多 ...

  10. FQ:从入门到放弃(二)

    上次的FQ:从入门到放弃(一)介绍了XXNet的部署和基本使用.本文整理一些部署过程中出现的问题,都是这几天朋友们安装过程中出现的问题.如果覆盖不全,欢迎在博客下方评论,互相交流,互相学习. 不过首先 ...