事務控制好像有些問題,請幫我看看,行不?
try{
cp = ConnectionPool.getInstance();
con = cp.getConnection();
//開始事務處理
con.setAutoCommit(false);
stmt = con.createStatement();
stmt.addBatch("update Bom set SampleOrderId='" +
this.getSampleOrderId() + "',ColorDescription='" +
this.getColorDescription() + "',DesignDate='" +
this.getDesignDate() + "',spec1=" +
this.getSpec1() + ",spec2=" +
this.getSpec2() + ",spec3=" +
this.getSpec3() + ",lasteditedBy='" +
this.getLastEditedBy() + "',lasteditTime='" +
this.getLastEditTime() + "',remark='" +
this.getRemark() + "' where bomId='" +
this.getBomId() + "'");
//刪除材料項
stmt.addBatch("delete bomItem where bomid='" +
this.getBomId() + "'");
//增加材料項
if (this.getBomItems()!=null){
BomItem bomItem;
for (int i = 0; i < getBomItems().size(); i++) {
bomItem = (BomItem) getBomItems().elementAt(i);
stmt.addBatch("insert into BomItem (bomId,materialId,materialType,saving,paperPattern,length,width,quantity,amount,extraInfo) values ('" +
bomItem.getBomId() + "','" +
bomItem.getMaterialId() + "','" +
bomItem.getMaterialType().getId() + "','" +
bomItem.isSaving() + "','" +
bomItem.getPaperPattern() + "','" +
bomItem.getLength() + "','" +
bomItem.getWidth() +
"','" + bomItem.getQuantity() + "','" +
bomItem.getAmount() + "','" +
bomItem.getExtraInfo() + "')");
}
}
stmt.executeBatch();
//提交事務
con.commit();
} catch (SQLException sqle) {
System.out.println("Bom.java.update:" + sqle);
try {
//如在事務的處理過程當中出現數據庫錯誤,回退事務,解鎖數據表
con.rollback();
} catch (SQLException innerSql) {
System.out.println("rollback error:" + innerSql);
}
throw sqle;
} catch (Exception e) {
System.out.println("Bom.java.update:" + e);
try {
//如在事務的處理過程當中出現數據庫錯誤,回退事務,解鎖數據表
con.rollback();
} catch (SQLException innerSql) {
System.out.println("rollback error:" + innerSql);
}
throw e;
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.setAutoCommit(true);
//恢復自動提交
cp.releaseConnection(con);
}
} catch (SQLException sqle) {
System.out.println("Releasing resource error:" + sqle);
} catch (Exception e) {
System.out.println("Releasing resource error:" + e);
}
}
问题点数:0、回复次数:1Top
1 楼onenew(onenew)回复于 2004-09-03 00:02:06 得分 0
catch (Exception e) {
System.out.println("Bom.java.update:" + e);
try {
//如在事務的處理過程當中出現數據庫錯誤,回退事務,解鎖數據表
con.rollback();
} catch (SQLException innerSql) {
System.out.println("rollback error:" + innerSql);
}
throw e;
此部分不要 con.rollback();因为如果出现数据库错误不会进入到此部分,只有出现非数据库方面错误,才会进入到此处,而此时还未提交,因此根本就不要回滚。
另外
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.setAutoCommit(true);
//恢復自動提交
此处每一个都要分别写在try ..catch块中,试想如果stmt.close出错下面会不执行,con.setAutoCommit..出错下面也不执行。Top




