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语名执行成功,第二条出错,但第一个表的数据被删除了,但第二个表没删除,事务没有回滚
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语名执行成功,第二条出错,但第一个表的数据被删除了,但第二个表没删除,事务没有回滚
Connection connection = DriverManager.getConnection(sConnStr, user, pwd)这一行里的
connection是同一个吗?
把代码里的getConnection()换成这个connnection
如果这段代码里所有的connection指向是同一个connection的话,那就可能是
Statement st = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
这个地方的问题了,直接用connection.createStatement()试试。不过,个人感觉可能还是getConnection()和connection = DriverManager.getConnection(sConnStr, user, pwd)这两个connection不是同一个的问题,
要不楼主把getConnection()的代码贴出来看一下?
然后再executeUpdate(sql);
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());
}
}
..........
..........
}type=innodb;