zoukankan      html  css  js  c++  java
  • MySql 中的setAutoCommit方法

    引言


    setAutoCommit方法用一句话说就是用来保持事务完整性。一个系统的更新操作可能涉及多张表,这个时候,就须要用多个Sql语句来实现,实际上我认为这个东西就是用来实现事务的。


    当我们进行多条数据进行增删改的时候,一旦在一句sql中出现了错误,就会出现有部分数据已经成功。而后面的数据就没有办法运行。这个时候,就会出现脏数据。


    因此我们使用setAutoCommit方法,这种方法有一个參数。參数值为Boolean,当true的时候可启用自己主动提交模式,false可禁用该模式。


    凝视中有一句话是这样说的:Newlycreated Connection objects are in auto-commit mode by default, which means thatindividual SQL statements are committed automatically when the statement iscompleted. To be able to group SQL statements intotransactions and commit them or roll them back as a unit, auto-commit must bedisabled by calling the method setAutoCommit with false as its argument. Whenauto-commit is disabled, the user must call either the commit or rollbackmethod explicitly to end a transaction.翻译过来是这种:假设连接处于自己主动提交模式下。则其全部的SQL语句将作为单个事务执行并提交。否则,其SQL语句将作为事务组,直到调用Commit方法或rollback方法为止。

    默认情况下,新连接处于自己主动提交模式。


    简单来说,


    以下的代码:


    <span style="font-size:18px;">int[] result =null;
    	    con.setAutoCommit(false);   
    	    Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,   
    	                                       ResultSet.CONCUR_READ_ONLY);   
    	    String[] SqlString= null;
    	    for(String strvalue : SqlString){   	    	
    	       stmt.execute(strvalue);   
    	    }   
    	    con.commit(); 
    	    return result;</span>


    能够看到。假设代码没有出错,弹幕我们就运行Commit没有问题,可是一旦出错。我们应该运行数据的rollback,可是该代码没有进行处理。这个时候。就会出现锁,将表锁住。这个锁就没有机会释放。


    因此,我们应该这样写:


    <span style="font-size:18px;">    	 boolean result = false;
        	 try{
    		    con.setAutoCommit(false);   
    		    Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,   
    		                                       ResultSet.CONCUR_READ_ONLY);   
    		    String[] SqlString= null;
    		    for(String strvalue : SqlString){   	    	
    		    	result = stmt.execute(strvalue);   
    		    }   
    		    con.commit(); 
        	 }catch(Throwable e){
                 if(con!=null){
                     try {
                         con.rollback();
                     } catch (SQLException e1) {
                         e1.printStackTrace();
                     }
                 }
        	 }finally{
                 if(con!=null){
                     try {
                         con.close();
                     } catch (SQLException e) {
                         e.printStackTrace();
                     }
                 }
        	 }
    	    return result;</span>

    因此,我们一定不要小看了这个问题,这个问题一旦出现,性能就会收到非常大的影响。


    假设我们将SQL语句作为单个事务进行处理的话:


    <span style="font-size:18px;">  /**
         * 使用PreparedStatement加批量的方法
         * @return
         */
        public int[] executeUpdateMore(){    
        	int[] result=null;    	
        	try{   
        		PreparedStatement prest =con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
        	     for(List sqlValueString : sqlValue){    	    
        	    	 for(int i=0;i<sqlValueString.size();i++){
        	     		try {
        	     			prest.setObject(i+1,sqlValueString.get(i));
        	 			} catch (SQLException e) {
        	 				// TODO Auto-generated catch block
        	 				e.printStackTrace();
        	 			}    	 				   	 			
        	     	}
        	    	prest.addBatch();
        	     }
        	     prest.executeBatch();  
        	 /*    con.commit();*/   
        	     this.closeAll(con, prest, null);
        	} catch (SQLException ex){   
        	  Logger.getLogger(Dbhelper.class.getName()).log(Level.SEVERE, null,ex);   
        	} 
        	return result;       
        } </span>

    假设我们将SQL语句作为事务组来处理的话。我们就要这样写:


    <span style="font-size:18px;">    /**
         * 使用PreparedStatement加批量的方法,strvalue:
         * "INSERT INTOadlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3','localhost','20081009',8,'23123')"
         * @return
         * @throws SQLException 
         */
        public boolean executeUpdateMoreNotAuto() throws SQLException{   
        	
        	 boolean result = false;
        	 try{
    		    con.setAutoCommit(false);   
    		    Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,   
    		                                       ResultSet.CONCUR_READ_ONLY);   
    		    String[] SqlString= null;
    		    for(String strvalue : SqlString){   	    	
    		    	result = stmt.execute(strvalue);   
    		    }   
    		    con.commit(); 
        	 }catch(Throwable e){
                 if(con!=null){
                     try {
                         con.rollback();
                     } catch (SQLException e1) {
                         e1.printStackTrace();
                     }
                 }
        	 }finally{
                 if(con!=null){
                     try {
                         con.close();
                     } catch (SQLException e) {
                         e.printStackTrace();
                     }
                 }
        	 }
    	    return result;
        }</span>

    结束语:


    一定要记住处理完之后要提交或者回滚奥!





  • 相关阅读:
    iscsi序列一、搭建iscsi存储系统
    LVS系列三、LVS集群-IP-TUN模式
    LVS系列二、LVS集群-DR模式
    LVS系列一、LVS集群-NAT模式
    LINUX 的网站压力测试工具 webbench
    关于 /proc/sys/net/ipv4/下 文件的详细解释
    CentOS7+rsync+sersync实现数据实时同步
    Linux Shell 自动备份脚本
    Vim的强大配置文件
    nginx出现403 Forbidden解决方法
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7086585.html
Copyright © 2011-2022 走看看