⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batch.html

📁 是一个中文的Hibernate库文档
💻 HTML
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;14&nbsp;&#31456;&nbsp;&#25209;&#37327;&#22788;&#29702;&#65288;Batch processing&#65289;</title><link rel="stylesheet" href="../shared/css/html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="up" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="previous" href="events.html" title="&#31532;&nbsp;13&nbsp;&#31456;&nbsp;&#xA;	&#25318;&#25130;&#22120;&#19982;&#20107;&#20214;(Interceptors and events)&#xA;	"><link rel="next" href="queryhql.html" title="&#31532;&nbsp;15&nbsp;&#31456;&nbsp;HQL: Hibernate&#26597;&#35810;&#35821;&#35328;"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">&#31532;&nbsp;14&nbsp;&#31456;&nbsp;&#25209;&#37327;&#22788;&#29702;&#65288;Batch processing&#65289;</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="events.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="queryhql.html">&#19979;&#19968;&#39029;</a></td></tr></table><hr></div><div class="chapter" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title"><a name="batch"></a>&#31532;&nbsp;14&nbsp;&#31456;&nbsp;&#25209;&#37327;&#22788;&#29702;&#65288;Batch processing&#65289;</h2></div></div><div></div></div><p>        &#20351;&#29992;Hibernate&#23558; 100 000 &#26465;&#35760;&#24405;&#25554;&#20837;&#21040;&#25968;&#25454;&#24211;&#30340;&#19968;&#20010;&#24456;&#33258;&#28982;&#30340;&#20570;&#27861;&#21487;&#33021;&#26159;&#36825;&#26679;&#30340;    </p><pre class="programlisting">Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();for ( int i=0; i&lt;100000; i++ ) {    Customer customer = new Customer(.....);    session.save(customer);}tx.commit();session.close();</pre><p>        &#36825;&#27573;&#31243;&#24207;&#22823;&#27010;&#36816;&#34892;&#21040; 50 000 &#26465;&#35760;&#24405;&#24038;&#21491;&#20250;&#22833;&#36133;&#24182;&#25243;&#20986; <tt class="literal">&#20869;&#23384;&#28322;&#20986;&#24322;&#24120;&#65288;OutOfMemoryException&#65289;</tt> &#12290;        &#36825;&#26159;&#22240;&#20026; Hibernate &#25226;&#25152;&#26377;&#26032;&#25554;&#20837;&#30340; <tt class="literal">&#23458;&#25143;&#65288;Customer&#65289;</tt>&#23454;&#20363;&#22312; session&#32423;&#21035;&#30340;&#32531;&#23384;&#21306;&#36827;&#34892;&#20102;&#32531;&#23384;&#30340;&#32536;&#25925;&#12290;    </p><p>        &#25105;&#20204;&#20250;&#22312;&#26412;&#31456;&#21578;&#35785;&#20320;&#22914;&#20309;&#36991;&#20813;&#27492;&#31867;&#38382;&#39064;&#12290;&#39318;&#20808;&#65292;&#22914;&#26524;&#20320;&#35201;&#25191;&#34892;&#25209;&#37327;&#22788;&#29702;&#24182;&#19988;&#24819;&#35201;&#36798;&#21040;&#19968;&#20010;&#29702;&#24819;&#30340;&#24615;&#33021;&#65292;        &#37027;&#20040;&#20351;&#29992;JDBC&#30340;&#25209;&#37327;&#65288;batching&#65289;&#21151;&#33021;&#26159;&#33267;&#20851;&#37325;&#35201;&#12290;&#23558;JDBC&#30340;&#25209;&#37327;&#25235;&#21462;&#25968;&#37327;&#65288;batch size&#65289;&#21442;&#25968;&#35774;&#32622;&#21040;&#19968;&#20010;&#21512;&#36866;&#20540;        &#65288;&#27604;&#22914;&#65292;10-50&#20043;&#38388;&#65289;&#65306;    </p><pre class="programlisting">hibernate.jdbc.batch_size 20</pre><p>        &#20320;&#20063;&#21487;&#33021;&#24819;&#22312;&#25191;&#34892;&#25209;&#37327;&#22788;&#29702;&#26102;&#20851;&#38381;&#20108;&#32423;&#32531;&#23384;&#65306;    </p><pre class="programlisting">hibernate.cache.use_second_level_cache false</pre><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="batch-inserts"></a>14.1.&nbsp;&#25209;&#37327;&#25554;&#20837;&#65288;Batch inserts&#65289;</h2></div></div><div></div></div><p>            &#22914;&#26524;&#35201;&#23558;&#24456;&#22810;&#23545;&#35937;&#25345;&#20037;&#21270;&#65292;&#20320;&#24517;&#39035;&#36890;&#36807;&#32463;&#24120;&#30340;&#35843;&#29992; <tt class="literal">flush()</tt> &#20197;&#21450;&#31245;&#21518;&#35843;&#29992;             <tt class="literal">clear()</tt> &#26469;&#25511;&#21046;&#31532;&#19968;&#32423;&#32531;&#23384;&#30340;&#22823;&#23567;&#12290;        </p><pre class="programlisting">Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();   for ( int i=0; i&lt;100000; i++ ) {    Customer customer = new Customer(.....);    session.save(customer);    if ( i % 20 == 0 ) { //20, same as the JDBC batch size //20,&#19982;JDBC&#25209;&#37327;&#35774;&#32622;&#30456;&#21516;        //flush a batch of inserts and release memory:        //&#23558;&#26412;&#25209;&#25554;&#20837;&#30340;&#23545;&#35937;&#31435;&#21363;&#20889;&#20837;&#25968;&#25454;&#24211;&#24182;&#37322;&#25918;&#20869;&#23384;        session.flush();        session.clear();    }}   tx.commit();session.close();</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="batch-update"></a>14.2.&nbsp;&#25209;&#37327;&#26356;&#26032;&#65288;Batch updates&#65289;</h2></div></div><div></div></div><p>            &#27492;&#26041;&#27861;&#21516;&#26679;&#36866;&#29992;&#20110;&#26816;&#32034;&#21644;&#26356;&#26032;&#25968;&#25454;&#12290;&#27492;&#22806;&#65292;&#22312;&#36827;&#34892;&#20250;&#36820;&#22238;&#24456;&#22810;&#34892;&#25968;&#25454;&#30340;&#26597;&#35810;&#26102;&#65292;            &#20320;&#38656;&#35201;&#20351;&#29992; <tt class="literal">scroll()</tt> &#26041;&#27861;&#20197;&#20415;&#20805;&#20998;&#21033;&#29992;&#26381;&#21153;&#22120;&#31471;&#28216;&#26631;&#25152;&#24102;&#26469;&#30340;&#22909;&#22788;&#12290;        </p><pre class="programlisting">Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();   ScrollableResults customers = session.getNamedQuery("GetCustomers")    .setCacheMode(CacheMode.IGNORE)    .scroll(ScrollMode.FORWARD_ONLY);int count=0;while ( customers.next() ) {    Customer customer = (Customer) customers.get(0);    customer.updateStuff(...);    if ( ++count % 20 == 0 ) {        //flush a batch of updates and release memory:        session.flush();        session.clear();    }}   tx.commit();session.close();</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="batch-direct"></a>14.3.&nbsp;&#22823;&#25209;&#37327;&#26356;&#26032;/&#21024;&#38500;&#65288;Bulk update/delete&#65289;</h2></div></div><div></div></div><p>            &#23601;&#20687;&#24050;&#32463;&#35752;&#35770;&#30340;&#37027;&#26679;&#65292;&#33258;&#21160;&#21644;&#36879;&#26126;&#30340; &#23545;&#35937;/&#20851;&#31995; &#26144;&#23556;&#65288;object/relational mapping&#65289;&#20851;&#27880;&#20110;&#31649;&#29702;&#23545;&#35937;&#30340;&#29366;&#24577;&#12290;            &#36825;&#23601;&#24847;&#21619;&#30528;&#23545;&#35937;&#30340;&#29366;&#24577;&#23384;&#22312;&#20110;&#20869;&#23384;&#65292;&#22240;&#27492;&#30452;&#25509;&#26356;&#26032;&#25110;&#32773;&#21024;&#38500; (&#20351;&#29992; SQL &#35821;&#21477; <tt class="literal">UPDATE</tt> &#21644;            <tt class="literal">DELETE</tt>) &#25968;&#25454;&#24211;&#20013;&#30340;&#25968;&#25454;&#23558;&#19981;&#20250;&#24433;&#21709;&#20869;&#23384;&#20013;&#30340;&#23545;&#35937;&#29366;&#24577;&#21644;&#23545;&#35937;&#25968;&#25454;&#12290;            &#19981;&#36807;&#65292;Hibernate&#25552;&#20379;&#36890;&#36807;Hibernate&#26597;&#35810;&#35821;&#35328;&#65288;<a href="queryhql.html" title="&#31532;&nbsp;15&nbsp;&#31456;&nbsp;HQL: Hibernate&#26597;&#35810;&#35821;&#35328;">&#31532;&nbsp;15&nbsp;&#31456; <i>HQL: Hibernate&#26597;&#35810;&#35821;&#35328;</i></a>&#65289;&#26469;&#25191;&#34892;&#22823;&#25209;            &#37327;SQL&#39118;&#26684;&#30340;&#65288;<tt class="literal">UPDATE</tt>&#65289;&#21644;&#65288;<tt class="literal">DELETE</tt>&#65289; &#35821;&#21477;&#30340;&#26041;&#27861;&#12290;        </p><p>            <tt class="literal">UPDATE</tt> &#21644; <tt class="literal">DELETE</tt>&#35821;&#21477;&#30340;&#35821;&#27861;&#20026;&#65306;            <tt class="literal">( UPDATE | DELETE ) FROM? ClassName (WHERE WHERE_CONDITIONS)?</tt>&#12290;            &#26377;&#20960;&#28857;&#35828;&#26126;&#65306;        </p><div class="itemizedlist"><ul type="disc" compact><li><p>                    &#22312;FROM&#23376;&#21477;&#65288;from-clause&#65289;&#20013;&#65292;FROM&#20851;&#38190;&#23383;&#26159;&#21487;&#36873;&#30340;                </p></li><li><p>                    &#22312;FROM&#23376;&#21477;&#65288;from-clause&#65289;&#20013;&#21482;&#33021;&#26377;&#19968;&#20010;&#31867;&#21517;&#65292;&#24182;&#19988;&#23427;<span class="emphasis"><em>&#19981;&#33021;</em></span>&#26377;&#21035;&#21517;                </p></li><li><p>                    &#19981;&#33021;&#22312;&#22823;&#25209;&#37327;HQL&#35821;&#21477;&#20013;&#20351;&#29992;&#36830;&#25509;&#65288;&#26174;&#24335;&#25110;&#32773;&#38544;&#24335;&#30340;&#37117;&#19981;&#34892;&#65289;&#12290;&#19981;&#36807;&#22312;WHERE&#23376;&#21477;&#20013;&#21487;&#20197;&#20351;&#29992;&#23376;&#26597;&#35810;&#12290;                </p></li><li><p>                       &#25972;&#20010;WHERE&#23376;&#21477;&#26159;&#21487;&#36873;&#30340;&#12290;                </p></li></ul></div><p>            &#20030;&#20010;&#20363;&#23376;&#65292;&#20351;&#29992;<tt class="literal">Query.executeUpdate()</tt>&#26041;&#27861;&#25191;&#34892;&#19968;&#20010;HQL            <tt class="literal">UPDATE</tt>&#35821;&#21477;&#65306;        </p><pre class="programlisting">Session session = sessionFactory.openSession();        Transaction tx = session.beginTransaction();        String hqlUpdate = "update Customer set name = :newName where name = :oldName";        int updatedEntities = s.createQuery( hqlUpdate )                            .setString( "newName", newName )                            .setString( "oldName", oldName )                            .executeUpdate();        tx.commit();        session.close();</pre><p>            &#25191;&#34892;&#19968;&#20010;HQL <tt class="literal">DELETE</tt>&#65292;&#21516;&#26679;&#20351;&#29992; <tt class="literal">Query.executeUpdate()</tt> &#26041;&#27861;            &#65288;&#27492;&#26041;&#27861;&#26159;&#20026; &#37027;&#20123;&#29087;&#24713;JDBC <tt class="literal">PreparedStatement.executeUpdate()</tt> &#30340;&#20154;&#20204;&#32780;&#35774;&#23450;&#30340;&#65289;        </p><pre class="programlisting">Session session = sessionFactory.openSession();        Transaction tx = session.beginTransaction();        String hqlDelete = "delete Customer where name = :oldName";        int deletedEntities = s.createQuery( hqlDelete )                            .setString( "oldName", oldName )                            .executeUpdate();        tx.commit();        session.close();</pre><p>            &#30001;<tt class="literal">Query.executeUpdate()</tt>&#26041;&#27861;&#36820;&#22238;&#30340;<tt class="literal">&#25972;&#22411;</tt>&#20540;&#34920;&#26126;&#20102;&#21463;&#27492;&#25805;&#20316;&#24433;&#21709;&#30340;&#35760;&#24405;&#25968;&#37327;&#12290;            &#27880;&#24847;&#36825;&#20010;&#25968;&#20540;&#21487;&#33021;&#19982;&#25968;&#25454;&#24211;&#20013;&#34987;&#65288;&#26368;&#21518;&#19968;&#26465;SQL&#35821;&#21477;&#65289;&#24433;&#21709;&#20102;&#30340;&#8220;&#34892;&#8221;&#25968;&#26377;&#20851;&#65292;&#20063;&#21487;&#33021;&#27809;&#26377;&#12290;&#19968;&#20010;&#22823;&#25209;&#37327;HQL&#25805;&#20316;&#21487;&#33021;&#23548;&#33268;&#22810;&#26465;&#23454;&#38469;&#30340;SQL&#35821;&#21477;&#34987;&#25191;&#34892;&#65292;            &#20030;&#20010;&#20363;&#23376;&#65292;&#23545;joined-subclass&#26144;&#23556;&#26041;&#24335;&#30340;&#31867;&#36827;&#34892;&#30340;&#27492;&#31867;&#25805;&#20316;&#12290;&#36825;&#20010;&#36820;&#22238;&#20540;&#20195;&#34920;&#20102;&#23454;&#38469;&#34987;&#35821;&#21477;&#24433;&#21709;&#20102;&#30340;&#35760;&#24405;&#25968;&#37327;&#12290;&#22312;&#37027;&#20010;joined-subclass&#30340;&#20363;&#23376;&#20013;&#65292;            &#23545;&#19968;&#20010;&#23376;&#31867;&#30340;&#21024;&#38500;&#23454;&#38469;&#19978;&#21487;&#33021;&#19981;&#20165;&#20165;&#20250;&#21024;&#38500;&#23376;&#31867;&#26144;&#23556;&#21040;&#30340;&#34920;&#32780;&#19988;&#20250;&#24433;&#21709;&#8220;&#26681;&#8221;&#34920;&#65292;&#36824;&#26377;&#21487;&#33021;&#24433;&#21709;&#19982;&#20043;&#26377;&#32487;&#25215;&#20851;&#31995;&#30340;joined-subclass&#26144;&#23556;&#26041;&#24335;&#30340;&#23376;&#31867;&#30340;&#34920;&#12290;        </p><p>            &#27880;&#24847;&#65292;&#19978;&#36848;&#22823;&#25209;&#37327;HQL&#25805;&#20316;&#30340;&#23569;&#25968;&#38480;&#21046;&#20250;&#22312;&#26032;&#29256;&#26412;&#20013;&#24471;&#21040;&#25913;&#36827;&#65307;&#36827;&#19968;&#27493;&#35814;&#32454;&#20449;&#24687;&#35831;&#21442;&#32771;JIRA&#37324;&#30340;&#36335;&#32447;&#22270;(roadmap)&#12290;        </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="events.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="index.html">&#19978;&#19968;&#32423;</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="queryhql.html">&#19979;&#19968;&#39029;</a></td></tr><tr><td width="40%" align="left" valign="top">&#31532;&nbsp;13&nbsp;&#31456;&nbsp;	&#25318;&#25130;&#22120;&#19982;&#20107;&#20214;(Interceptors and events)	&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">&#36215;&#22987;&#39029;</a></td><td width="40%" align="right" valign="top">&nbsp;&#31532;&nbsp;15&nbsp;&#31456;&nbsp;HQL: Hibernate&#26597;&#35810;&#35821;&#35328;</td></tr></table></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -