📄 batch.html
字号:
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>第 14 章 批量处理(Batch processing)</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 - 符合Java习惯的关系数据库持久化"><link rel="up" href="index.html" title="HIBERNATE - 符合Java习惯的关系数据库持久化"><link rel="previous" href="events.html" title="第 13 章 
 拦截器与事件(Interceptors and events)
 "><link rel="next" href="queryhql.html" title="第 15 章 HQL: Hibernate查询语言"></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">第 14 章 批量处理(Batch processing)</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="events.html">上一页</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="queryhql.html">下一页</a></td></tr></table><hr></div><div class="chapter" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title"><a name="batch"></a>第 14 章 批量处理(Batch processing)</h2></div></div><div></div></div><p> 使用Hibernate将 100 000 条记录插入到数据库的一个很自然的做法可能是这样的 </p><pre class="programlisting">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();</pre><p> 这段程序大概运行到 50 000 条记录左右会失败并抛出 <tt class="literal">内存溢出异常(OutOfMemoryException)</tt> 。 这是因为 Hibernate 把所有新插入的 <tt class="literal">客户(Customer)</tt>实例在 session级别的缓存区进行了缓存的缘故。 </p><p> 我们会在本章告诉你如何避免此类问题。首先,如果你要执行批量处理并且想要达到一个理想的性能, 那么使用JDBC的批量(batching)功能是至关重要。将JDBC的批量抓取数量(batch size)参数设置到一个合适值 (比如,10-50之间): </p><pre class="programlisting">hibernate.jdbc.batch_size 20</pre><p> 你也可能想在执行批量处理时关闭二级缓存: </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. 批量插入(Batch inserts)</h2></div></div><div></div></div><p> 如果要将很多对象持久化,你必须通过经常的调用 <tt class="literal">flush()</tt> 以及稍后调用 <tt class="literal">clear()</tt> 来控制第一级缓存的大小。 </p><pre class="programlisting">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批量设置相同 //flush a batch of inserts 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-update"></a>14.2. 批量更新(Batch updates)</h2></div></div><div></div></div><p> 此方法同样适用于检索和更新数据。此外,在进行会返回很多行数据的查询时, 你需要使用 <tt class="literal">scroll()</tt> 方法以便充分利用服务器端游标所带来的好处。 </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. 大批量更新/删除(Bulk update/delete)</h2></div></div><div></div></div><p> 就像已经讨论的那样,自动和透明的 对象/关系 映射(object/relational mapping)关注于管理对象的状态。 这就意味着对象的状态存在于内存,因此直接更新或者删除 (使用 SQL 语句 <tt class="literal">UPDATE</tt> 和 <tt class="literal">DELETE</tt>) 数据库中的数据将不会影响内存中的对象状态和对象数据。 不过,Hibernate提供通过Hibernate查询语言(<a href="queryhql.html" title="第 15 章 HQL: Hibernate查询语言">第 15 章 <i>HQL: Hibernate查询语言</i></a>)来执行大批 量SQL风格的(<tt class="literal">UPDATE</tt>)和(<tt class="literal">DELETE</tt>) 语句的方法。 </p><p> <tt class="literal">UPDATE</tt> 和 <tt class="literal">DELETE</tt>语句的语法为: <tt class="literal">( UPDATE | DELETE ) FROM? ClassName (WHERE WHERE_CONDITIONS)?</tt>。 有几点说明: </p><div class="itemizedlist"><ul type="disc" compact><li><p> 在FROM子句(from-clause)中,FROM关键字是可选的 </p></li><li><p> 在FROM子句(from-clause)中只能有一个类名,并且它<span class="emphasis"><em>不能</em></span>有别名 </p></li><li><p> 不能在大批量HQL语句中使用连接(显式或者隐式的都不行)。不过在WHERE子句中可以使用子查询。 </p></li><li><p> 整个WHERE子句是可选的。 </p></li></ul></div><p> 举个例子,使用<tt class="literal">Query.executeUpdate()</tt>方法执行一个HQL <tt class="literal">UPDATE</tt>语句: </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> 执行一个HQL <tt class="literal">DELETE</tt>,同样使用 <tt class="literal">Query.executeUpdate()</tt> 方法 (此方法是为 那些熟悉JDBC <tt class="literal">PreparedStatement.executeUpdate()</tt> 的人们而设定的) </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> 由<tt class="literal">Query.executeUpdate()</tt>方法返回的<tt class="literal">整型</tt>值表明了受此操作影响的记录数量。 注意这个数值可能与数据库中被(最后一条SQL语句)影响了的“行”数有关,也可能没有。一个大批量HQL操作可能导致多条实际的SQL语句被执行, 举个例子,对joined-subclass映射方式的类进行的此类操作。这个返回值代表了实际被语句影响了的记录数量。在那个joined-subclass的例子中, 对一个子类的删除实际上可能不仅仅会删除子类映射到的表而且会影响“根”表,还有可能影响与之有继承关系的joined-subclass映射方式的子类的表。 </p><p> 注意,上述大批量HQL操作的少数限制会在新版本中得到改进;进一步详细信息请参考JIRA里的路线图(roadmap)。 </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">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="queryhql.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">第 13 章 拦截器与事件(Interceptors and events) </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 第 15 章 HQL: Hibernate查询语言</td></tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -