Spring编程式事务管理
--------------------siwuxie095
Spring 编程式事务管理
以转账为例
1、在
MySQL 中手动创建数据库和表
数据库名:tx_db,表名:account,字段:id、name、money

手动添加数据,用作测试

2、具体步骤
(1)配置事务管理器
|
<!-- 配置事务管理器 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 在 DataSourceTransactionManager 源代码中有 属性 dataSource 和其 set 方法,所以可以注入 --> <property </bean> |
(2)配置事务管理模板
|
<!-- 配置事务管理模板 --> <bean class="org.springframework.transaction.support.TransactionTemplate"> <!-- 在 TransactionTemplate 源代码中有属性 transactionTemplate 和其 set 方法,所以可以注入 --> <property </bean> |
(3)在业务层注入事务管理模板
|
<!-- 配置对象并注入属性 --> <bean <property <!-- 在业务层注入注入事务管理模板 --> <property </bean> |
(4)在业务层手动编写代码实现事务管理
3、具体实现
(1)编写
Dao 类
AccountDao.java:
|
package com.siwuxie095.dao; import org.springframework.jdbc.core.JdbcTemplate; public class AccountDao { private JdbcTemplate jdbcTemplate;
public this.jdbcTemplate = jdbcTemplate; }
/** * 转出 */ public String sql="update account set money=money-? where name=?"; jdbcTemplate.update(sql, money, from); }
/** * 转入 */ public 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 this.accountDao = accountDao; }
public this.transactionTemplate = transactionTemplate; }
/** * 转账 */ public
// 在业务层手动编写代码实现事务管理 transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override protected
accountDao.lessMoney(from, money);
// 即便中间出现了什么异常,也会进行回滚 // 如:int num=10/0;
accountDao.moreMoney(to, money);
} });
}
} |
(3)在配置文件中进行配置
applicationContext.xml:
|
<?xml <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 <property <!-- jdbc:mysql:///tx_db 是 jdbc:mysql://localhost:3306/tx_db 的简写 --> <property <property <property </bean>
<!-- 配置事务管理器 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 在 DataSourceTransactionManager 源代码中有 属性 dataSource 和其 set 方法,所以可以注入 --> <property </bean>
<!-- 配置事务管理模板 --> <bean class="org.springframework.transaction.support.TransactionTemplate"> <!-- 在 TransactionTemplate 源代码中有属性 transactionTemplate 和其 set 方法,所以可以注入 --> <property </bean>
<!-- 配置对象并注入属性 --> <bean <property <!-- 在业务层注入注入事务管理模板 --> <property </bean>
<bean <property </bean>
<bean <!-- 在 JdbcTemplate 源代码中有属性 dataSource 和其 set 方法,所以可以注入 --> <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
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService=(AccountService) context.getBean("accountService");
accountService.transfer("小白", "小黑", 1000); }
} |
【made by siwuxie095】
Spring编程式事务管理的更多相关文章
- spring 编程式事务管理和声明式事务管理
编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...
- Spring编程式事务管理及声明式事务管理
本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...
- Spring编程式事务管理和声明式事务管理
本来想写一篇随笔记一下呢,结果发现一篇文章写的很好了,已经没有再重复写的必要了. https://www.ibm.com/developerworks/cn/education/opensource/ ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- Spring学习8-Spring事务管理(编程式事务管理)
一.Spring事务的相关知识 1.事务是指一系列独立的操作,但在概念上具有原子性. 比如转账:A账号-100, B账号+100,完成.这两个操作独立是没问题的. 但在逻辑上,要么全部完成,要么一 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- Spring事务管理的另一种方式--TransactionTemplate编程式事务管理简单入门
1, 一直以来, 在用Spring进行事物管理时, 只知道用声明式的策略, 即根据不同的数据源, 配置一个事物管理器(TransactionManager), 通过配置切面(PointCut)应用到相 ...
- 8.spring:事务管理(上):Spring的数据库编程、编程式事务管理
Spring的数据库编程 Spring框架提供了JDBC模板模式------>JdbcTemplate 简化了开发,在开发中并不经常是使用 实际开发更多使用的是Hibernate和MyBatis ...
- Spring事务管理之编程式事务管理
© 版权声明:本文为博主原创文章,转载请注明出处 案例:利用Spring的编程式事务管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( `id` INT NOT ...
随机推荐
- VS2005常用快捷键
Visual C++ 2005有很多种快捷键的映射方案,有适合 Emacs 用户的,有适合 Visual C++ 6.0 用户的,也有 Visual Studio 2005的,下面的快捷键符合IDE默 ...
- 接口测试3-3Excel格式
java操作Excel,需要第三方库poi #xml <dependency> <groupId>org.apache.poi</groupId> <arti ...
- 【BZOJ】1975 [Sdoi2010]魔法猪学院(A*)
题目 传送门:QWQ 分析 k短路,Astar.估价函数是终点向外跑的最短路. 显然不是正解qwq. 代码 // By noble_ // Astar algorithm // #include &l ...
- 使用for...of 优点,代替for...in,forEach和for循环
来自阮一峰ES6标准: http://es6.ruanyifeng.com/#docs/iterator
- AVL树Python实现
# coding=utf-8 # AVL树Python实现 def get_height(node): return node.height if node else -1 def tree_mini ...
- python(十二)下:ORM框架SQLAlchemy使用学习
此出处:http://blog.csdn.net/fgf00/article/details/52949973 本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 ...
- linux主机555、644、666、755、777权限详解
linux主机555.644.666.755.777权限详解 发表时间:2014-06-03 05:07 来源:未知 分类:其它代码 作者:岑溪网站开发 点击:次 linux主机555.644.666 ...
- Dell Latitude 3490 使用 UEFI+GPT 安装 Win7 x64
转载请注明出处!转载请注明出处!转载请注明出处! 公司近期采购了一批笔记本,由于刚好赶上Dell升级换代,原来的3480升级到了3490. 由于部分同事用不惯Win10系统,再加上有些软件不兼容,于是 ...
- zabbix_get无法执行agent端的脚本文件解决办法
一,无法执行脚本参考网站:http://blog.51cto.com/13589448/2070180 权限不足时提示: server端提示: [root@yao local]# zabbix_get ...
- Spring Boot学习--项目启动时执行特定方法
Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动 ...