hibernate 批量插入資料

來源:互聯網
上載者:User

標籤:style   blog   java   color   使用   os   檔案   資料   

如題,有兩種方法

1)使用FLUSH

2)使用JDBC

 

分別來解釋:

1)hibernate在進行資料庫操作的時候,都要有事務支援的。可能你曾遇到過,沒有加事務,程式會報錯的情況。

    而事務每次提交的時候,都會和資料庫互動,即讓資料庫執行SQL語句。

     在說到hibernate中的save() 或者saveOrUpdate()方法,其實,它們都是利用hibernate的一級緩衝,在事務沒有提交的時候,所有對象,並沒有寫入資料庫。而是儲存在記憶體中。在事務提交的時候,hibernate會把這些對象持久化到資料庫中。另一方面,hibernate提供了一個顯式的API來強制寫資料庫。就是FLUSH。當程式執行session.flush(),就會持久化資料,而不必等到事務提交時才執行。

     本人寫了一個DEMO,一個線程產生USER,一個儲存USER。

生產者代碼如下:

package com.baidu.test;import java.util.ArrayList;import java.util.Collections;import java.util.List;import com.baidu.model.User;public class test extends Thread {    public static int count = 0;    public static List<User> userlist = Collections.synchronizedList(new ArrayList());     public User user;    public static void main(String[] args) {        new test().start();        new HandleThread().start();    }    public static int usercount=0;    @Override    public void run() {        while (true) {            try {                Thread.sleep(2000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            User t = new User();            t.setId(usercount);            t.setUsername("name_" + usercount);            userlist.add(t);            System.out.println("生產出一個user_"+usercount);            usercount++;        }    }}

消費者代碼如下:

package com.baidu.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.baidu.model.User;public class HandleThread extends Thread {    @Override    public void run() {        boolean flag=false;        Configuration config = new Configuration();        config.configure();        SessionFactory factory = config.buildSessionFactory();        Session session = factory.openSession();        while (true) {            if (test.userlist.size() > 0) {                for (int i = 0; i < test.userlist.size(); i++) {                    System.out.println("處理了"                            + test.userlist.get(i).getId());                    insert(test.userlist.get(i),session);                }                test.userlist.clear();            }            try {                Thread.sleep(3000);            } catch (InterruptedException e) {                e.printStackTrace();            }            if(flag)                break;        }        session.close();    }    public void insert(User user,Session session) {            Transaction tran = session.beginTransaction();        session.save(user);        if (test.count++ % 10 == 0) {            System.out.println(test.count);            session.flush();            session.clear();        }        tran.commit();            }}
        if (test.count++ % 10 == 0) {            System.out.println(test.count);            session.flush();            session.clear();        }
這段代碼就是執行大量操作的核心。當然這個需要在hibernate設定檔中配置下 
<property name="hibernate.jdbc.batch_size">10</property>
這個值,從網上得到的說法是一次推送SQL語句的條數。暫且相信了,後續我將驗證(通過抓包)。

至此,第一種批量處理已經完成。

2) to be continue...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.