| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>5. Cache Management</title><link rel="stylesheet" href="css/docbook.css" type="text/css"><base href="display"><meta name="generator" content="DocBook XSL Stylesheets V1.72.0"><link rel="start" href="manual.html" title="Apache OpenJPA 2.1 User's Guide"><link rel="up" href="jpa_overview_em.html" title="Chapter 8. EntityManager"><link rel="prev" href="jpa_overview_em_identity.html" title="4. Entity Identity Management"><link rel="next" href="jpa_overview_em_query.html" title="6. Query Factory"></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">5. |
| Cache Management |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_em_identity.html">Prev</a> </td><th width="60%" align="center">Chapter 8. |
| EntityManager |
| </th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_em_query.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jpa_overview_em_cache"></a>5. |
| Cache Management |
| </h2></div></div></div><a class="indexterm" name="d0e5494"></a><pre class="programlisting"> |
| public void flush(); |
| </pre><p> |
| <a class="indexterm" name="d0e5503"></a> |
| <a class="indexterm" name="d0e5509"></a> |
| <a class="indexterm" name="d0e5515"></a> |
| The <code class="methodname">flush</code> method writes any changes that have been made |
| in the current transaction to the datastore. If the <code class="classname">EntityManager |
| </code> does not already have a connection to the datastore, it obtains one |
| for the flush and retains it for the duration of the transaction. Any exceptions |
| during flush cause the transaction to be marked for rollback. See |
| <a href="jpa_overview_trans.html" title="Chapter 9. Transaction">Chapter 9, <i xmlns:xlink="http://www.w3.org/1999/xlink"> |
| Transaction |
| </i></a>. |
| </p><p> |
| Flushing requires an active transaction. If there isn't a transaction in |
| progress, the <code class="methodname">flush</code> method throws a <code class="classname"> |
| TransactionRequiredException</code>. |
| </p><pre class="programlisting"> |
| public FlushModeType getFlushMode(); |
| public void setFlushMode(FlushModeType flushMode); |
| </pre><p> |
| <a class="indexterm" name="d0e5541"></a> |
| <a class="indexterm" name="d0e5547"></a> |
| The <code class="classname">EntityManager</code>'s <code class="literal">FlushMode</code> property |
| controls whether to flush transactional changes before executing queries. This |
| allows the query results to take into account changes you have made during the |
| current transaction. Available |
| <a xmlns:xlink="http://www.w3.org/1999/xlink" href="http://java.sun.com/javaee/6/docs/api/javax/persistence/FlushModeType.html" target="_top"> |
| <code class="classname">javax.persistence.FlushModeType</code></a> constants are: |
| </p><div class="itemizedlist"><ul type="disc"><li><p> |
| <code class="literal">COMMIT</code>: Only flush when committing, or when told to do so |
| through the <code class="methodname">flush</code> method. Query results may not take |
| into account changes made in the current transaction. |
| </p></li><li><p> |
| <code class="literal">AUTO</code>: The implementation is permitted to flush before |
| queries to ensure that the results reflect the most recent object state. |
| </p></li></ul></div><p> |
| You can also set the flush mode on individual |
| <a href="jpa_overview_query.html" title="Chapter 10. JPA Query"><code class="classname">Query</code></a> |
| instances. |
| </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p> |
| OpenJPA only flushes before a query if the query might be affected by data |
| changed in the current transaction. Additionally, OpenJPA allows fine-grained |
| control over flushing behavior. See the Reference Guide's |
| <a href="ref_guide_dbsetup_retain.html" title="8. Configuring the Use of JDBC Connections">Section 8, “ |
| Configuring the Use of JDBC Connections |
| ”</a>. |
| </p></div><pre class="programlisting"> |
| public void clear(); |
| </pre><p> |
| <a class="indexterm" name="d0e5593"></a> |
| <a class="indexterm" name="d0e5599"></a> |
| Clearing the <code class="classname">EntityManager</code> effectively ends the |
| persistence context. All entities managed by the <code class="classname">EntityManager |
| </code> become detached. |
| </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_em_identity.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_em.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jpa_overview_em_query.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4. |
| Entity Identity Management |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 6. |
| Query Factory |
| </td></tr></table></div></body></html> |