CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web 开发

事務控制好像有些問題,請幫我看看,行不?

楼主lovelinforever(我喜歡你,你喜歡我嗎---)2004-09-02 23:28:59 在 Java / Web 开发 提问

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

相关问题

  • datawindow可不可以控制哪一行可更改或不可更,现在好像只能控制到列?
  • Page_Load和Page_UnLoad好像没有执行?怎么回事啊?
  • 请问oracle的事务控制怎么进行?急
  • (fzymr) asp.net中怎样进行事务控制?!(C#)
  • 事务控制会影响执行效率吗?
  • ORACLE事务控制问题
  • vb中timer控件如何控制指定时间执行事件
  • 哪位老大能不能告之:ODAC怎么进行事务控制?
  • 利用鼠标点击事件控制表格行的背景问题?
  • 紧急事件:如何控制对ListView第三列进行修改!!!

关键词

  • bomitem
  • getspec
  • spec
  • stmt
  • con

得分解答快速导航

  • 帖主:lovelinforever

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo