|  | <html><head> | 
|  | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|  | <title>5.  Closing the EntityManagerFactory</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.4 User's Guide"><link rel="up" href="jpa_overview_emfactory.html" title="Chapter 7.  EntityManagerFactory"><link rel="prev" href="jpa_overview_emf_properties.html" title="4.  Retrieving Properties Information"><link rel="next" href="jpa_overview_emfactory_puutil.html" title="6.  PersistenceUnitUtil"></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.  | 
|  | Closing the EntityManagerFactory | 
|  | </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_emf_properties.html">Prev</a> </td><th width="60%" align="center">Chapter 7.  | 
|  | EntityManagerFactory | 
|  | </th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_emfactory_puutil.html">Next</a></td></tr></table><hr></div><div class="section" title="5.  Closing the EntityManagerFactory"><div class="titlepage"><div><div><h2 class="title" style="clear: both" id="jpa_overview_emfactory_close">5.  | 
|  | Closing the EntityManagerFactory | 
|  | </h2></div></div></div> | 
|  |  | 
|  | <a class="indexterm" name="d5e2271"></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_emf_properties.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_emfactory_puutil.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4.  | 
|  | Retrieving Properties Information | 
|  |  </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 6.  | 
|  | PersistenceUnitUtil | 
|  | </td></tr></table></div></body></html> |