Spring事务管理2----编程式事务管理
编程式事务管理
通过使用将Spring框架提供的TransactionTemplate模板注入到业务层来进行事务管理,这样对业务层原来的代码修改过多。不利于项目的后期维护。
以下是声明式事务管理的具体代码实现:
环境搭建:http://www.cnblogs.com/kuoAT/p/7803193.html
Dao层
package com.sdf.spring;/*** @author AT* 转账dao层*/ public interface AccountDao {/*** 转出钱* @param outer* @param money*/public void remove(String outer,Double money);/*** 转入钱* @param input* @param money*/public void add(String input,Double money); }
dao层实现类
package com.sdf.spring;import org.springframework.jdbc.core.support.JdbcDaoSupport;/*** 转账dao层实现*/ public class AccountDaoimpl extends JdbcDaoSupport implements AccountDao{/*** 转出钱* @param outer* @param money*/@Overridepublic void remove(String outer, Double money) {String sql = "update account set money = money - ? where name = ?";this.getJdbcTemplate().update(sql, money,outer);}/*** 转入钱* @param input* @param money*/@Overridepublic void add(String input, Double money) {String sql = "update account set money = money + ? where name = ?";this.getJdbcTemplate().update(sql, money,input);}}
service业务层
package com.sdf.spring;/*** @author AT* 转账业务接口*/ public interface AccountSevice {public void transfer(String input,String out,Double money);//消费 }
service业务层实现类
/*** @author AT* 编程式事务管理*/ public class AccountServiceImpl implements AccountSevice {@Resource(name="accountDao")private AccountDao accountDao;//在业务类中注入事务模板@Resource(name="transactionTemplate")private TransactionTemplate transactionTemplate;@Overridepublic void transfer(final String input, final String out,final Double money) { // accountDao.remove(out, money); // int a = 1/0; // accountDao.add(input, money);transactionTemplate.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus status) {accountDao.add(input, money);int a = 1/0;//模拟转账过程中发生故障accountDao.remove(out, money);}});}public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}public void setTransactionTemplate(TransactionTemplate transactionTemplate) {this.transactionTemplate = transactionTemplate;}}
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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 引入外部属性文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置C3P0连接池 --><bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!-- dao层和业务的类 --><bean id="accountDao" class="com.sdf.spring.dao.impl.AccountDaoimpl"><property name="dataSource" ref="datasource"></property></bean><bean id="accountService" class="com.sdf.spring.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"/><property name="transactionTemplate" ref="transactionTemplate"/></bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="datasource"/></bean><!-- 定义事务管理模板:Spring为了简化事务管理的代码提供的类 --><bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"><property name="transactionManager" ref="transactionManager"/></bean> </beans>
测试类
/*** @author AT* 测试转账信息 编程式事务管理*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AccountTest {@Resource(name="accountService")private AccountSevice accountService;@Testpublic void test01(){accountService.transfer("A", "B", 100d);}public void setAccountService(AccountSevice accountService) {this.accountService = accountService;} }
转载于:https://www.cnblogs.com/kuoAT/p/7803324.html
Spring事务管理2----编程式事务管理相关推荐
- Spring中两种编程式事务管理
Spring中两种编程式事务管理 在代码中显示调用beginTransaction,commit,rollback等与事务处理相关的方法,这就是编程式事务管理,当只有少数事务操作时,编程式事务管理才比 ...
- Spring事务传播行为-编程式事务
1. 编程式事务 Spring 事务管理有两种方式: 编程式事务:通过编码方式实现事务: 声明式事务:基于AOP实现,如 @Transactional 声明式注解. 2. 实现原理 编程式事务基于Tr ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理(转)
摘要 Spring 的事务管理是 Spring 框架中一个比较重要的知识点,该知识点本身并不复杂,只是由于其比较灵活,导致初学者很难把握.本教程从基础知识开始,详细分析了 Spring 事务管理的使用 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理
转自:http://www.open-open.com/lib/view/open1414310646012.html 关于本教程 本教程将深切讲授 Spring 庞杂而丁壮夜的事务治理功用,包括编程 ...
- Spring笔记(4) - Spring的编程式事务和声明式事务详解
一.背景 事务管理对于企业应用而言至关重要.它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性.就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到操作 ...
- java编程式事务_Spring编程式和声明式事务实例讲解
Spring事务管理 Spring支持两种方式的事务管理: 编程式事务管理: 通过Transaction Template手动管理事务,实际应用中很少使用, 使用XML配置声明式事务: 推荐使用(代码 ...
- 编程式事务和声明式事务浅析
事务管理 在spring中,事物管理一般分为两类,编程式事务管理和声明式事务管理. 编程式事务管理,一般我们使用TransactionTemplate来实现. 声明式事物管理本质上是spring AO ...
- [事务] 编程式事务和声明式事务的优缺点
事务管理在系统开发中是不可缺少的一部分,Spring提供了很好事务管理机制,主要分为编程式事务和声明式事务两种. 关于事务的基础知识,如什么是事务,数据库事务以及Spring事务的ACID.隔离级别. ...
- 事务声明声明式事务和编程式事务区别
事务声明声明式事务和编程式事务区别 1.编程式事务: 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理.管理使用TransactionTemplate或者直接使用底层的Pla ...
最新文章
- 如何在Linux系统上部署接口测试环境
- EOS 账号创建和信息查询
- java web六:tomcat其他小问题
- 玩转springboot:整合mybatis实例
- Ubuntu 下一个 vim 建立python 周围环境 构造
- 发布composer包到 Packagist,并设置自动同步(从github到Packagist)
- Andorid之Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)的用法总结
- 深度学习VS机器学习——到底什么区别
- C# 委托?这篇文章让你困惑全摆脱!
- gentoo Portage使用
- 阿里云盾证书服务助力博客装逼成功
- web前端学习(总结/心得)
- QAM识别算法matlab,16qam调制识别matlab
- 英尺英寸和厘米的换算_C语言中关于英尺、英寸、厘米的换算
- python随机密码生成代码大全_Python实现随机密码生成器
- 记录一次iPhone5s的iCloud bypass经历
- A Game of Thrones(50)
- OpenOCD调试ARM芯片,Ubuntu 安装arm-none-eabi-gdb
- Keil的安装及使用
- 计算机技术在医学应用中的论文,浅论计算机技术在医学中的应用.pdf
热门文章
- day17 Python 反射获取内容和修改内容
- 认识一个工具 Stopwatch
- 9-21 调试javaweb 数据库连接感想
- krpano音量控制(我们已经转移到krpano中国网站 krpano360.com)
- 通过 powershell 配置 IIS
- iOS6中旋转的略微改变
- python---之np.prod() 函数计算数组元素乘积等
- catkin_make 只编译一个包
- java 远程连接_java实现连接远程服务器并执行命令的基本原理
- python的django_真正搞明白Python中Django和Flask框架的区别