[
https://jira.jboss.org/jira/browse/DNA-509?page=com.atlassian.jira.plugin...
]
Randall Hauch commented on DNA-509:
-----------------------------------
Added to the JPA Connector project a new class that allows JpaSource and JpaConnection to
hold onto a JPA EntityManager and EntityManagerFactory, while ensuring that the
EntityManager and EntityManagerFactory instances are ALWAYS closed - as long as the
JpaConnection objects and JpaSource objects are always closed (in any order). It also
allows the JpaSource to start using a new EntityManagerFactory if the settings are
changed, without impacting any previously-opened and in-use connections.
Although I did try to find a way for the JpaSource to not have a 'close()' method,
it simply was not possible to do without adding TREMENDOUS overhead to the creation of an
EntityManagerFactory for every connection (remember, the EntityManagerFactory must be
configured with each entity class, and does the schema validation/create/drop/update
operations, and this had to be done on EVERY create new connection). And rather than have
'close()' be an artifact of the JpaSource class, it's actually better if this
method is defined on the RepositorySource interface and that all implementations provide a
close() method. This provides a standard way for the RepositorySource implementations to
begin reclaiming resources such as connections, caches, files, and/or large objects.
RepositoryLibrary and RepositoryService were modified to ensure that all RepositorySource
objects are closed when the library and service are shutdown (via their administrative
interface).
The reusable connector framework unit tests were also modified to ensure that they all
close the RepositorySource upon teardown of the tests (or, in the case of the reading
tests, upon teardown after all tests are run).
JPA Connector does not close all EntityManager instances that it
creates
------------------------------------------------------------------------
Key: DNA-509
URL:
https://jira.jboss.org/jira/browse/DNA-509
Project: DNA
Issue Type: Bug
Components: Connectors
Affects Versions: 0.6
Reporter: Randall Hauch
Assignee: Randall Hauch
Priority: Blocker
Fix For: 0.6
The JPA connector acquires several EntityManager instances, but does not close all of
them. This can cause a memory leak in the code, and can cause the JVM to run out of
memory. Because the EntityManager is the manager for all of the JPA entity beans
(obviously), not closing them will retain objects in memory and prevent them from being
garbage collected.
There doesn't appear to be an easy way with the current JpaSource and JpaConnection
design to properly do this, other than call the JpaSource.close() method. Unfortunately
that does not always work, as the current implementation of JpaSource.getConnection() may
(if the model is changed) create a new EntityManagerFactory. And even if the
JpaConnection did close the EntityManager it's using, the JpaSource's
EntityManagerFactory is not guaranteed to be called (even if we adopt a new
RepositorySource.close() and require it to be called everywhere).
The best way to fix this might be for the JpaSource to not have to maintain all this
state (like it currently does), but instead to have an object in-between the JpaSource and
JpaConnection that maintains the EntityManagerFactory and creates EntityManagers as
needed. The JpaSource can hold onto this, but wouldn't own the instances. This
intermediate object can always ensure that all EntityManagers and EntityManagerFactories
are closed when no longer needed (e.g., using reference counts and a
"last-one-to-leave-turn-the-lights-off" approach). JpaConnection.close() would
obvious signal to this object that it no longer needs its EntityManager.
JpaSource cannot properly do all this management, because its bean properties can be
changed at any time, meaning that even after some connections to one database have been
created, the JpaSource can be changed and pointed to a different database. Yes, this
isn't good practice, but it technically is possible and thus needs to be addressed.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira