乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Hibernate批量插入數(shù)據(jù)

       Blex 2011-05-02

      使用Hibernate將 100 000 條記錄插入到數(shù)據(jù)庫(kù)的一個(gè)很自然的做法可能是這樣的

      Session session = sessionFactory.openSession();
      Transaction tx = session.beginTransaction();
      for ( int i=0; i<100000; i++ ) {
      Customer customer = new Customer(.....);
      session.save(customer);
      }
      tx.commit();
      session.close();

      這段程序大概運(yùn)行到 50 000 條記錄左右會(huì)失敗并拋出 內(nèi)存溢出異常(OutOfMemoryException) 。 這是因?yàn)?Hibernate 把所有新插入的 客戶(hù)(Customer)實(shí)例在 session級(jí)別的緩存區(qū)進(jìn)行了緩存的緣故。

      我們會(huì)在本章告訴你如何避免此類(lèi)問(wèn)題。首先,如果你要執(zhí)行批量處理并且想要達(dá)到一個(gè)理想的性能, 那么使用JDBC的批量(batching)功能是至關(guān)重要。將JDBC的批量抓取數(shù)量(batch size)參數(shù)設(shè)置到一個(gè)合適值 (比如,10-50之間):

      hibernate.jdbc.batch_size 20

      注意,假若你使用了identiy標(biāo)識(shí)符生成器,Hibernate在JDBC級(jí)別透明的關(guān)閉插入語(yǔ)句的批量執(zhí)行。

      你也可能想在執(zhí)行批量處理時(shí)關(guān)閉二級(jí)緩存:

      hibernate.cache.use_second_level_cache false

      但是,這不是絕對(duì)必須的,因?yàn)槲覀兛梢燥@式設(shè)置CacheMode來(lái)關(guān)閉與二級(jí)緩存的交互。

      13.1. 批量插入(Batch inserts)

      如果要將很多對(duì)象持久化,你必須通過(guò)經(jīng)常的調(diào)用 flush() 以及稍后調(diào)用 clear() 來(lái)控制第一級(jí)緩存的大小。

      Session session = sessionFactory.openSession();
      Transaction tx = session.beginTransaction();
      for ( int i=0; i<100000; i++ ) {
      Customer customer = new Customer(.....);
      session.save(customer);
      if ( i % 20 == 0 ) { //20, same as the JDBC batch size //20,與JDBC批量設(shè)置相同
      //flush a batch of inserts and release memory:
      //將本批插入的對(duì)象立即寫(xiě)入數(shù)據(jù)庫(kù)并釋放內(nèi)存
      session.flush();
      session.clear();
      }
      }
      tx.commit();
      session.close();
       

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多