DAO跨事物调用---转账

第一步创建实体类:Entity
package com.beiwo.epet.entity;
public class Account {
private int id;
private String name;
private int money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
第二步:基类接口和实现基类接口:Account AccountDaoImpl
package com.beiwo.epet.dao;
import com.beiwo.epet.entity.Account;
public interface AccountDao {
public void updateAccount(String fromName,String toName,int money)throws Exception;
public void updateAccount(Account account)throws Exception;
public Account findAccountByName(String name)throws Exception;
}
package com.beiwo.epet.dao.impl;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.beiwo.epet.dao.AccountDao;
import com.beiwo.epet.entity.Account;
import com.beiwo.epet.util.C3P0Util;
import com.beiwo.epet.util.TransactionManager;
public class AccountDaoImpl implements AccountDao{
@Override
public void updateAccount(String fromName, String toName, int money) throws Exception{
QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
qr.update("UPDATE account SET money=money-? WHERE name=?",money,fromName);
qr.update("UPDATE account SET money=money+? WHERE name=?",money,toName);
}
@Override
public void updateAccount(Account account) throws Exception{
QueryRunner qr=new QueryRunner();
qr.update(TransactionManager.getConnection(),"UPDATE account SET money=? WHERE name=?", account.getMoney(),account.getName());
}
@Override
public Account findAccountByName(String name) throws Exception{
QueryRunner qr=new QueryRunner();
return qr.query(TransactionManager.getConnection(),"SELECT * FROM account WHERE name=?",new BeanHandler<Account>(Account.class),name);
}
}
第三步:业务逻辑接口和业务逻辑的实现:AccountService AccountServiceImply
package com.beiwo.epet.service;
public interface AccountService {
public void transfer(String formName,String toName,int money);
}
package com.beiwo.epet.service.impl;
import com.beiwo.epet.dao.AccountDao;
import com.beiwo.epet.dao.impl.AccountDaoImpl;
import com.beiwo.epet.entity.Account;
import com.beiwo.epet.service.AccountService;
import com.beiwo.epet.util.TransactionManager;
public class AccountServiceImpl implements AccountService{
@Override
public void transfer(String formName, String toName, int money) {
AccountDao accountDao=new AccountDaoImpl();
try {
///开始一个事务,start transaction;
//获取转入和转出的账户对象
TransactionManager.startTransaction();
Account fromAccount=accountDao.findAccountByName(formName);
Account toAccount=accountDao.findAccountByName(toName);
//修改账户的各自金额
fromAccount.setMoney(fromAccount.getMoney()-money);
toAccount.setMoney(toAccount.getMoney()+money);
//完成转账的操作
accountDao.updateAccount(fromAccount);
int i=2/0;
accountDao.updateAccount(toAccount);
TransactionManager.commitTransaction();
} catch (Exception e) {
try {
TransactionManager.rollbackTransaction();;//事务的回滚
} catch (Exception e2) {
e2.printStackTrace();
}
}finally{
try {
TransactionManager.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
第四步:测试类 TesTransfer
package com.beiwo.epet.test; import org.junit.Test; import com.beiwo.epet.service.AccountService;
import com.beiwo.epet.service.impl.AccountServiceImpl; public class TestTransfer { @Test
public void test(){
AccountService accountService=new AccountServiceImpl(); accountService.transfer("aaa", "bbb", 100); }
}
数据库的建立:

DAO跨事物调用---转账的更多相关文章
- Dao跨事务调用实现转账功能
1.首先在数据库当中创建数据库,并且创建它的 实现类 package com.beiwo.epet.entity; public class Account { private int id; pri ...
- 配置CORS解决跨域调用—反思思考问题的方式
导读:最近都在用一套完整的Java EE的体系做系统,之前都是用spring框架,现在弄这个Java EE,觉得新鲜又刺激.但,由于之前没有过多的研究和使用,在应用的过程中,也出现了不少的问题.累积了 ...
- Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结
Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结 1.1. 边缘检测的基本方法Canny最常用了1 1.2. 编写matlab边缘检测代码, ...
- AJAX跨域调用ASP.NET MVC或者WebAPI服务的解决方案
问题描述 当跨域(cross domain)调用ASP.NET MVC或者ASP.NET Web API编写的服务时,会发生无法访问的情况. 重现方式 使用模板创建一个最简单的ASP.NET Web ...
- ajax——CORS跨域调用REST API 的常见问题以及前后端的设置
RESTful架构是目前比较流行的一种互联网软件架构,在此架构之下的浏览器前端和手机端能共用后端接口. 但是涉及到js跨域调用接口总是很头疼,下边就跟着chrome的报错信息一起来解决一下. 假设:前 ...
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码】
项目中常常用到搜索,特别是导航类的网站.自己做关键字搜索不太现实,直接调用百度的是最好的选择.使用jquery.ajax的jsonp方法可以异域调用到百度的js并拿到返回值,当然$.getScript ...
- js Ajax跨域调用JSON并赋值全局变量
//跨域调用JSON <script type="text/javascript"> function _callback(obj) { alert(obj); } j ...
- ThinkPHP跨控制器调用方法
跨控制器调用方法 1. 先造对象,再调用里面的方法 $sc=new \Home\Controller\IndexController(); 用绝对路径找echo $sc->ShuChu(); ...
- 跨域调用webapi
web端跨域调用webapi 在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webap ...
随机推荐
- leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【转】MAPI over HTTP协议
这是一篇非常详细和精彩的介绍MAPI over HTTP协议英文博文.原文地址如下: http://blogs.technet.com/b/exchange/archive/2014/05/09/ou ...
- 从网易与淘宝的font-size思考前端设计稿与工作流 (转)
从网易与淘宝的font-size思考前端设计稿与工作流 阅读目录 1. 问题的引出 2. 简单问题简单解决 3. 网易的做法 4. 淘宝的做法 5. 比较网易与淘宝的做法 6. 如何与设计协作 7 ...
- mybatis 与 缓存
首先从配置文件说起,有个cacheEnabled的配置项,当设置为true时(默认就是true),Session就会用一个CachingExecutor来包装我们的Executor实例: public ...
- SQL Server 开发-语法学习
一.定义变量 --简单赋值 declare @a int print @a --使用select语句赋值 ) select @user1='张三' print @user1 ) print @user ...
- MVC学习笔记-01
什么是MVC MVC是指(Model-View-Controll即模型-视图-控制器)用于表示表示一种软件架构模式,它把软件分成三个基本的部分:模型(Model),视图(view),控制器(Contr ...
- Ubuntu 14.04 (Trusty Tahr) LTS发布,附下载地址,各种镜像【bubuko.com】
Ubuntu 14.04 有很多的改进和新功能: 同时还发布几个不同版本:Ubuntu GNOME.Kubuntu.Xubuntu.Lubuntu.Edubuntu.Ubuntu Kylin.Ubun ...
- lumen 构建api(dingo api)
什么是 API API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力, ...
- Mac iTerm with Powerline
1. 下载iTerm 地址: http://www.iterm2.com/ 完全可以取代Mac自带的终端了. 2. 之前我装过oh-my-zsh git clone git://github.com/ ...
- 很不错的在线Office控件:IWebOffice与SOAOffice
http://blog.csdn.net/cjh200102/article/details/17220441 iWebOffice2003文档控件 iWebOffice2003网络文档中间件能够在I ...