StringBuffer sqldd = new StringBuffer("delete from dingdanb where ddh='");
sqldd.append(getDdh());
sqldd.append("'");
StringBuffer sqlDelete = new StringBuffer("delete from ghmxb where ddh='"); //清空购物车
sqlDelete.append(getDdh());
sqlDelete.append("'");
try {
Connection connection = DriverManager.getConnection(sConnStr, user, pwd)
getConnection().setAutoCommit(false);
Statement st = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);st.executeUpdate(sqldd.toString());
st.executeUpdate(sqlDelete.toString());
getConnection().commit();//提交JDBC事务
getConnection().setAutoCommit(true);// 恢复JDBC事务的默认提交方式
st.close();
//closeStatement();
closeConnection();
} catch (Exception e) {
try {
getConnection().rollback();
st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
//closeStatement();
closeConnection();
}
第一条sql语名执行成功,第二条出错,但第一个表的数据被删除了,但第二个表没删除,事务没有回滚

解决方案 »

  1.   

    你的代码里面的getConnection()取得的连接和
    Connection connection = DriverManager.getConnection(sConnStr, user, pwd)这一行里的
    connection是同一个吗?
    把代码里的getConnection()换成这个connnection
      

  2.   

    我用的数据库是mysql4.1.9  JDBC驱动是:3.1.8的。
      

  3.   

    可能是因为设置了 autoCommit 属性,如果这个属性为 true ,那么只要你执行操作,就直接更新到数据库了,你可以让这个值为 false ,如果事务成功,再提交。
      

  4.   

    感觉在提交后,设置autoCommit属性应该没有问题
    如果这段代码里所有的connection指向是同一个connection的话,那就可能是
    Statement st = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    这个地方的问题了,直接用connection.createStatement()试试。不过,个人感觉可能还是getConnection()和connection = DriverManager.getConnection(sConnStr, user, pwd)这两个connection不是同一个的问题,
    要不楼主把getConnection()的代码贴出来看一下?
      

  5.   

    autoCommit(false);
    然后再executeUpdate(sql);
      

  6.   

    private Connection connection = null;    public Connection getConnection() {
            return connection;
        }    public void setConnection(Connection connection) {
            this.connection = connection;
        }
    public void databaseConnection(){
            try {
                setConnection(DriverManager.getConnection(sConnStr, user, pwd)); //Connect to DBMS
                
            } catch (SQLException ex) {
                System.err.println("connection errer: " + ex.getMessage());
            }
        }
      

  7.   

    是不是我用的mysql4.1.9 和jdbc 3.1.8的版本的问题?还是什么问题?
      

  8.   

    而且要设置mysql 的 表的 type=InnoDB;这样才可以支持事务-------------------------------------create table aaa{
    ..........
    ..........
    }type=innodb;