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.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/Shared and Isolated"
(→Shared, Isolated and Protected Cache) |
|||
Line 6: | Line 6: | ||
The cache isolation levels are: | The cache isolation levels are: | ||
− | + | * Isolated - entities are only cached in the persistence context, not in the persistence unit | |
+ | * Shared - entities are cached both in the persistence context and persistence unit, read-only entities are shared and only cached in the persistence unit | ||
+ | * Protected - entities are cached both in the persistence context and persistence unit, read-only entities are isolated and cached in the persistence unit and persistence context | ||
==Isolated Cache== | ==Isolated Cache== | ||
− | 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 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 for an entity disables its 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: | Use a isolated cache to do the following: | ||
* avoid caching highly volatile data in the shared cache; | * avoid caching highly volatile data in the shared cache; | ||
* achieve serializable transaction isolation; | * achieve serializable transaction isolation; | ||
− | * use the Oracle Virtual Private Database (VPD) feature | + | * use the Oracle Virtual Private Database (VPD) feature (see [[#Oracle Virtual Private Database (VPD)|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 <code>EntityManager.clear()</code> operation is used. | 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 <code>EntityManager.clear()</code> operation is used. | ||
Line 20: | Line 22: | ||
When you use an <code>EntityManager</code> to read an isolated entity, the <code>EntityManager</code> reads the entity directly from the database and stores it in the persistence context's isolated cache. When you use an <code>EntityManager</code> to read a shared entity, the <code>EntityManager</code> 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. | When you use an <code>EntityManager</code> to read an isolated entity, the <code>EntityManager</code> reads the entity directly from the database and stores it in the persistence context's isolated cache. When you use an <code>EntityManager</code> to read a shared entity, the <code>EntityManager</code> 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. | + | The persistence context can access the data source using a connection pool or an exclusive connection. The persistence unit property <code>"eclipselink.jdbc.exclusive-connection.mode"</code> can be used to use an exclusive connection. Using an exclusive connection provides improved user-based security for reads and writes. Specific queries can also be configured to use the persistence context's exclusive connection. |
{{EclipseLink_Note | {{EclipseLink_Note | ||
|note=If an <code>EntityManager</code> contains an exclusive connection, you must close the <code>EntityManager</code> when you are finished using it. We do not recommend relying on the finalizer to release the connection when the <code>EntityManager</code> is garbage-collected. If you are using a managed persistence context, then you do not need to close it. | |note=If an <code>EntityManager</code> contains an exclusive connection, you must close the <code>EntityManager</code> when you are finished using it. We do not recommend relying on the finalizer to release the connection when the <code>EntityManager</code> is garbage-collected. If you are using a managed persistence context, then you do not need to close it. | ||
Line 27: | Line 29: | ||
===Oracle Virtual Private Database (VPD)=== | ===Oracle Virtual Private Database (VPD)=== | ||
− | + | Oracle Database Server 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: | |
<tt>http://www.oracle.com/technology/deploy/security/index.html</tt>. | <tt>http://www.oracle.com/technology/deploy/security/index.html</tt>. | ||
− | To use the Oracle Database VPD feature in your EclipseLink-enabled application, | + | To use the Oracle Database VPD feature in your EclipseLink-enabled application, an isolated cache should be used. |
− | Any | + | Any entity that maps to a table that uses VPD should have the descriptor configured as isolated. |
− | When you use | + | When you use VPD, you typically should also use exclusive connections. |
− | To support VPD, you are responsible for implementing session event handlers that the EclipseLink runtime invokes during the | + | To support VPD, you are responsible for implementing session event handlers that the EclipseLink runtime invokes during the persistence context's 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|VPD with Oracle Database Proxy Authentication]] and [[#VPD Without Oracle Database Proxy Authentication|VPD Without Oracle Database Proxy Authentication]]). |
====VPD with Oracle Database Proxy Authentication==== | ====VPD with Oracle Database Proxy Authentication==== | ||
− | If you are using Oracle Database proxy authentication, you | + | If you are using Oracle Database proxy authentication, you should implement a session event handler for the following session events: |
* <tt>noRowsModifiedSessionEvent</tt> | * <tt>noRowsModifiedSessionEvent</tt> | ||
− | By using Oracle Database proxy authentication, you can set up VPD support entirely in the database. That is, rather than | + | By using Oracle Database proxy authentication, you can set up VPD support entirely in the database. That is, rather than session event handlers to execute SQL, the database performs the required setup in an after login trigger using the proxy <tt>session_user</tt>. |
Line 53: | Line 55: | ||
* <tt>noRowsModifiedSessionEvent</tt> | * <tt>noRowsModifiedSessionEvent</tt> | ||
− | In your implementation of these handlers, you obtain the required user credentials from the | + | In your implementation of these handlers, you can obtain the required user credentials from the associated session's properties. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Revision as of 12:02, 9 May 2012
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
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:
- Isolated - entities are only cached in the persistence context, not in the persistence unit
- Shared - entities are cached both in the persistence context and persistence unit, read-only entities are shared and only cached in the persistence unit
- Protected - entities are cached both in the persistence context and persistence unit, read-only entities are isolated and cached in the persistence unit and persistence context
Isolated Cache
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 for an entity disables its 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 (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. The persistence unit property "eclipselink.jdbc.exclusive-connection.mode"
can be used to use an exclusive connection. Using an exclusive connection provides improved user-based security for reads and writes. Specific queries can also be configured to use the persistence context's exclusive connection.
Note: If an EntityManager
contains an exclusive connection, you must close the EntityManager
when you are finished using it. We do not recommend relying on the finalizer to release the connection when the EntityManager
is garbage-collected. If you are using a managed persistence context, then you do not need to close it.
Oracle Virtual Private Database (VPD)
Oracle Database Server 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:
http://www.oracle.com/technology/deploy/security/index.html.
To use the Oracle Database VPD feature in your EclipseLink-enabled application, an isolated cache should be used.
Any entity that maps to a table that uses VPD should have the descriptor configured as isolated.
When you use VPD, you typically should also use exclusive connections.
To support VPD, you are responsible for implementing session event handlers that the EclipseLink runtime invokes during the persistence context's 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 should implement a session event handler for the following session events:
- noRowsModifiedSessionEvent
By using Oracle Database proxy authentication, you can set up VPD support entirely in the database. That is, rather than session event handlers to 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.
- noRowsModifiedSessionEvent
In your implementation of these handlers, you can obtain the required user credentials from the associated session's properties.