Skip to main content

Notice: This Wiki is now read only and edits are no longer 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/UserGuide/JPA/2.4/a cache"

m (New page: {{EclipseLink_TLJPA |info=n |toc=n |category=JPA |release=2.4.x}} <span class="metaname"> </span> {| class="simple oac_no_warn" width="100%" | align="left" valign="top" | '''Java Pers...)
 
m
Line 3: Line 3:
 
|toc=n
 
|toc=n
 
|category=JPA
 
|category=JPA
|release=2.4.x}}
+
|release=2.4.x
 +
|title=}}
 
<span class="metaname">    </span>
 
<span class="metaname">    </span>
  
 
{| class="simple oac_no_warn" width="100%"
 
{| class="simple oac_no_warn" width="100%"
| align="left" valign="top" | '''Java Persistence API (JPA) Extensions Reference for EclipseLink'''<br />'''Version 2.4.x'''<br />
+
| align="left" valign="top" |
 
| width="144" align="right" valign="bottom" |
 
| width="144" align="right" valign="bottom" |
 
{| class="simple oac_no_warn" width="100%"
 
{| class="simple oac_no_warn" width="100%"

Revision as of 07:22, 8 June 2012


EclipseLink, 2.4.x

Search

Contents


<- Previous

Next ->


@Cache

Use @Cache) to configure the EclipseLink object cache. By default, EclipseLink uses a shared object cache to cache all objects. You can configure the caching type and options on a per class basis to allow optimal caching.

Annotation Elements

Table 2-1 describes this annotation's elements.

Table 2-1 @Cache Annotation Elements

Annotation Element Description Default

type

(Optional) Set this attribute to the type (org.eclipse.persistence.annotations.CacheType enumerated type) of the cache that you will be using:

  • FULL
  • WEAK
  • SOFT
  • SOFT_WEAK
  • HARD_WEAK
  • CACHE (not recommended)
  • NONE (not recommended, use shared=false instead)

You can override this attribute with these persistence unit properties:

  • eclipselink.cache.type.<ENTITY>
  • eclipselink.cache.type.default

CacheType.SOFT_WEAK

size

(Optional) Set this attribute to an int value to define the size of cache to use (number of objects).

100

shared

(Optional) Indicate whether cached instances should be in the shared cache or in a client isolated cache (see Isolated Client Session Cache). This allows the shared cache (L2 cache) to be disabled.

  • true —use shared cache for cached instances.
  • false—use client isolated cache for cached instances (no L2 cache).

true

expiry

(Optional) The int value to enable the expiration of the cached instance after a fixed period of time (milliseconds). Queries executed against the cache after this will be forced back to the database for a refreshed copy.

.-1 (no expiry)

expiryTimeOfDay

(Optional) Specific time of day (org.eclipse.persistence.annotations.TimeOfDay) when the cached instance will expire. Queries executed against the cache after this will be forced back to the database for a refreshed copy.

@TimeOfDay(specified=false)

alwaysRefresh

(Optional) Set to a boolean value of true to force all queries that go to the database to always refresh the cache

false

refreshOnlyIfNewer

(Optional) Set to a boolean value of true to force all queries that go to the database to refresh the cache only if the data received from the database by a query is newer than the data in the cache (as determined by the optimistic locking field).

Note Note:

  • This option only applies if one of the other refreshing options, such as alwaysRefresh, is already enabled.
  • A version field is necessary to apply this feature.

false

disableHits

(Optional) Set to a boolean value of true to force all queries to bypass the cache for hits, but still resolve against the cache for identity. This forces all queries to hit the database.

false

coordinationType

(Optional) Set this attribute to the cache coordination mode (org.eclipse.persistence.annotations.CacheCoordinationType enumerated type).

CacheCoordinationType.SEND_OBJECT_CHANGES


Usage

You can define the @Cache annotation on the following:

  • @Entity
  • @MappedSuperclass
  • the root of the inheritance hierarchy (if applicable)

If you define the @Cache annotation on an inheritance subclass, the annotation will be ignored. If you define the @Cache annotation on @Embeddable EclipseLink will throw an exception.

Caching in EclipseLink

The EclipseLink cache is an in-memory repository that stores recently read or written objects based on class and primary key values. EclipseLink uses the cache to do the following:

  • Improve performance by holding recently read or written objects and accessing them in-memory to minimize database access.
  • Manage locking and isolation level.
  • Manage object identity.

For more information about the EclipseLink cache and its default behavior, see:

EclipseLink defines the following entity caching annotations:

EclipseLink also provides a number of persistence unit properties that you can specify to configure the EclipseLink cache. These properties may compliment or provide an alternative to the usage of annotations.

For more information, see the following:

Examples

Example 2-1 illustrates an @Cache annotation.

Example 2-1 Using @Cache Annotation


...
@Entity
@Cache(
  type=CacheType.SOFT, // Cache everything until the JVM decides memory is low.
  size=64000  // Use 64,000 as the initial cache size.
  expiry=36000000,  // 10 minutes
  coordinationType=CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS  // if cache coordination is used, only send invalidation messages.
)
public class Employee {
  ...
}

Example 2-2 Using <cache> Element


<?xml version="1.0"?>
<entity-mappings
  xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm
  http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_4.xsd"
  version="2.4">
    <entity name="Employee" class="org.acme.Employee" access="FIELD">
      <cache type="SOFT" size="64000" expiry="36000000" coordination-type="INVALIDATE_CHANGED_OBJECTS"/>
    </entity>
</entity-mappings>

You can also specify caching properties at the persistence unit level (in the persistence.xml file) as shown here:

Example 2-3 Using persistence.xml


<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd"
  version="2.0">
    <persistence-unit name="acme" transaction-type="RESOURCE_LOCAL">
      <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
          <property name="eclipselink.cache.shared.default" value="false"/>
          <property name="eclipselink.cache.shared.Employee" value="true"/>
          <property name="eclipselink.cache.type.Employee" value="SOFT"/>
          <property name="eclipselink.cache.size.Employee" value="64000"/>
        </properties>
    </persistence-unit>
</persistence>

See Also

For more information, see:


<- Previous

Next ->

EclispeLink
EclipseLink Home

Search

Contents

Back to the top