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 ...
随机推荐
- python protobuf序列化repeated运用
下面是proto描述文件的定义 message Person { required string name = 1; required int32 id = 2; optional string em ...
- Web 数据源
问题描述: ClassCastException:类型转换异常 问题代码: private static LinkedList<Connection> pool = (LinkedList ...
- 浅谈forword和sendRedirect
最近项目中全部用ajax请求数据,导致在做登录过滤器时不能重定向,然后仔细翻了翻Forward和sendRedirect,以下内容收集自百度: 1. forward (服务器端作的重定向) 服务器往c ...
- 用dataset保存数据注意的问题
Private Function Save() As Boolean Try Dim dschgs As DataSet = ds.GetChanges(DataRowState.Added + Da ...
- Laravel 5.4: 特殊字段太长报错
laravel 5.4 改变了默认的数据库字符集,现在utf8mb4包括存储emojis支持.如果你运行MySQL v5.7.7或者更高版本,则不需要做任何事情. 当你试着在一些MariaDB或者一些 ...
- selenium+python自动化89-用例不通过的时候发送邮件
前言 实现需求:当测试用例全部通过的时候,不发邮件,当用例出现Error或Failure的时候发送邮件 解决思路:生成html测试报告后,用bs4解析html页面,写个函数判断页面上是都有不通过的记录 ...
- uva-10344
题意: 枚举23点,注意,数字也是可以枚举的,wa了一次 #include<stdio.h> #include<iostream> #include<sstream> ...
- 逆地址解析协议RARP
解决的问题 一般系统启动时,从引导磁盘中获取ip 有些机器没有引导磁盘,如X终端或无盘工作站,则需要采用其他方法来获得IP地址 解决的过程 无盘系统依据RARP协议 从接口卡上读取唯一的硬件地址,然后 ...
- 8.tomcat认证访问
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 接上例:namespce的作用除了在前面提到的避免协同开发名字冲突外,还为认 ...
- vs2015 调试IIS
vs2015 调试IIS vs2015,menu,调试>附加到进程>w3wp 然后用浏览器打开网页,单步调试跟踪. http://blog.csdn.net/hyperhawk/artic ...