| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>4. Closing the EntityManagerFactory</title><link rel="stylesheet" href="css/docbook.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.72.0"><link rel="start" href="manual.html" title="Apache OpenJPA User's Guide"><link rel="up" href="jpa_overview_emfactory.html" title="Chapter 7. EntityManagerFactory"><link rel="prev" href="jpa_overview_emfactory_perscontext.html" title="3. Persistence Context"><link rel="next" href="jpa_overview_em.html" title="Chapter 8. EntityManager"></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">4. |
| Closing the EntityManagerFactory |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_emfactory_perscontext.html">Prev</a> </td><th width="60%" align="center">Chapter 7. |
| EntityManagerFactory |
| </th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_em.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_emfactory_close"></a>4. |
| Closing the EntityManagerFactory |
| </h2></div></div></div><a class="indexterm" name="d0e4404"></a><pre class="programlisting"> |
| public boolean isOpen (); |
| public void close (); |
| </pre><p> |
| <code class="classname">EntityManagerFactory</code> instances are heavyweight objects. |
| Each factory might maintain a metadata cache, object state cache, <code class="classname"> |
| EntityManager</code> pool, connection pool, and more. If your application |
| no longer needs an <code class="classname">EntityManagerFactory</code>, you should |
| close it to free these resources. When an <code class="classname">EntityManagerFactory |
| </code> closes, all <code class="classname">EntityManager</code>s from that |
| factory, and by extension all entities managed by those <code class="classname"> |
| EntityManager</code> s, become invalid. Attempting to close an <code class="classname"> |
| EntityManagerFactory</code> while one or more of its <code class="classname"> |
| EntityManager</code>s has an active transaction may result in an |
| <code class="classname">IllegalStateException</code>. |
| </p><p> |
| Closing an <code class="classname">EntityManagerFactory</code> should not be taken |
| lightly. It is much better to keep a factory open for a long period of time than |
| to repeatedly create and close new factories. Thus, most applications will never |
| close the factory, or only close it when the application is exiting. Only |
| applications that require multiple factories with different configurations have |
| an obvious reason to create and close multiple <code class="classname">EntityManagerFactory |
| </code> instances. Once a factory is closed, all methods except |
| <code class="methodname">isOpen</code> throw an <code class="classname"> |
| IllegalStateException</code>. |
| </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_emfactory_perscontext.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_emfactory.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jpa_overview_em.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3. |
| Persistence Context |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 8. |
| EntityManager |
| </td></tr></table></div></body></html> |