通过银行转账业务体会JAVA与存储过程不同实现方式
2009-07-13 15:32
633 查看
业务说明及模拟数据代码:
任务:帐户表(帐户号,姓名,余额,锁定)。实现帐号1向帐号2转账若干元。
业务要求
1、两个帐户要存在(略)
2、转账方最低余额为10元
3、如果任何一方被锁定(u_lock=1),则不能转
4、要保证交易完整性(不能一个成功,一个失败)
create table T_ACCOUNT
(
U_ID NUMBER(5) primary key,
U_NAME VARCHAR2(100) not null,
U_MONEY NUMBER(7) not null,
U_LOCK NUMBER(1) default 0
);
insert into T_ACCOUNT (U_ID, U_NAME, U_MONEY, U_LOCK)
values (1, '张三', 900, 0);
insert into T_ACCOUNT (U_ID, U_NAME, U_MONEY, U_LOCK)
values (2, '李四', 100, 0);
insert into T_ACCOUNT (U_ID, U_NAME, U_MONEY, U_LOCK)
values (3, '杨五郎', 3000, 1);
commit;
Java业务实现核心代码代码:
public void transMoney() throws Exception{
//操作数据库(业务上的连接对象)
Connection conn = DBUtil.getNewConn();
//try捕捉的是SQL异常
try {
conn.setAutoCommit(false);
dao.setConn(conn);
poFrom = dao.getpAccountPo(poFrom.getId());
poTo = dao.getpAccountPo(poTo.getId());
System.out.println("查询账户完成,请稍侯...");
new Scanner(System.in).nextLine();
if(poFrom==null || poTo==null){
throw new Exception("帐户不存在!");
}
else if(poTo.getLock()==1 || poFrom.getLock()==1){
throw new Exception("帐户已被锁定!");
}
else if(poFrom.getMoney()-this.money<10){
throw new Exception("帐户余额不足!");
}
//转账
this.poFrom.setMoney(this.poFrom.getMoney()-this.money);
this.poTo.setMoney(this.poTo.getMoney()+this.money);
dao.updateMoney(poFrom);
dao.updateMoney(poTo);
//演示:某数据被修改以后不能再被其它用户修改
System.out.println("确定转账吗(1-确定;其它-取消)?");
int ok = new Scanner(System.in).nextInt();
if(ok==1){
dao.getConn().commit();
conn.setAutoCommit(true);
System.out.println("恭喜,转账成功!");
}else{
dao.getConn().rollback();
System.out.println("用户取消操作!");
}
} catch (SQLException e1) {
try {
dao.getConn().rollback();
System.out.println("出错回滚啦!"+e1.getMessage());
} catch (SQLException e) {
e.printStackTrace();
}
}finally{
DBUtil.closeConn(dao.getConn());
}
}
存储过程实现代码代码:
create or replace procedure sp_trans_money
(
p_from number,
p_to number,
p_money NUMBER
)
is
v_count number;
v_lock number;
v_money number;
begin
--to是否存在
select count(*) into v_count from t_account where u_id = p_to;
if v_count = 0 then
raise_application_error(-20087, '目标用户不存在!');
end if;
--to是否锁定
select u_lock into v_lock from t_account where u_id = p_to;
if v_lock = 1 then
raise_application_error(-20088, '该用户已锁定!');
end if;
--from是否存在
select count(*) into v_count from t_account where u_id = p_from;
if v_count = 0 then
raise_application_error(-20087, '转账用户不存在!');
end if;
--from是否锁定及余额
select u_lock, u_money into v_lock, v_money
from t_account
where u_id = p_from;
if v_lock = 1 then
raise_application_error(-20088, '该用户已锁定!');
elsif (v_money - p_money < 10) then
raise_application_error(-20089, '该用户余额不足!');
end if;
update t_account set u_money = u_money - p_money where u_id = p_from;
update t_account set u_money = u_money + p_money where u_id = p_to;
--通常是由存储过程提交事务
--如果存储过程不提交,则由调用方提交事务
--好处:可以将多个存储过程放在一个事务中,但通常一个存储过程就是处理一个单独的事务,所以在实际开发中,具体情况具体处理
--commit;
end;
任务:帐户表(帐户号,姓名,余额,锁定)。实现帐号1向帐号2转账若干元。
业务要求
1、两个帐户要存在(略)
2、转账方最低余额为10元
3、如果任何一方被锁定(u_lock=1),则不能转
4、要保证交易完整性(不能一个成功,一个失败)
create table T_ACCOUNT
(
U_ID NUMBER(5) primary key,
U_NAME VARCHAR2(100) not null,
U_MONEY NUMBER(7) not null,
U_LOCK NUMBER(1) default 0
);
insert into T_ACCOUNT (U_ID, U_NAME, U_MONEY, U_LOCK)
values (1, '张三', 900, 0);
insert into T_ACCOUNT (U_ID, U_NAME, U_MONEY, U_LOCK)
values (2, '李四', 100, 0);
insert into T_ACCOUNT (U_ID, U_NAME, U_MONEY, U_LOCK)
values (3, '杨五郎', 3000, 1);
commit;
Java业务实现核心代码代码:
public void transMoney() throws Exception{
//操作数据库(业务上的连接对象)
Connection conn = DBUtil.getNewConn();
//try捕捉的是SQL异常
try {
conn.setAutoCommit(false);
dao.setConn(conn);
poFrom = dao.getpAccountPo(poFrom.getId());
poTo = dao.getpAccountPo(poTo.getId());
System.out.println("查询账户完成,请稍侯...");
new Scanner(System.in).nextLine();
if(poFrom==null || poTo==null){
throw new Exception("帐户不存在!");
}
else if(poTo.getLock()==1 || poFrom.getLock()==1){
throw new Exception("帐户已被锁定!");
}
else if(poFrom.getMoney()-this.money<10){
throw new Exception("帐户余额不足!");
}
//转账
this.poFrom.setMoney(this.poFrom.getMoney()-this.money);
this.poTo.setMoney(this.poTo.getMoney()+this.money);
dao.updateMoney(poFrom);
dao.updateMoney(poTo);
//演示:某数据被修改以后不能再被其它用户修改
System.out.println("确定转账吗(1-确定;其它-取消)?");
int ok = new Scanner(System.in).nextInt();
if(ok==1){
dao.getConn().commit();
conn.setAutoCommit(true);
System.out.println("恭喜,转账成功!");
}else{
dao.getConn().rollback();
System.out.println("用户取消操作!");
}
} catch (SQLException e1) {
try {
dao.getConn().rollback();
System.out.println("出错回滚啦!"+e1.getMessage());
} catch (SQLException e) {
e.printStackTrace();
}
}finally{
DBUtil.closeConn(dao.getConn());
}
}
存储过程实现代码代码:
create or replace procedure sp_trans_money
(
p_from number,
p_to number,
p_money NUMBER
)
is
v_count number;
v_lock number;
v_money number;
begin
--to是否存在
select count(*) into v_count from t_account where u_id = p_to;
if v_count = 0 then
raise_application_error(-20087, '目标用户不存在!');
end if;
--to是否锁定
select u_lock into v_lock from t_account where u_id = p_to;
if v_lock = 1 then
raise_application_error(-20088, '该用户已锁定!');
end if;
--from是否存在
select count(*) into v_count from t_account where u_id = p_from;
if v_count = 0 then
raise_application_error(-20087, '转账用户不存在!');
end if;
--from是否锁定及余额
select u_lock, u_money into v_lock, v_money
from t_account
where u_id = p_from;
if v_lock = 1 then
raise_application_error(-20088, '该用户已锁定!');
elsif (v_money - p_money < 10) then
raise_application_error(-20089, '该用户余额不足!');
end if;
update t_account set u_money = u_money - p_money where u_id = p_from;
update t_account set u_money = u_money + p_money where u_id = p_to;
--通常是由存储过程提交事务
--如果存储过程不提交,则由调用方提交事务
--好处:可以将多个存储过程放在一个事务中,但通常一个存储过程就是处理一个单独的事务,所以在实际开发中,具体情况具体处理
--commit;
end;
相关文章推荐
- 调用存储过程,java中通过bboss persistent实现数据库存储过程的调用(位置变量绑定)
- java调用存储过程的三种方式(包含通过hibernate调用)
- 业务逻辑实现方式的讨论:存储过程 good or bad?
- 存储过程调用,java中通过bboss persistent实现数据库存储过程的调用(命名变量绑定)
- 通过COM组件方式实现java调用C#写的DLL文件
- 今天无意中发现JavaBean类基本都要求实现了Serializable接口,以前只是知道序列化以后,可以通过io流的方式将对象序列化和反序列化,进行存取,但不知道为什么需要序列化,今天总结一下
- 通过COM组件方式实现java调用C#写的DLL文件 转
- java下实现调用oracle的存储过程和函数
- Java+Selenium3框架设计准备篇2-TestNG通过变量实现启动不同浏览器测试
- java 实现WebService 以及不同的调用方式
- java 实现WebService 4000 以及不同的调用方式
- 银行转账业务场景的几种实现思路对比
- java 实现WebService 以及不同的调用方式
- 使用SQL SERVER存储过程实现银行转账业务
- 减少存储过程封装业务逻辑-web开发与传统软件开发的思维模式不同
- Android的布局管理器与java中的实现方式不同
- 体验 Java 并发 api,用不同方式实现信号量锁(Semaphore)(5)
- 基于JAVA语言实现银行简单ATM应用,通过console输入输出的行式模拟
- Java调用oracle存储过程通过游标返回临时表数据
- oracle实现存储过程的分页并用java代码调用存储过程