Community
Participate
Working Groups
Allowing a Persistence Context to reference managed objects in a weak manner would provide Rich Clients an automatic memory management mechanism. Currently JPA clients must manage memory foot print manually by controlling the lifetime of the PersistenceContext as well as the contents of the Persistence Context. Within a Rich Client environment it is much easier for application developers to simply use the managed objects as the UI backing objects and leverage transparent persistence instead of creating a DOA layer. Currently the only limitation with this approach is memory management. Persistence Contexts tend to grow and detaching objects is problematic. With this feature memory management can be delegated to the VMs garbage collection functionality through the use of weak references. End users must be aware of certain side effects of unpredictable detachment from the Persistence Context but as long as developers are aware of the limitations the feature should provide the needed functionality.
Created attachment 87763 [details] Patch This patch enhances TopLink with the capability holding JPA-Entities inside the Entity Manager using WeakReferences (instead of "Hard" refs). This feature can be configured with TopLink's configuration mechanism. The signature of the IdentityHashtable was extracted to a superclass. Additional subclasses were introduced - so micro optimization (direct access to the inner class fields) is still possible.
Created attachment 87765 [details] Simple TestCase (self contained Eclipse Project with JavaDB) An Unit-Test which assures the identity of object managed in the EntityManager using WeakReferences.
Created attachment 96948 [details] Proposed Feature Patch
Created attachment 97342 [details] Feature Updates
Created attachment 98096 [details] Patch Post Feedback This patch contains the updates from feedback from James Sutherlan.
Created attachment 98739 [details] Final Patch
Created attachment 104421 [details] Patch Post Feedback
PERSISTENCE_CONTEXT_REFERENCE_MODE="eclipselink.persistence-context.reference-mode"; Specifies what the reference mode of the persistence context should be. The property set in persistence.xml or passed to createEntityManagerFactory affects all EntityManagers created by the factory. Alternatively, to apply the property only to some EntityManagers pass it to createEntityManager method. /** * <b>Purpose:</b> This class is a configuration property used to specify what * type of References EclipseLink will use when referencing Entities within the * Persistence Context / UnitOfWork. * * @author Gordon * */ public enum ReferenceMode { /** * References to Objects will be through hard references. These objects will * not be available for garbage collection until the referencing artifact * (usually a Persistence Context or UnitOfWork) released or closed. */ HARD, /** * References to Objects that support active attribute change tracking * (enabled through weaving or by the developer)will be held by weak * references. This means any of afore mentioned objects no longer * referenced directly or indirectly will be available for garbage * collection. If the object is gc’d before the EM/UnitOfWork flushes to the * database then this object and any others like it will not be checked for * changes. When a change is made to a change tracked object that object is * moved to a hard reference and will not be available for GC until flushed. * New and removed objects are also held by hard references. Non change * tracked objects will always be held by “hard” references and are not * available for GC. This is the default mode for EclipsLink. See: * {@link java.lang.ref.WeakReference} * {@link org.eclipse.persistence.descriptors.changetracking.AttributeChangeTrackingPolicy} */ WEAK, /** * Same as weak reference except Objects that can not be changed tracked * (Deferred Change Detection) will not be prevented from being garbage * collected. This may result in a loss of changes if a changed object is * removed before being flushed to the database. When a change is made to a * change tracked object that object is moved to a hard reference and will * not be available for GC until flushed. New and removed objects are also * held by hard references until flush.. */ FORCE_WEAK See design doc for more details : http://wiki.eclipse.org/EclipseLink/DesignDocs/214661
Adding design doc to URL property of bug.
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink