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 ...
随机推荐
- 杂项:CDN
ylbtech-杂项:CDN CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快 ...
- 统计numpy数组中每个值的个数
import numpy as np from collections import Counter data = np.array([1.1,2,3,4,4,5]) Counter(data) #简 ...
- ffmpeg 编码(视屏)
分析ffmpeg_3.3.2 muxing 1:分析主函数,代码如下: int main(int argc, char **argv) { OutputStream video_st = { }, a ...
- git 上传本地代码到github 服务器
git init git add . (所有文件用 点表示) git commit -m "注释语句" git remote add origin https://github.c ...
- DRL前沿之:Benchmarking Deep Reinforcement Learning for Continuous Control
1 前言 Deep Reinforcement Learning可以说是当前深度学习领域最前沿的研究方向,研究的目标即让机器人具备决策及运动控制能力.话说人类创造的机器灵活性还远远低于某些低等生物,比 ...
- 深度学习RNN实现股票预测实战(附数据、代码)
背景知识 最近再看一些量化交易相关的材料,偶然在网上看到了一个关于用RNN实现股票预测的文章,出于好奇心把文章中介绍的代码在本地跑了一遍,发现可以work.于是就花了两个晚上的时间学习了下代码,顺便把 ...
- 时间模块 --- time
表示时间有三种方法:timestamps Format String struct-time 1 时间戳 :通常来说,时间戳表示的是从1970年一月一日00:00:00开始按秒计算的偏移量,使 ...
- django的小操作,查询效率up, 引用art-template模板+djangorestframework
Part1: 提高查询效率newses = News.objects.select_related('category', 'author').get(id=1) # category和author字 ...
- 文件的编辑命令-echo/cat
touch test.yaml echo "line1 line2" >> test.yaml cat test.yaml line1 line2 # 创建test.y ...
- 6.requests编写企查查爬虫
(为编写完善能拿下来数据) 企查查代码数据如下: #encoding:utf-8 import requests from lxml import etree import random import ...