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

📄 batch.html

📁 hibernate的中文教程,html形式,方便了初学者可以任意copy代码,完成知识点的学习
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;13&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;12&nbsp;&#31456;&nbsp;&#xA;	&#25318;&#25130;&#22120;&#19982;&#20107;&#20214;(Interceptors and events)&#xA;	"><link rel="next" href="queryhql.html" title="&#31532;&nbsp;14&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;13&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;13&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><p>        &#20294;&#26159;&#65292;&#36825;&#19981;&#26159;&#32477;&#23545;&#24517;&#39035;&#30340;&#65292;&#22240;&#20026;&#25105;&#20204;&#21487;&#20197;&#26174;&#24335;&#35774;&#32622;<tt class="literal">CacheMode</tt>&#26469;&#20851;&#38381;&#19982;&#20108;&#32423;&#32531;&#23384;&#30340;&#20132;&#20114;&#12290;    </p><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="batch-inserts"></a>13.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>13.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-statelesssession"></a>13.3.&nbsp;StatelessSession (&#26080;&#29366;&#24577;session)&#25509;&#21475;</h2></div></div><div></div></div><p>            &#20316;&#20026;&#36873;&#25321;&#65292;Hibernate&#25552;&#20379;&#20102;&#22522;&#20110;&#21629;&#20196;&#30340;API&#65292;&#21487;&#20197;&#29992;detached object&#30340;&#24418;&#24335;&#25226;&#25968;&#25454;&#20197;&#27969;&#30340;&#26041;&#27861;&#21152;&#20837;&#21040;&#25968;&#25454;&#24211;&#65292;&#25110;&#20174;&#25968;&#25454;&#24211;&#36755;&#20986;&#12290;<tt class="literal">StatelessSession</tt>&#27809;&#26377;&#25345;&#20037;&#21270;&#19978;&#19979;&#25991;&#65292;&#20063;&#19981;&#25552;&#20379;&#22810;&#23569;&#39640;&#23618;&#30340;&#29983;&#21629;&#21608;&#26399;&#35821;&#20041;&#12290;&#29305;&#21035;&#26159;&#65292;&#26080;&#29366;&#24577;session&#19981;&#23454;&#29616;&#31532;&#19968;&#32423;cache,&#20063;&#19981;&#21644;&#31532;&#20108;&#32423;&#32531;&#23384;&#65292;&#25110;&#32773;&#26597;&#35810;&#32531;&#23384;&#20132;&#20114;&#12290;&#23427;&#19981;&#23454;&#29616;&#20107;&#21153;&#21270;&#20889;&#65292;&#20063;&#19981;&#23454;&#29616;&#33039;&#25968;&#25454;&#26816;&#26597;&#12290;&#29992;stateless session&#36827;&#34892;&#30340;&#25805;&#20316;&#29978;&#33267;&#19981;&#32423;&#32852;&#21040;&#20851;&#32852;&#23454;&#20363;&#12290;stateless session&#24573;&#30053;&#38598;&#21512;&#31867;(Collections)&#12290;&#36890;&#36807;stateless session&#36827;&#34892;&#30340;&#25805;&#20316;&#19981;&#35302;&#21457;Hibernate&#30340;&#20107;&#20214;&#27169;&#22411;&#21644;&#25318;&#25130;&#22120;&#12290;&#26080;&#29366;&#24577;session&#23545;&#25968;&#25454;&#30340;&#28151;&#28102;&#29616;&#35937;&#20813;&#30123;&#65292;&#22240;&#20026;&#23427;&#27809;&#26377;&#31532;&#19968;&#32423;&#32531;&#23384;&#12290;&#26080;&#29366;&#24577;session&#26159;&#20302;&#23618;&#30340;&#25277;&#35937;&#65292;&#21644;&#20302;&#23618;JDBC&#30456;&#24403;&#25509;&#36817;&#12290;        </p><pre class="programlisting">StatelessSession session = sessionFactory.openStatelessSession();Transaction tx = session.beginTransaction();   ScrollableResults customers = session.getNamedQuery("GetCustomers")    .scroll(ScrollMode.FORWARD_ONLY);while ( customers.next() ) {    Customer customer = (Customer) customers.get(0);    customer.updateStuff(...);    session.update(customer);}   tx.commit();session.close();</pre><p>            &#27880;&#24847;&#22312;&#19978;&#38754;&#30340;&#20363;&#23376;&#20013;&#65292;&#26597;&#35810;&#36820;&#22238;&#30340;<tt class="literal">Customer</tt>&#23454;&#20363;&#31435;&#21363;&#34987;&#33073;&#31649;(detach)&#12290;&#23427;&#20204;&#19982;&#20219;&#20309;&#25345;&#20037;&#21270;&#19978;&#19979;&#25991;&#37117;&#27809;&#26377;&#20851;&#31995;&#12290;        </p><p>            <tt class="literal">StatelessSession</tt> &#25509;&#21475;&#23450;&#20041;&#30340;<tt class="literal">insert(), update()</tt> &#21644; <tt class="literal">delete()</tt>&#25805;&#20316;&#26159;&#30452;&#25509;&#30340;&#25968;&#25454;&#24211;&#34892;&#32423;&#21035;&#25805;&#20316;&#65292;&#20854;&#32467;&#26524;&#26159;&#31435;&#21051;&#25191;&#34892;&#19968;&#26465;<tt class="literal">INSERT, UPDATE</tt> &#25110; <tt class="literal">DELETE</tt> &#35821;&#21477;&#12290;&#22240;&#27492;&#65292;&#23427;&#20204;&#30340;&#35821;&#20041;&#21644;<tt class="literal">Session</tt> &#25509;&#21475;&#23450;&#20041;&#30340;<tt class="literal">save(), saveOrUpdate()</tt> &#21644;<tt class="literal">delete()</tt> &#25805;&#20316;&#26377;&#24456;&#22823;&#30340;&#19981;&#21516;&#12290;        </p></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="batch-direct"></a>13.4.&nbsp;DML(&#25968;&#25454;&#25805;&#20316;&#35821;&#35328;)&#39118;&#26684;&#30340;&#25805;&#20316;(DML-style operations)</h2></div></div><div></div></div><p>        hence manipulating (using the SQL <tt class="literal">Data Manipulation Language</tt>            (DML) statements: <tt class="literal">INSERT</tt>, <tt class="literal">UPDATE</tt>, <tt class="literal">DELETE</tt>)            data directly in the database will not affect in-memory state. However, Hibernate provides methods            for bulk SQL-style DML statement execution which are performed through the            Hibernate Query Language (<a href="queryhql.html" title="&#31532;&nbsp;14&nbsp;&#31456;&nbsp;HQL: Hibernate&#26597;&#35810;&#35821;&#35328;">&#31532;&nbsp;14&nbsp;&#31456; <i>HQL: Hibernate&#26597;&#35810;&#35821;&#35328;</i></a>).                        

⌨️ 快捷键说明

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