日期:2014-05-18  浏览次数:20689 次

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?