Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Development/JPA 2.0/cache api"

m (DI 2: Weaving is not being disabled for the JPA test suite)
m (DI 1: How to get the ID when the CMPPolicy is null)
Line 83: Line 83:
 
<source lang="java">
 
<source lang="java">
 
         // Handle a null CMPPolicy from a MappedSuperclass
 
         // Handle a null CMPPolicy from a MappedSuperclass
        CMP3Policy policy = (CMP3Policy) (aDescriptor.getCMPPolicy());
 
 
         Object identifier = null;
 
         Object identifier = null;
         if(null == policy) {
+
         if(!aDescriptor.hasCMPPolicy()) {
 
             // the following code gets the key either from the weaved _persistence_primaryKey field or using valueFromObject) if not weaved
 
             // the following code gets the key either from the weaved _persistence_primaryKey field or using valueFromObject) if not weaved
 
             identifier = aDescriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, (AbstractSession)getSession());
 
             identifier = aDescriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, (AbstractSession)getSession());
Line 93: Line 92:
 
         }
 
         }
 
</source>
 
</source>
 +
 
====DI 2: Weaving is not being disabled for the JPA test suite====
 
====DI 2: Weaving is not being disabled for the JPA test suite====
 
*Q)Why when weaving is turned of do I still see weaved entities in the JPA test suite?
 
*Q)Why when weaving is turned of do I still see weaved entities in the JPA test suite?

Revision as of 15:12, 10 May 2010

Cache APIs

JPA 2.0 Root | Enhancement Request

Issue Summary

In JPA 2.0 the specification has added a Cache API obtainable from an EntityManagerFactory. This simple API provides developers with rudimentary access and control of a second level cache. User's can interrogate the cache through a contains(Class, Object) API call and cause cached data to be evicted from the cache through an evict(Class, Object), evict(Class) or evictAll() API call.

See The JPA 2.0 Final Release Specification section 7.10 p.306 for details.

General Solution

Should be simple to equate evict to invalidate.

Work Required

  1. Develop tests
    approx 1 day
  2. Implement API
    approx 2 days

Implementation Details

The Cache API consists of following 4 methods

  1. contains(Class cls, Object primaryKey) --- checks whether the class with the specified primary key
    is contained in IdentityMap.
  2. evict(Class cls, Object primaryKey) --- invalidates the specified object in the IdentityMap
  3. evict(Class cls) --- invalidates the specified class in the IdentityMap.
  4. evictAll() --- Invalidates all classes in the IdentityMap.

Refactor 20100322

  • See https://bugs.eclipse.org/bugs/attachment.cgi?id=165763&action=diff
  • The following details describe changes to the original SVN 2896 implementation of Dec 2008 (Thank you very much Darani Y.). These modifications based on a review by Doug C. will arrive in Trunk around April 2010.
  • The implementation was modified on the following dates as follows...
    • 20100201: Rev# 6463 - bug# 301063 : Refactor CacheImpl to implement the EclipseLink API interface JpaCache (which implements Cache from JPA 2.0 via the new functions - clear(), clear(Class), clearQueryCache(), clearQueryCache(queryName), timeToLive(Object), isValid(Object), isValid(Class, id), print(), print(Class), printLocks(), validate(), getObject(Class, id), getObject(Object), putObject(Object), removeObject(Class), removeObject(Class, id), containsObject(Object), evictObject(Object), getId(Object)
    • 20100126: Rev# 6406 - bug# 298985 : Refactor Vector usage to Id or CacheId
    • 20091124: Rev# 5875 - bug# 272895 : Check invalidation during contains()
    • 20090513: Rev# 4170 - bug# 275953 : Bootstrap performance and compatibility

Analysis

  • Fact: Only @Entity objects are cached. @MappedSuperclass objects are not cached. @Embeddable objects are not cached directly but are cached inside their containing @Entity.

Issue 1: Fix evict() to handle non-Entity classes

  • Cache.evict(Class entity) and Cache.evict(Class entity, Object key) require handling of entity Class parameters that are other than entities like MappedSuperclasses, Embeddables or non-annotated java classes.

Issue 2: Refactor to get IdentityMapAccessor state through EMF reference

  • Doug: "1. Unsure if we need to hold all of this state or just hold the EMF and access

the necessary IdentityMapAccessor through this access."

  • We should also get the serverSession from the EMF when required instead of caching it on the CacheImpl.
    • Wherever we see... ServerSession this.serversession
    • replace it with... emf.getServerSession()

Fixed/Patched in SVN Rev# NNNN

Issue 3: Refactor dependencies to use Interfaces instead of Impl subclasses

  • Doug: "2. We will need to support more then just ServerSession so we need to have

impls such as this just rely on more general interfaces for the shared session such as Session or DatabaseSession interfaces." Fixed/Patched in SVN Rev# NNNN

Issue 4: Handle no CMPPolicy case for getId()

  • Doug: "3. The createPKVector does not handle the case where the descriptor does not

have a CMPPolicy. This occurs when bootstrapping from native metadata."

  • Avoid a possible NPE on the return from descriptor.getCMPolicy()
  • createPKVector changed to createPrimaryKeyFromId to refactor out Vector usage in bug 298985

Issue 5: Handle no associated descriptor for Class parameter

  • Doug: "3. continued...Also does not properly handle the case where the cls provided is not associated with

a descriptor."

  • See contains(Class, Object) and contains(Object).
  • The class may still be associated with a Descriptor if it is a MappedSuperclass though.

Issue 6: Add support for @MappedSuperclass descriptors

  • Doug: "4. Should we support passing in MappedSuperclass or general types from the

entity inheritance tree? I believe we should."

  • MappedSuperclasses do not represent real descriptors - they add functionality to concrete Entities that inherit from them - therefore they should not be evicted.
  • As Gordon mentioned, we should throw an IllegalArgumentException if a Class (abstract class) representing a MappedSuperclass is passed into evict(Class) - however after further discussion between us we will evict implementing subclasses.

Fixed/Patched in SVN Rev# NNNN

Analysis 6:
  • When checking for a MappedSuperclass we cannot use session.getDescriptor(class) or session.getClassDescriptor(class) because both will return the first Entity descriptor in the hierarchy if it exists. We need to check the mappedSuperclassDescriptors Map<MetadataClass, RelationalDescriptor> Map on the Project off the Session.

Issue 7: Throw an IAE for Interfaces and Embeddable classes passed to evict()

  • Should we throw an IllegalArgumentException for interfaces or embeddables passed into evict(class) as well?

Design

DI 1: How to get the ID when the CMPPolicy is null

        // Handle a null CMPPolicy from a MappedSuperclass
        Object identifier = null;
        if(!aDescriptor.hasCMPPolicy()) {
            // the following code gets the key either from the weaved _persistence_primaryKey field or using valueFromObject) if not weaved
            identifier = aDescriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, (AbstractSession)getSession());
        } else {
            // Get identifier via EMF
            identifier = getEntityManagerFactory().getIdentifier(object);
        }

DI 2: Weaving is not being disabled for the JPA test suite

  • Q)Why when weaving is turned of do I still see weaved entities in the JPA test suite?
  • A)The eclipselink.annotation-model.jar has a copy of all classes unless it is excluded - the weaver for each jar causes us to weave the class because it is in two jars
    <target name="package-annotation" depends="">
 
...
                     excludes="org/eclipse/persistence/testing/models/jpa/xml/**
 
...
                               org/eclipse/persistence/testing/models/jpa/metamodel/**

DI 3: Eviction of a MappedSuperclass also evicts first Entity Superclass

  • This issue has 2 parts.
    • 1) Do we support an inheritance heirarcy that involves Entity parents of MappedSuperclasses?
    • 2) How much do we invalidate when invalidating a MappedSuperclass that is not itself in the cache - but exists indirectly via implementing subclasses and because of 1) entities extended above.

Implementation

Testing

getId() when CMPPolicy is null with weaving on

Thread [Thread-3] (Suspended)	
	ObjectBuilder.extractPrimaryKeyFromObject(Object, AbstractSession, boolean) line: 1917	
	ObjectBuilder.extractPrimaryKeyFromObject(Object, AbstractSession) line: 1905	
	CacheImpl.getId(Object) line: 400	
	CacheImplJUnitTest.testGetId_fromNativeMappedSuperclass_handles_null_cmp3policy_weaving_on() line: 535

getId() when CMPPolicy is null with weaving off

Thread [Thread-3] (Suspended)	
	ObjectBuilder.extractPrimaryKeyFromObject(Object, AbstractSession, boolean) line: 1952	
	ObjectBuilder.extractPrimaryKeyFromObject(Object, AbstractSession) line: 1905	
	CacheImpl.getId(Object) line: 400	
	CacheImplJUnitTest.testGetId_fromNativeMappedSuperclass_handles_null_cmp3policy_and_null_pk_with_weaving_off() line: 618

References

Back to the top