EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/Shared and Isolated
|Mailing List • Forums • IRC • mattermost|
EclipseLink defines three cache isolation levels. The cache isolation level defines how caching for an entity is performed by the persistence unit and the persistence context.
The cache isolation levels are:
The isolated cache (L1) is the cache stored in the persistence context. It is a transactional or user session based cache. Setting the cache isolation to isolated disables the shared cache. With an isolated cache all queries and find operations will access the database unless the object has already been read into the persistence context and refreshing is not used.
Use a isolated cache to do the following:
- avoid caching highly volatile data in the shared cache;
- achieve serializable transaction isolation;
- use the Oracle Virtual Private Database (VPD) feature in your EclipseLink-enabled application (see Oracle Virtual Private Database (VPD)).
Each persistence context owns an initially empty isolated cache. The persistence context's isolated cache is discarded when the persistence context is closed, or the
EntityManager.clear() operation is used.
When you use an
EntityManager to read an isolated entity, the
EntityManager reads the entity directly from the database and stores it in the persistence context's isolated cache. When you use an
EntityManager to read a shared entity, the
EntityManager reads the shared entity from the persistence unit's shared cache. If the shared entity is not in the persistence unit's shared cache, it will read it from the database and store a copy in the persistence unit's shared cache, and another copy in the persistence context's isolated cache.
The persistence context can access the data source using a connection pool or an exclusive connection. To use an exclusive connection, acquire the
EntityManager using a ConnectionPolicy. Using an exclusive connection provides improved user-based security for reads and writes. Specific queries can also use an exclusive connection.
Oracle Virtual Private Database (VPD)
Oracle9i Database Server (and later) provides a server-enforced, fine-grained access control mechanism called Virtual Private Database (VPD). VPD ties a security policy to a table by dynamically appending SQL statements with a predicate to limit data access at the row level. You can create your own security policies, or use Oracle's custom implementation of VPD called Oracle Label Security (OLS). For more information on VPD and OLS, see the following:
To use the Oracle Database VPD feature in your EclipseLink-enabled application, use isolated client sessions.
Any class that maps to a table that uses VPD must have the descriptor configured as isolated.
When you use isolated client sessions with VPD, you typically use exclusive connections.
To support VPD, you are responsible for implementing session event handlers that the EclipseLink runtime invokes during the isolated client session life cycle (see Isolated Client Session Life Cycle). The session event handler you must implement depends on whether or not you are using Oracle Database proxy authentication (see VPD with Oracle Database Proxy Authentication and VPD Without Oracle Database Proxy Authentication).
VPD with Oracle Database Proxy Authentication
If you are using Oracle Database proxy authentication, you must implement a session event handler for the following session events:
By using Oracle Database proxy authentication, you can set up VPD support entirely in the database. That is, rather than making the isolated client session execute SQL, the database performs the required setup in an after login trigger using the proxy session_user.
VPD Without Oracle Database Proxy Authentication
If you are not using Oracle Database proxy authentication, you must implement session event handlers for the following session events:
- postAcquireExclusiveConnection: used to perform VPD setup at the time EclipseLink allocates a dedicated connection to an isolated session and before the isolated session user uses the connection to interact with the database.
- preReleaseExclusiveConnection: used to perform VPD cleanup at the time the isolated session is released and after the user is finished interacting with the database.
In your implementation of these handlers, you obtain the required user credentials from the ConnectionPolicy associated with the session.
Isolated Client Session Life Cycle
This section provides an overview of the key phases in the life cycle of an isolated session, including the following:
- Setup required before using an isolated session
- Interaction among isolated session objects
- Clean-up required after using an isolated session
To enable the life cycle of an isolated session, use this procedure:
- Prepare VPD configuration in the database.
- Configure your project and session:
- Designate descriptors as isolated.
- Configure your server session to allocate exclusive connections.
- Implement session event listeners for the required connection events:
- Acquire an isolated session:
- If you are using Oracle Database proxy authentication:
Session myIsolatedClientSession =
Because you configured one or more descriptors as isolated, myIsolatedClientSession is an isolated session with an exclusive connection.
- If you are not using Oracle Database proxy authentication:
ConnectionPolicy myConnPolicy = (ConnectionPolicy)server.getDefaultConnectionPolicy().clone(); myConnectionPolicy.setProperty("credentials", myUserCredentials); Session myIsolatedClientSession = server.acquireClientSession(myConnectionPolicy);
Set the user's credentials as appropriate properties on myConnectionPolicy. Because you configured one or more descriptors as isolated, myIsolatedClientSession is an isolated session with an exclusive connection.
The EclipseLink runtime raises a SessionEvent.PostAcquireExclusiveConnection event handled by your SessionEventListener.
- If you are using Oracle Database proxy authentication:
- Use myIsolatedClientSession to interact with the database.
If the EclipseLink runtime raises a SessionEvent.NoRowsModified event, it is handled by your SessionEventListener.
- When you are finished using myIsolatedClientSession, release the isolated session:
The EclipseLink runtime prepares to destroy the isolated cache and to close the exclusive connection associated with this isolated session.
The EclipseLink runtime raises a SessionEvent.PreReleaseExclusiveConnection event handled by your SessionEventListener.
- Repeat steps #3 to #5 (as required) until the application exits.
- Isolated objects may be related to shared objects, but shared objects cannot have any relationships with isolated objects.
- If a table has a VPD security policy associated with it, then the class mapped to that table must be isolated.
- If one of the tables in a multiple table mapping is isolated, then the main class must also be isolated.
Isolated Client Session Limitations
For the purposes of security as well as efficiency, observe the limitations described in the following section, when you use isolated client sessions in your EclipseLink three-tier application:
Consider the following mapping and relationship restrictions when using isolated sessions with your relational model:
The EclipseLink runtime enforces these restrictions during descriptor initialization.
Aggregates and aggregate mappings inherit the isolated configuration of their parents.
If a class is isolated, then all inheriting classes should be isolated. Otherwise, if you relate a shared class to a shared superclass with isolated subclasses, it is possible that some of the isolated subclasses will lose object identity when the isolated session is released.
To give you the flexibility to mix shared and isolated classes, the EclipseLink runtime does not enforce these restrictions during descriptor initialization. If you wish to mix shared and isolated classes in your inheritance hierarchy, then you must be prepared to deal with this possible loss of object identity.
Caching and Cache Coordination
Isolated classes are never loaded into the shared cache of a parent server session. Isolated classes cannot be used with cache coordination.
We recommend that you do not configure a sequencing object or sequence table using VPD security. EclipseLink does not access sequencing objects using the isolated session's dedicated connection, and so VPD restricted sequence values are not available to the isolated session. Sequence objects not using VPD security are fine.
Transactions and JTA
We recommend that you explicitly release an isolated session when you are finished using it, rather than wait for the Java garbage collector to invoke the finalizer. The finalizer is provided as a last resort: waiting for the garbage collector may cause errors when dealing with a JTA transaction.