当前位置: 首页 > news >正文

做不锈钢百度网站哪个比较好/最新新闻事件

做不锈钢百度网站哪个比较好,最新新闻事件,四川网站建设方案,哪里有网站开发企业误用Connection.setAutoCommit导致的数据库死锁问题 http://www.javaeye.com/topic/52044 误用Connection.setAutoCommit导致的数据库死锁问题。 系统在发布到用户的的服务器了,运行一段时间偶尔出现某些功能不能正常运行,甚至不能自动恢复,严…

误用Connection.setAutoCommit导致的数据库死锁问题

http://www.javaeye.com/topic/52044

 

 误用Connection.setAutoCommit导致的数据库死锁问题。

系统在发布到用户的的服务器了,运行一段时间偶尔出现某些功能不能正常运行,甚至不能自动恢复,严重导致服务器当机,表现为服务器不响应用户的请求,数据库有大量的锁。在服务器重启后才能恢复正常。今天通遍的查看了一下代码,初步分析了原因,记录了下来,跟大家交流交流。
先看下面一段有问题的代码:
1       Connection con = null;
2      try{
3          con = getConnection();
4          con.setAutoCommit(false);
           /*
5          * update USER set name=’winson’ where id=’000001’;
            */
6          con.commit();
7       }finally{
8          if(con!=null){
9              try {
10                 con.close();
11             } catch (SQLException e) {
12                 e.printStackTrace();
13             }
           }
       }
分析:问题就出现在第4行,写代码的人把数据库连接con 设置成非自动提交,但没有在执行出现异常的时候进行回滚。如果在执行第5行的时候出现异常,con既没有提交也没有回滚,表USER就会被锁住(如果oracle数据库就是行锁),而这个锁却没有机会释放。有人会质疑,在执行con.close()的时候不会释放锁吗?因为如果应用服务器使用了数据库连接池,连接不会被断开。
附:在oracle上查看锁的方法:select * from v$lock_object或者select * from v$lock.
JDBCapi文档是这样对setAutoCommit方法解释的:
Sets the connection's auto-commit mode to enableAutoCommit.
      Newly created Connection objects are in auto-commit mode by default, which means that individual SQL statements are committed automatically when the statement is completed. To be able to group SQL statements into transactions and commit them or roll them back as a unit, auto-commit must be disabled by calling the method setAutoCommit with false as its argument. When auto-commit is disabled, the user must call either the commit or rollback method explicitly to end a transaction.(一定不能大意哦,如果设置成非自动提交,在最后一定要调用commit或者rollback方法)
      The commit occurs when the statement completes or the next execute occurs, whichever comes first. In the case of statements returning a ResultSet object, the statement completes when the last row of the result set has been retrieved or the ResultSet object has been closed. In advanced cases, a single statement may return multiple results as well as output parameter values. In this case, the commit may occur when all results and output parameter values have been retrieved, or the commit may occur after each result is retrieved.
参考正确的写法应该是:
        Connection con = null;
       try{
           con = getConnection();
           con.setAutoCommit(false);
           /*
            * do what you want here.
            */
           con.commit();
        }catch(Throwable e){
           if(con!=null){
               try {
                   con.rollback();
               } catch (SQLException e1) {
                   e1.printStackTrace();
               }
           }
throw new RuntimeException(e);
        }finally{
           if(con!=null){
               try {
                   con.close();
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
       }
这种疏忽很容易出现,但又导致非常严重的运行问题。所以在这里作个提醒,以后在处理外部资源的时候一定要格外小心。今天还发现代码中一些地方滥用synchronized关键字,导致系统性能受到很大的影响,处理不当跟前面提到问题一起出现,那系统就是时候over了。
另外,如果不是自己来处理事务,可能在用hibernate或者ejb等,都一定要记住在处理完之后要提交或者回滚哦。

 

转载于:https://www.cnblogs.com/hellofei/archive/2010/03/03/1677202.html

http://www.jmfq.cn/news/4847959.html

相关文章:

  • 物联网的网络架构/德兴网站seo
  • 安徽省建设部干部网站/域名归属查询
  • 中山百度网站排名/长尾词挖掘免费工具
  • 一起做玩具网站/企业网站排名优化
  • 阳泉建设局网站/seo运营专员
  • 怎么用html做移动网站/百度权重提升
  • 汉鼎中国 网站建设/西安疫情最新数据消息5分钟前
  • 国家建设网站/营销网络推广方式有哪些
  • 怎样推广广告/山东seo多少钱
  • 保定比较好的网站建设公司/平台软件定制开发
  • 国内很多网站不是响应式/市场调查报告
  • 网站开发建设及推广合同/推广策略有哪些方法
  • 直播app定制开发/成都关键词优化平台
  • 武汉集团网站建设/北京网站推广排名外包
  • wordpress修改固定链接打不开/天津seo培训机构
  • 烟台网络公司员工人数/搜索引擎优化简称
  • 网站案例分析/中山排名推广
  • 网站托管主要干点什么/网络营销是什么工作主要干啥
  • 眉山政府网站建设/北京百度总部电话
  • 购物网站免费模板/拼多多关键词排名查询软件
  • 甘肃省人民政府官网首页/百度seo竞价推广是什么
  • 泸州网站公司/做一个官网要多少钱
  • wordpress 知名站点/网络营销专业学什么课程
  • 中企动力科技股份有限公司干嘛的/宁波seo教学
  • 学习网站建设总结/厦门谷歌seo
  • 顺企网上海网站建设/深圳网络推广服务是什么
  • 小程序开发平台需要网站吗/营销培训总结
  • 企业网站pv是什么/企排排官网
  • 网站优化公司/杭州网站关键词排名
  • 互联网专业主要学什么/自己怎么做网站优化