This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 214661 - VM managed Entity detachment
Summary: VM managed Entity detachment
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL: http://wiki.eclipse.org/EclipseLink/D...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-01-08 14:51 EST by Gordon Yorke CLA
Modified: 2022-06-09 10:21 EDT (History)
1 user (show)

See Also:


Attachments
Patch (344.07 KB, application/octet-stream)
2008-01-24 08:31 EST, Adam Bien CLA
no flags Details
Simple TestCase (self contained Eclipse Project with JavaDB) (126.93 KB, application/zip)
2008-01-24 08:38 EST, Adam Bien CLA
no flags Details
Proposed Feature Patch (108.11 KB, patch)
2008-04-21 21:55 EDT, Gordon Yorke CLA
no flags Details | Diff
Feature Updates (115.22 KB, patch)
2008-04-23 21:48 EDT, Gordon Yorke CLA
no flags Details | Diff
Patch Post Feedback (116.36 KB, patch)
2008-04-29 21:38 EDT, Gordon Yorke CLA
no flags Details | Diff
Final Patch (110.20 KB, patch)
2008-05-05 22:07 EDT, Gordon Yorke CLA
no flags Details | Diff
Patch Post Feedback (9.23 KB, patch)
2008-06-10 22:22 EDT, Gordon Yorke CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Gordon Yorke CLA 2008-01-08 14:51:36 EST
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.
Comment 1 Adam Bien CLA 2008-01-24 08:31:54 EST
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.
Comment 2 Adam Bien CLA 2008-01-24 08:38:02 EST
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.
Comment 3 Gordon Yorke CLA 2008-04-21 21:55:38 EDT
Created attachment 96948 [details]
Proposed Feature Patch
Comment 4 Gordon Yorke CLA 2008-04-23 21:48:58 EDT
Created attachment 97342 [details]
Feature Updates
Comment 5 Gordon Yorke CLA 2008-04-29 21:38:38 EDT
Created attachment 98096 [details]
Patch Post Feedback

This patch contains the updates from feedback from James Sutherlan.
Comment 6 Gordon Yorke CLA 2008-05-05 22:07:33 EDT
Created attachment 98739 [details]
Final Patch
Comment 7 Gordon Yorke CLA 2008-06-10 22:22:27 EDT
Created attachment 104421 [details]
Patch Post Feedback
Comment 8 Gordon Yorke CLA 2008-06-13 13:23:25 EDT
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
Comment 9 Doug Clarke CLA 2008-06-13 13:27:27 EDT
Adding design doc to URL property of bug.
Comment 10 Eclipse Webmaster CLA 2022-06-09 10:21:31 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink