Java事务问题,请各位路过的兄弟帮忙,多谢!

Esengnet 2007-09-26 09:26:04
java前端:
try{
Connection = dataSource.getConnection();
connection.setAutocommit(false);
CallateStatement cs = connection.prepareCall("{call sp_test}");
cs.executeUpdate()
con.commit();//如果把这句修改成 con.rollback; 但是数据库的数据依然修改了,都不能回滚数据
}catch(SQException e){
con.rollback;
System.out.println("Exception:"+e.getMessage());
}finally{
cs.close();
connection.close();
}
-----------------------------------------------------------------
数据库存储过程:
create procedure sp_test
as
begin
create table #test(id int, name varchar(20));
begin tran
insert #test select id,name from test
update test_a
set test_a.id = 20
from test_a,#test
where test_a.id=#test.id
if @error != 0
begin
rollback tran
return
end

update test_a
set test_a.name = 'test'
from test_a,#test
where test_a.id=#test.id
if @error != 0
begin
rollback tran
return
end
commit tran
drop table #test
end

-----------------------------------------------------------------


...全文
101 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wshsm 2007-09-27
  • 打赏
  • 举报
回复
是啊,存储过程里面都commit了,还怎么rollback?
nanjg 2007-09-27
  • 打赏
  • 举报
回复
connection.setAutocommit(true);+在con.commit();之后
ys_wangtao 2007-09-27
  • 打赏
  • 举报
回复
存储过程里已经有事务了,没必要在程序里再写了吧
qybao 2007-09-26
  • 打赏
  • 举报
回复
sp_test存储过程内部就commit了
外部rollback也已经不顶用了
就好像你写sql语句在数据库端执行
update xxx set yyy=zzz
commit
然后你再rollback,这时rollback已经无法回滚被commit的事务了

Esengnet 2007-09-26
  • 打赏
  • 举报
回复
不好意思写错了一点: connection与con是一个变量,可以理解Connection对象全部指向con or connection.

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧