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.
Difference between revisions of "EclipseLink/Examples/JPA/Oracle/Proxy"
< EclipseLink | Examples | JPA | Oracle
Line 15: | Line 15: | ||
=== Requirements === | === Requirements === | ||
− | * Access to OracleConnection (typically from OracleDataSource) | + | * Access to OracleConnection (typically from OracleDataSource) using Oracle jdbc driver version 10.1.0.2 or later. |
− | * | + | === Main Configuration === |
+ | * EntityManager uses proxy user "john" for writes and reads inside transaction. Note that reads performed outside of transaction are done through the main (non proxied) connection. | ||
+ | <source lang=java> | ||
+ | Map emProperties = new HashMap(); | ||
+ | emProperties.put("eclipselink.oracle.connection.proxy-type", OracleConnection.PROXYTYPE_USER_NAME); | ||
+ | emProperties.put(OracleConnection.PROXY_USER_NAME, "john"); | ||
+ | EntityManager em = emf.createEntityManager(emProperties); | ||
+ | // or in case of injected EntityManager | ||
+ | ((org.eclipse.persistence.internal.jpa.EntityManagerImpl)em.getDelegate()).setProperties(emProperties); | ||
+ | </source> | ||
+ | * To make EntityManager use the proxied connection also for reads outside of transaction specify in SessionCustomizer that exclusive connection should be used: | ||
+ | <source lang=java> | ||
+ | ((ServerSession)session).getDefaultConnectionPolicy().setShouldUseExclusiveConnection(true); | ||
+ | </source> | ||
+ | * Note that exclusive connection requires at least one entity using isolated cache: | ||
+ | <source lang=java> | ||
+ | // Entity named Employee uses isolated cache. | ||
+ | "eclipselink.cache.shared.Employee" -> "false" | ||
+ | // All entities use isolated cache. | ||
+ | "eclipselink.cache.shared.default" -> "false" | ||
+ | </source> | ||
− | == | + | === Other Configurations === |
+ | Proxy properties may be also used by EntityManagerFactory. In that case all connections use them - unless overridden in EntityManager. | ||
+ | <source lang=java> | ||
+ | Map factoryProperties = new HashMap(); | ||
+ | factoryProperties.put("eclipselink.oracle.connection.proxy-type", OracleConnection.PROXYTYPE_USER_NAME); | ||
+ | factoriesProperties.put(OracleConnection.PROXY_USER_NAME, "sarah"); | ||
+ | EntityManagerFactory emf = Persistence.createEntityManager(factoryProperties); | ||
− | + | // em1 doesn't specify its own proxy properties - uses proxy user "sarah" specified by the factory. | |
+ | EntityManager em1 = emf.createEntityManager(); | ||
− | = | + | // em2 uses its own proxy properties - proxy user "john", doesn't matter whether factory has proxy properties or not. |
+ | EntityManager em2 = emf.createEntityManager(emProperties); | ||
− | + | // em3 doesn't use any proxy connection - cancels proxy properties defined in the factory. | |
− | + | Map cancelProperties = new HashMap(); | |
− | + | cancelProperties.put("eclipselink.oracle.connection.proxy-type", ""); | |
− | + | EntityManager em3 = emf.createEntityManager(cancelProperties); | |
− | + | </source> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + |
Revision as of 13:33, 13 May 2008
This example is currently under development see Bug 224964 |
How to use EclipseLink JPA with Oracle Proxy Authentication
The Oracle database offers proxy authentication enabling the application to leverage a shared data source connected to the database by a single common/default user and then when used within the application the connection can be 'proxied' to be a different user. This offers the benefit of the database having knowledge of the specific user for the purposes of auditing or secure data access.
In this how-to the focus is on the usage of proxy authentication in conjunction with EclipseLink's JPA.
Contents
Overview
Requirements
- Access to OracleConnection (typically from OracleDataSource) using Oracle jdbc driver version 10.1.0.2 or later.
Main Configuration
- EntityManager uses proxy user "john" for writes and reads inside transaction. Note that reads performed outside of transaction are done through the main (non proxied) connection.
Map emProperties = new HashMap(); emProperties.put("eclipselink.oracle.connection.proxy-type", OracleConnection.PROXYTYPE_USER_NAME); emProperties.put(OracleConnection.PROXY_USER_NAME, "john"); EntityManager em = emf.createEntityManager(emProperties); // or in case of injected EntityManager ((org.eclipse.persistence.internal.jpa.EntityManagerImpl)em.getDelegate()).setProperties(emProperties);
- To make EntityManager use the proxied connection also for reads outside of transaction specify in SessionCustomizer that exclusive connection should be used:
((ServerSession)session).getDefaultConnectionPolicy().setShouldUseExclusiveConnection(true);
- Note that exclusive connection requires at least one entity using isolated cache:
// Entity named Employee uses isolated cache. "eclipselink.cache.shared.Employee" -> "false" // All entities use isolated cache. "eclipselink.cache.shared.default" -> "false"
Other Configurations
Proxy properties may be also used by EntityManagerFactory. In that case all connections use them - unless overridden in EntityManager.
Map factoryProperties = new HashMap(); factoryProperties.put("eclipselink.oracle.connection.proxy-type", OracleConnection.PROXYTYPE_USER_NAME); factoriesProperties.put(OracleConnection.PROXY_USER_NAME, "sarah"); EntityManagerFactory emf = Persistence.createEntityManager(factoryProperties); // em1 doesn't specify its own proxy properties - uses proxy user "sarah" specified by the factory. EntityManager em1 = emf.createEntityManager(); // em2 uses its own proxy properties - proxy user "john", doesn't matter whether factory has proxy properties or not. EntityManager em2 = emf.createEntityManager(emProperties); // em3 doesn't use any proxy connection - cancels proxy properties defined in the factory. Map cancelProperties = new HashMap(); cancelProperties.put("eclipselink.oracle.connection.proxy-type", ""); EntityManager em3 = emf.createEntityManager(cancelProperties);