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
-----------------------------------------------------------------