`
ygsilence
  • 浏览: 332301 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Connection.setAutoCommit使用的注意事项

    博客分类:
  • java
阅读更多


setAutoCommit总的来说就是保持数据的完整性,一个系统的更新操作可能要涉及多张表,需多个SQL语句进行操作

循环里连续的进行插入操作,如果你在开始时设置了:conn.setAutoCommit(false);
最后才进行conn.commit(),这样你即使插入的时候报错,修改的内容也不会提交到数据库,
而如果你没有手动的进行setAutoCommit(false);
出错时就会造成,前几条插入,后几条没有
会形成脏数据~~

setAutoCommit(false)的误用
(设定setAutoCommit(false)没有在catch中进行Connection的rollBack操作,操作的表就会被锁住,造成数据库死锁):
误用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.
JDBC的api文档是这样对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等,都一定要记住在处理完之后要提交或者回滚哦。

 

例子:

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 用于JDBC操作数据库的公共类
 */
public class CommonSql {
    /** 数据库连接对象 */
 private Connection conn;
 /** 数据库操作对象 */
 private PreparedStatement ps;
 /** 返回的数据结果集对象 */
 private ResultSet rs;

 /**
  * 测试数据库连接是否成功
  * @param args
  */
 /**
  * 打开数据库连接并创建数据库连接对象
  * @return boolean true:连接成功,false:连接失败
  */
 public boolean openConn() {
  Boolean isPassed = false;
  String driver = RWProperties.getProperty("driver", "com/test/config/db.properties");
  String url = RWProperties.getProperty("url", "com/test/config/db.properties");
  String user = RWProperties.getProperty("user", "com/test/config/db.properties");
  String pwd = RWProperties.getProperty("pwd", "com/test/config/db.properties");

  try {
   Class.forName(driver);
   conn = DriverManager.getConnection(url, user, pwd);
   isPassed = true;
  } catch (ClassNotFoundException e) {
   closeAll();
   e.printStackTrace();
   System.out.println("数据库连接失败!");
  } catch (Exception e) {
   closeAll();
   e.printStackTrace();
   System.out.println("数据库连接失败!");
  }

  return isPassed;
 }
 /**
  * 执行数据库的新增和修改语句,只操作一张表
  * @param sql 要执行的SQL语句
  * @return boolean true:执行成功,false:执行失败
  */
 /**
     * 执行数据库的新增和修改语句,同时操作多张表
     * @param sql 要执行的SQL语句的字符串数组
     * @return boolean true:执行成功,false:执行失败
     */
 public boolean execUpdate(String[] sql) {
  boolean isPassed = false;
  // 判断连接数据库是否成功
  if (openConn()) {
   try {
    conn.setAutoCommit(false);
    for (int i = 0; i < sql.length; i++) {
     ps = conn.prepareStatement(sql[i]);
     ps.executeUpdate();
    }
    conn.commit();
    isPassed = true;
   } catch (SQLException e) {
    try {
     conn.rollback();
    } catch (SQLException e1) {
     e1.printStackTrace();
    }
    for (int i = 0; i < sql.length; i++) {
     System.out.println("SQL:"+sql[i]);
    }
    e.printStackTrace();
   } finally {
    closeAll();
   }
  } else {
   closeAll();
   for (int i = 0; i < sql.length; i++) {
    System.out.println(sql[i]);
   }
   System.out.println("数据库连接失败!");
  }

  return isPassed;
 }
    /**
     * 执行数据库的新增和修改语句,同时操作多张表
     * @param sql 要执行的SQL语句的集合
     * @return boolean true:执行成功,false:执行失败
     */
 public boolean execUpdate(List<String> sql) {
  boolean isPassed = false;
  // 判断连接数据库是否成功
  if (openConn()) {
   try {
    conn.setAutoCommit(false);
    for (int i = 0; i < sql.size(); i++) {
     ps = conn.prepareStatement(sql.get(i));
     ps.executeUpdate();
    }
    conn.commit();
    isPassed = true;
   } catch (SQLException e) {
    try {
     conn.rollback();
    } catch (SQLException e1) {
     e1.printStackTrace();
    }
    for (int i = 0; i < sql.size(); i++) {
     System.out.println("SQL:"+sql.get(i));
    }
    e.printStackTrace();
   } finally {
    closeAll();
   }
  } else {
   closeAll();
   for (int i = 0; i < sql.size(); i++) {
    System.out.println(sql.get(i));
   }
   System.out.println("数据库连接失败!");
  }

  return isPassed;
 }
    /**
     * 执行数据库查询操作
     * @param sql 要执行的SQL语句
     * @return ResultSet 返回查询的结果集对象
     */
 public ResultSet execQuery(String sql) {
     rs = null;
     // 判断连接数据库是否成功
  if (openConn()) {
   try {
    ps = conn.prepareStatement(sql);
    rs = ps.executeQuery();
   } catch (SQLException e) {
    closeAll();
    System.out.println("SQL:"+sql);
    e.printStackTrace();
   }
  } else {
   closeAll();
   System.out.println("SQL:"+sql);
   System.out.println("数据库连接失败!");
  }

  return rs;
 }
 /**
  * 关闭所有数据库连接对象
  */
 public void closeAll() {
  if (conn != null) {
   try {
    conn.close();
    conn = null;
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  if (ps != null) {
   try {
    ps.close();
    ps = null;
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  if (rs != null) {
   try {
    rs.close();
    rs = null;
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
}


分享到:
评论
1 楼 327764984 2017-01-20  
CommonSql里
不知道把PreparedStatment和ResultSet作为实例属性保存的意义何在 它们既没有被复用 也没有被管理销毁 却在一次查询后一度持有引用阻止被GC
从生命周期角度 ResultSet短于PreparedStatment短于Connection 全部关闭操作也不是按这个顺序

相关推荐

    采用C3P0写的数据库连接池

    Connection conn = ConnectionManager.getInstance().getConnection();这样来获得conn实体对象,然后就可以应用了。 conn.setAutoCommit(false);用事务提交 最后conn.commit(); conn.close();就可以了。

    携程网数据建表SWAP.PDF

    Connection con=ConnectionFactory.getConnection(); try{ con.setAutoCommit(false); st=con.createStatement(); String sql="insert into dept(name) values('gaoyajun')"; int i=st....

    MySQL数据库

    connection.setAutoCommit(false/true); 2. 提交事务 connection.commit(); 3. 回滚 connection.rollback(); - 实现转账: 超人 500 蝙蝠侠 5000 蝙蝠侠给超人转2000 执行第一次成功 执行第二次成功 执行第三...

    JDBC事务处理机制探秘

    1、JavaBean中使用JDBC事务处理 在JDBC中怎样将多个SQL语句组合成一个事务呢?在JDBC中,打开一个连接对象Connection时,缺省是auto-commit模式,每个SQL语句都被当作一个事务,即每次执行一个语句,都会自动的得到...

    java开发中的一些常用小技巧

    用JDBC时的一些注意事项 1.多使用PreparedStatement代替Statement这样可以避免在拼接字符串的时候出现 "select * from emp where name = '"+name+"'"单引号过多的情况 2 sql语句过长的时候尽量避免使用sql="str1"+...

    连接sql数据库的jdbc类

    public class JDBConnection { private final String ... con.setAutoCommit(true); } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("creatConnectionError!"); }

    day22--事务2

    }}DBUtils事务操作Connection对象的方法名描述conn.setAutoCommit(false)开启事务new QueryRunner()创建核

    mysql-connector-java-5.1.46-bin

    2.建立连接 使用connection对象的getConnection方法   url(jdbc:oracle:thin:@ip:端口:数据库sid),  user,password,如果要手动提交,调用conn.setAutoCommit(false) 3.创建statement对象 一个statement对象只能...

    Oracle addBatch()用法实例详解

    Oracle addBatch()用法实例详解 PreparedStatement.addbatch()的使用 Statement和...connection.setAutoCommit(false); 3.预编译SQL语句,只编译一回哦,效率高啊 PreparedStatement statement = co

    共享数据库连接池HTTPSQL RADServer.rar

    RadSqlCmd radSqlCmd = RadSqlCmd.setAutoCommit(false); argsList = new ArrayList(); argsList.add("%2%"); System.out.println( radSqlCmd.sqlList("select * from ztest where tname like ?", args...

    mySQL事务处理

    public static void StartTransaction(Connection con, String[] sqls) throws Exception { if (sqls == null) { return; } Statement sm = null; try { // 事务开始 System.out.println("事务处理...

    火车票管理系统

    import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Iterator...

    数据库连接,里面有各种数据库的连接方法

    //con.setAutoCommit(false);//添加事物,既是否自动提交 stmt=con.createStatement(); } catch (SQLException e) { e.printStackTrace(); } } public Statement getStmt(){ return stmt; } public ...

    servlet制作网站

    private Connection conn = null; { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("创建驱动失败"); e.printStackTrace(); } try {...

    jdbc连接数据库的方式2

    为了获得更好的性能,可以通过调用带布尔值false参数的Connection类的setAutoCommit()方法关闭自动提交功能,如下所示:  conn.setAutoCommit(false);  值得注意的是,一旦关闭了自动提交功能,我们就需要通过调用...

    JSP 多条SQL语句同时执行的方法

    代码如下:con.setAutoCommit(true);//设为true,每次executeUpdate将立刻被执行 sql = “insert into table1(lable1) values(‘001’)”; rs = stmt.executeUpdate(sql); sql = “insert into table2(lable2) values...

    jdbc基础和参考

    Connection.setAutoCommit(false); 正常: conn.commit(); 异常: conn.rollback(); JDBC批处理: addBatch executeBatch Statement PreparedStatement Statement{ 1.获取连接 getConnection(); 2.创建...

    servlet+jsp+javaBean开发的网站书店(完整源码)

    dbUtil.getCon().setAutoCommit(false); //将订单中的数据录入数据库 pstmt_order = dbUtil.getCon().prepareStatement(sql_order); pstmt_order.setString(1, order.getUser().getName()); pstmt_...

    java业务层框架开发ibatis(java源码)

    import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.Savepoint; import java.sql....

Global site tag (gtag.js) - Google Analytics