--------------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

Spring 编程式事务管理

 
 

 
 

以转账为例

 
 

 
 

1、在
MySQL 中手动创建数据库和表

 
 

数据库名:tx_db,表名:account,字段:id、name、money

 
 

 
 

 
 

手动添加数据,用作测试

 
 

 
 

 
 

 
 

 
 

2、具体步骤

 
 

(1)配置事务管理器

 
 

<!-- 配置事务管理器 -->

<bean
id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!--

在 DataSourceTransactionManager 源代码中有

属性 dataSource 和其 set 方法,所以可以注入

-->

<property
name="dataSource"
ref="dataSource"/>

</bean>

 
 

 
 

 
 

(2)配置事务管理模板

 
 

<!-- 配置事务管理模板 -->

<bean
id="transactionTemplate"

class="org.springframework.transaction.support.TransactionTemplate">

<!--

在 TransactionTemplate 源代码中有属性 transactionTemplate

和其 set 方法,所以可以注入

-->

<property
name="transactionManager"
ref="transactionManager"/>

</bean>

 
 

 
 

 
 

(3)在业务层注入事务管理模板

 
 

<!-- 配置对象并注入属性 -->

<bean
id="accountService"
class="com.siwuxie095.service.AccountService">

<property
name="accountDao"
ref="accountDao"></property>

<!-- 在业务层注入注入事务管理模板 -->

<property
name="transactionTemplate"
ref="transactionTemplate"/>

</bean>

 
 

 
 

 
 

(4)在业务层手动编写代码实现事务管理

 
 

 
 

 
 

 
 

3、具体实现

 
 

(1)编写
Dao 类

 
 

AccountDao.java:

 
 

package com.siwuxie095.dao;

 
 

import org.springframework.jdbc.core.JdbcTemplate;

 
 

public class AccountDao {

 
 

private JdbcTemplate jdbcTemplate;

 

public
void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

this.jdbcTemplate = jdbcTemplate;

}

 

 

/**

* 转出

*/

public
void lessMoney(String from, int money) {

String sql="update account set money=money-? where name=?";

jdbcTemplate.update(sql, money, from);

}

 

 

/**

* 转入

*/

public
void moreMoney(String to, int money) {

String sql="update account set money=money+? where name=?";

jdbcTemplate.update(sql, money, to);

}

 

}

 
 

 
 

 
 

(2)编写一个
Service 类

 
 

AccountService.java:

 
 

package com.siwuxie095.service;

 
 

import org.springframework.transaction.TransactionStatus;

import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import org.springframework.transaction.support.TransactionTemplate;

 
 

import com.siwuxie095.dao.AccountDao;

 
 

public class AccountService {

 
 

private AccountDao accountDao;

private TransactionTemplate transactionTemplate;

 

public
void setAccountDao(AccountDao accountDao) {

this.accountDao = accountDao;

}

 

public
void setTransactionTemplate(TransactionTemplate transactionTemplate) {

this.transactionTemplate = transactionTemplate;

}

 

 

/**

* 转账

*/

public
void transfer(String from,String to,int money) {

 

 

// 在业务层手动编写代码实现事务管理

transactionTemplate.execute(new TransactionCallbackWithoutResult() {

 

@Override

protected
void doInTransactionWithoutResult(TransactionStatus status) {

 

accountDao.lessMoney(from, money);

 

// 即便中间出现了什么异常,也会进行回滚

// 如:int num=10/0;

 

accountDao.moreMoney(to, money);

 

}

});

 

 

}

 

}

 
 

 
 

 
 

(3)在配置文件中进行配置

 
 

applicationContext.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:aop="http://www.springframework.org/schema/aop"

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/aop

http://www.springframework.org/schema/aop/spring-aop.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">

 

 

<!-- 配置内置连接池 -->

<bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property
name="driverClassName"
value="com.mysql.jdbc.Driver"/>

<!--

jdbc:mysql:///tx_db 是 jdbc:mysql://localhost:3306/tx_db 的简写

-->

<property
name="url"
value="jdbc:mysql:///tx_db"/>

<property
name="username"
value="root"/>

<property
name="password"
value="8888"/>

</bean>

 

 

 

<!-- 配置事务管理器 -->

<bean
id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!--

在 DataSourceTransactionManager 源代码中有

属性 dataSource 和其 set 方法,所以可以注入

-->

<property
name="dataSource"
ref="dataSource"/>

</bean>

 

 

<!-- 配置事务管理模板 -->

<bean
id="transactionTemplate"

class="org.springframework.transaction.support.TransactionTemplate">

<!--

在 TransactionTemplate 源代码中有属性 transactionTemplate

和其 set 方法,所以可以注入

-->

<property
name="transactionManager"
ref="transactionManager"/>

</bean>

 

 

 

<!-- 配置对象并注入属性 -->

<bean
id="accountService"
class="com.siwuxie095.service.AccountService">

<property
name="accountDao"
ref="accountDao"></property>

<!-- 在业务层注入注入事务管理模板 -->

<property
name="transactionTemplate"
ref="transactionTemplate"/>

</bean>

 

<bean
id="accountDao"
class="com.siwuxie095.dao.AccountDao">

<property
name="jdbcTemplate"
ref="jdbcTemplate"></property>

</bean>

 

<bean
id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">

<!--

在 JdbcTemplate 源代码中有属性 dataSource

和其 set 方法,所以可以注入

-->

<property
name="dataSource"
ref="dataSource"></property>

</bean>

 
 

 
 

</beans>

 
 

 
 

 
 

(4)编写一个测试类

 
 

TestDemo.java:

 
 

package com.siwuxie095.test;

 
 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 
 

import com.siwuxie095.service.AccountService;

 
 

public class TestDmo {

 
 

/**

* 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 的 jar 包)

*

* 选中方法名,右键->Run As->JUint Test

*/

@Test

public
void testService() {

 

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

 

AccountService accountService=(AccountService) context.getBean("accountService");

 

accountService.transfer("小白", "小黑", 1000);

}

 

}

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

Spring编程式事务管理的更多相关文章

  1. spring 编程式事务管理和声明式事务管理

    编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...

  2. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

  3. Spring编程式事务管理和声明式事务管理

    本来想写一篇随笔记一下呢,结果发现一篇文章写的很好了,已经没有再重复写的必要了. https://www.ibm.com/developerworks/cn/education/opensource/ ...

  4. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  5. Spring学习8-Spring事务管理(编程式事务管理)

    一.Spring事务的相关知识   1.事务是指一系列独立的操作,但在概念上具有原子性. 比如转账:A账号-100, B账号+100,完成.这两个操作独立是没问题的. 但在逻辑上,要么全部完成,要么一 ...

  6. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  7. Spring事务管理的另一种方式--TransactionTemplate编程式事务管理简单入门

    1, 一直以来, 在用Spring进行事物管理时, 只知道用声明式的策略, 即根据不同的数据源, 配置一个事物管理器(TransactionManager), 通过配置切面(PointCut)应用到相 ...

  8. 8.spring:事务管理(上):Spring的数据库编程、编程式事务管理

    Spring的数据库编程 Spring框架提供了JDBC模板模式------>JdbcTemplate 简化了开发,在开发中并不经常是使用 实际开发更多使用的是Hibernate和MyBatis ...

  9. Spring事务管理之编程式事务管理

    © 版权声明:本文为博主原创文章,转载请注明出处 案例:利用Spring的编程式事务管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( `id` INT NOT ...

随机推荐

  1. [转]常用字符与ASCII代码对照表

    常用字符与ASCII代码对照表 为了便于查询,以下列出ASCII码表:第128-255号为扩展字符(不常用) ASCII码 键盘 ASCII 码 键盘 ASCII 码 键盘 ASCII 码 键盘 27 ...

  2. 字符串拼接和dom回流

    以对象的角度分析  对象的两方面 属性和方法 研究对象,主要是研究对象的属性和方法 案例: <ul id="list"> <li></li> & ...

  3. 转载 深入理解java类加载器

    1 基本信息 每个开发人员对java.lang.ClassNotFoundExcetpion这个异常肯定都不陌生,这背后就涉及到了java技术体系中的类加载.Java的类加载机制是技术体系中比较核心的 ...

  4. django-重写User模型

    User模型有很多功能,验证什么的,重写需要满足下面的功能(基本上写注释的地方都是需要的) 开始: 创建一个重写user的app, 记得注册app startapp newauth from djan ...

  5. jenkins坑—— shell 命令返回空导致构建失败

    今天用jenkins做CI遇到个坑,命令为:isSnapshot=`ls|grep isv-osp-service|grep -i snapshot` ls命令返回空的话,Jenkins构建就直接失败 ...

  6. plsql 执行批量文件

    plsql 执行批量文件 plsql>command window @c:\a.sql;@c:\b.sql;@c:\c.sql;

  7. IP Editor IP控件

    HWND hIpEdit; void __fastcall TForm2::FormCreate(TObject *Sender) { hIpEdit = CreateWindow(WC_IPADDR ...

  8. Simple2D-21(重构)渲染部分

    以前 Simple2D 的渲染方法是先设置 Pass,然后添加顶点数据,相同 Pass 的顶点数据会合并在一起.当设置新的 Pass 时,将旧的 Pass 和对应的顶点数据添加到渲染数组中.最后在帧结 ...

  9. 使用 C++11 编写类似 QT 的信号槽——下篇

    要实现 Signal-Slot,Signal 类中应该拥有一个保存 std::function 的数组: template<class FuncType> class Signal { p ...

  10. iOS 真机调试 Xcode 显示 device Error: device unavailable

    一般来说有两个原因: 1. iphone没有加到test device里,将iphone的设备id加到test device列表里 2. Xcode不支持当前的iOS版本,将Xcode升级到最新版