JDBC的PreparedStatement啓動事務使用批處理executeBatch()

JDBC使用MySQL處理大數據的時候,天然而然的想到要使用批處理,html

普通的執行過程是:每處理一條數據,就訪問一次數據庫;mysql

而批處理是:累積到必定數量,再一次性提交到數據庫,減小了與數據庫的交互次數,因此效率會大大提升sql

至於事務:事務指邏輯上的一組操做,組成這組操做的各個單元,要不所有成功,要不所有不成功,默認是關閉事務的。數據庫

              更多事務的資料,請參考這裏:http://blog.csdn.net/caomiao2006/article/details/22412755ide

 

1. PreparedStatement使用批處理 executeBatch()

       1.1. 不使用executeBatch(),而使用executeUpdate()

          代碼以下:  post

         Class.forName("com.mysql.jdbc.Driver");
         Connection conn = DriverManager.getConnection(dbUrl, user, password);
         PreparedStatement pstmt = conn.prepareStatement("update content set introtext=? where id=?");大數據

         for(int i=0; i<10000; i++){
             pstmt.setString(1, "abc"+i);
             pstmt.setInt(2, id);
             pstmt.executeUpdate();
         }url

         這樣,更新10000條數據,就得訪問數據庫10000次spa

      1.2. 而使用executeBatch()

          代碼以下:.net

         Class.forName("com.mysql.jdbc.Driver");
         Connection conn = DriverManager.getConnection(dbUrl, user, password);
         PreparedStatement pstmt = conn.prepareStatement("update content set introtext=? where id=?");

         for(int i=0; i<10000; i++){
             pstmt.setString(1, "abc"+i);
             pstmt.setInt(2, id);
             pstmt.addBatch();//添加到同一個批處理中
         }

         pstmt.executeBatch();//執行批處理


 

         注意:1. 若是使用了 addBatch() -> executeBatch() 仍是很慢,那就得使用到這個參數了

                      rewriteBatchedStatements=true (啓動批處理操做)

                      在數據庫鏈接URL後面加上這個參數:      

                          String dbUrl =  "jdbc:mysql://localhost:3306/User? rewriteBatchedStatements=true";

                      2. 在代碼中,pstmt的位置不能亂放,

                          //必須放在循環體外

                     pstmt = conn.prepareStatement("update content set introtext=? where id=?");

                     for(int i=0; i<10000; i++){

                           //放這裏,批處理會執行不了,由於每次循環從新生成了pstmt,不是同一個了

                           //pstmt = conn.prepareStatement("update content set introtext=? where id=?");
                           pstmt.setString(1, "abc"+i);
                           pstmt.setInt(2, id);
                           pstmt.addBatch();//添加到同一個批處理中
                     }

                     pstmt.executeBatch();//執行批處理

2. 啓用事務處理

          Class.forName("com.mysql.jdbc.Driver");

 

          Connection conn = DriverManager.getConnection(dbUrl, user, password);

          conn.setAutoCommit(false);//將自動提交關閉
          PreparedStatement pstmt = conn.prepareStatement("update content set introtext=? where id=?");
          pstmt.setString(1, tempintrotext);
          pstmt.setInt(2, id);
          pstmt.addBatch();
          pstmt.executeBatch();
          pstmt.close();


          conn.commit();//執行完後,手動提交事務
          conn.setAutoCommit(true);//再把自動提交打開,避免影響其餘須要自動提交的操做
          conn.close();

3. 事務和批處理混合使用      

          Class.forName("com.mysql.jdbc.Driver");

          Connection conn = DriverManager.getConnection(dbUrl, user, password);

          conn.setAutoCommit(false);//將自動提交關閉
          PreparedStatement pstmt = conn.prepareStatement("update content set introtext=? where id=?");

          for(int i=0; i<1000000; i++){
               pstmt.setString(1, tempintrotext);
               pstmt.setInt(2, id);
               pstmt.addBatch();

               //每500條執行一次,避免內存不夠的狀況,可參考,Eclipse設置JVM的內存參數

               if(i>0 && i%500==0){
                    pstmt.executeBatch();

                    //若是不想出錯後,徹底沒保留數據,則能夠每執行一次提交一次,但得保證數據不會重複

                    conn.commit();

                }

         }
          pstmt.executeBatch();//執行最後剩下不夠500條的
          pstmt.close();

          conn.commit();//執行完後,手動提交事務
          conn.setAutoCommit(true);//再把自動提交打開,避免影響其餘須要自動提交的操做
          conn.close();

 較完整的代碼:

 1 public class ExecuteBatchTest {
 2     private Connection conn;
 3     private PreparedStatement pstmt;
 4     private PreparedStatement pstmt2;
 5     private ResultSet rs;
 6     private String user = "root";
 7     private String password = "123456";
 8     private String dbUrl = "jdbc:mysql://localhost:3306/user?rewriteBatchedStatements=true";
 9     private int limitNum = 10000;
10 
11     public void changeData() {
12         try {
13             Class.forName("com.mysql.jdbc.Driver");
14             conn = DriverManager.getConnection(dbUrl, user, password);
15             
16             //既不用batch,也不用事務
17             testBatch(false,false);
18             //只用batch, 不用事務
19             testBatch(false,true);
20             //只用事務,不用batch
21             testBatch(true,false);
22             //不只用事務,還用batch
23             testBatch(true,true);
24             
25             pstmt.close();
26             conn.close();
27         } catch (ClassNotFoundException e) {
28             e.printStackTrace();
29         } catch (SQLException e) {
30             e.printStackTrace();
31         }
32     }
33     
34     public void testBatch(Boolean openTransaction, Boolean useBatch) throws SQLException{
35         if(openTransaction)
36             conn.setAutoCommit(false);
37         
38         if(pstmt!=null){
39             pstmt.clearParameters();
40             pstmt.clearBatch();
41         }
42         
43         pstmt = conn.prepareStatement("insert into person (name) values (?)");
44         long start = System.currentTimeMillis();
45         for(int a = 0;a<limitNum;a++){
46             String name = "tommy"+a;
47             pstmt.setString(1, name);
48             if(useBatch)
49                 pstmt.addBatch();
50             else
51                 pstmt.executeUpdate();
52         }
53         
54         if(useBatch)
55            pstmt.executeBatch();
56          
57         if(openTransaction){
58             conn.commit();
59             conn.setAutoCommit(true);
60         }
61         long end = System.currentTimeMillis();
62         System.out.println("use time:"+(end-start)+" ms");
63         
64     }
65     
66     //main method
67     publi static void main(String[] args){
68         ExecuteBatchTest ebt = new ExecuteBatchTest();
69         ebt.changeData();
70     }
71        
72 }
View Code

運行結果:

   分別是: 不用批處理,不用事務;

               只用批處理,不用事務;

               只用事務,不用批處理;

               既用事務,也用批處理;(很明顯,這個最快,因此建議在處理大批量的數據時,同時使用批處理和事務)

相關文章
相關標籤/搜索