blob: aecb4d482539290fd9da5af453eb332144da1841 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>5.&nbsp; Closing the EntityManagerFactory</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.0 User's Guide"><link rel="up" href="jpa_overview_emfactory.html" title="Chapter&nbsp;7.&nbsp; EntityManagerFactory"><link rel="prev" href="jpa_overview_emf_properties.html" title="4.&nbsp; Retrieving Properties Information"><link rel="next" href="jpa_overview_emfactory_puutil.html" title="6.&nbsp; 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.&nbsp;
Closing the EntityManagerFactory
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_emf_properties.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;7.&nbsp;
EntityManagerFactory
</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="jpa_overview_emfactory_puutil.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>5.&nbsp;
Closing the EntityManagerFactory
</h2></div></div></div><a class="indexterm" name="d0e4710"></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>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_emfactory.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="jpa_overview_emfactory_puutil.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4.&nbsp;
Retrieving Properties Information
&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;6.&nbsp;
PersistenceUnitUtil
</td></tr></table></div></body></html>