📄 batch.html
字号:
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>第 13 章 批量处理(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="第 12 章 
 拦截器与事件(Interceptors and events)
 "><link rel="next" href="queryhql.html" title="第 14 章 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">第 13 章 批量处理(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>第 13 章 批量处理(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><p> 但是,这不是绝对必须的,因为我们可以显式设置<tt class="literal">CacheMode</tt>来关闭与二级缓存的交互。 </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. 批量插入(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>13.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-statelesssession"></a>13.3. StatelessSession (无状态session)接口</h2></div></div><div></div></div><p> 作为选择,Hibernate提供了基于命令的API,可以用detached object的形式把数据以流的方法加入到数据库,或从数据库输出。<tt class="literal">StatelessSession</tt>没有持久化上下文,也不提供多少高层的生命周期语义。特别是,无状态session不实现第一级cache,也不和第二级缓存,或者查询缓存交互。它不实现事务化写,也不实现脏数据检查。用stateless session进行的操作甚至不级联到关联实例。stateless session忽略集合类(Collections)。通过stateless session进行的操作不触发Hibernate的事件模型和拦截器。无状态session对数据的混淆现象免疫,因为它没有第一级缓存。无状态session是低层的抽象,和低层JDBC相当接近。 </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> 注意在上面的例子中,查询返回的<tt class="literal">Customer</tt>实例立即被脱管(detach)。它们与任何持久化上下文都没有关系。 </p><p> <tt class="literal">StatelessSession</tt> 接口定义的<tt class="literal">insert(), update()</tt> 和 <tt class="literal">delete()</tt>操作是直接的数据库行级别操作,其结果是立刻执行一条<tt class="literal">INSERT, UPDATE</tt> 或 <tt class="literal">DELETE</tt> 语句。因此,它们的语义和<tt class="literal">Session</tt> 接口定义的<tt class="literal">save(), saveOrUpdate()</tt> 和<tt class="literal">delete()</tt> 操作有很大的不同。 </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. DML(数据操作语言)风格的操作(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="第 14 章 HQL: Hibernate查询语言">第 14 章 <i>HQL: Hibernate查询语言</i></a>).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -