读书人

Java事务有关问题,请各位路过的兄弟帮

发布时间: 2012-01-08 22:48:50 作者: rapoo

Java事务问题,请各位路过的兄弟帮忙,多谢!
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

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




[解决办法]
sp_test存储过程内部就commit了
外部rollback也已经不顶用了
就好像你写sql语句在数据库端执行
update xxx set yyy=zzz
commit
然后你再rollback,这时rollback已经无法回滚被commit的事务了


[解决办法]
存储过程里已经有事务了,没必要在程序里再写了吧
[解决办法]
connection.setAutocommit(true);+在con.commit();之后
[解决办法]
是啊,存储过程里面都commit了,还怎么rollback?

读书人网 >Java Web开发

热点推荐