诡异的 rollback() should not be called while in auto-commit mode.
那位大侠帮我看看下面这段代码,为什么会抛出“ rollback() should not be called while in auto-commit mode.”异常,而且数据库中的数据已被修改,这样还抛异常
public String deleCTypeById(int typeid){
String message="删除失败";
PreparedStatement pstmt = null;
String sql=" ";
sql="delete from ContractType where id=?";
try{
this.con=DBConnection.getConnection();
con.setAutoCommit(false);
pstmt=con.prepareStatement(sql);
if(this.typeIsUse(typeid)){//判断要删除的类型是否在使用中,如果在使用就不能删除
return "类型正在使用,不能删除";
}else{
pstmt.setInt(1, typeid);
pstmt.executeUpdate();
}
con.commit();
message="删除成功";
return message;
}catch(Exception e){
try{
con.rollback();
}catch(SQLException ex){
ex.printStackTrace();
}
e.printStackTrace();
return "数据库异常";
}
finally{
try{
if(pstmt!=null){pstmt.close(); }
if(con!=null){con.close(); }
}catch(SQLException se){
se.printStackTrace();
}
}
//return message;
}