Want to dive even deeper?

Take the course Java 8 Fundamentals by Raoul Urma and Jim Gough and Richard Warburton and become an expert!
You're watching a preview of this video, click the button on the left to puchase the full version from Richard Warburton's Channel.

JPA Multi-Tenancy & Extensibility

One of the major themes of Java EE 7 and JPA 2.1 is multi-tenancy and EclipseLink, the JPA 2.1 reference implementation, has blazed the trail by shipping support for both multi-tenancy and tenant specific extensions in the Eclipse Indigo release. EclipseLink multi-tenancy enables the use of a single persistence unit by multiple tenants while keeping their data isolated and secure.

Published on
  • 2.986
  • 50
  • 0
  • 2
  • 0
  • Multi-Tenancy & Extensibility: Blazing the Trail to JPA 2.1 in EclipseLink Shaun Smith Eclipse Commiter/Oracle TopLink shaun.smith@oracle.com @shaunMsmith
  • About Me · From Toronto, Canada · Product Manager at Oracle for TopLink · Object-Relational Mapping since '96! · Committer on various Eclipse projects including EclipseLink 2
  • Multitenancy Wikipedia http://en.wikipedia.org/wiki/Multitenancy · Multitenancy refers to a principle in software architecture where a single instance of the software runs on a server, serving multiple client organizations (tenants). · Multitenancy is contrasted with a multi-instance architecture where separate software instances (or hardware systems) are set up for different client organizations. 3
  • Application Dev and the Cloud · Today ­Single Tenant or non-Tenant Applications ­Dedicated application instance and database · Future ­Support multiple tenants ­Support extensibility (custom fields per tenant) ­Support various deployment architectures · Dedicated or shared application instances · Dedicated or shared databases 4
  • So Many Clouds · Infrastructure - IaaS ­E.g. Amazon Web Services · Platform ­ PaaS ­E.g. Oracle Public Cloud, Cloud Bees, Google App Engine · Software ­ SaaS ­E.g. SalesForce.com, Google Mail 5
  • Java Persistence: The Problem Space Customer id: int name: String creditRating: int Java JAXB/SDO <customer id="..."> <name>...</name> ... </contact-info> </customer> XML JPA DBWS CUST ID NAME C_RATING Relational JPA: Java Persistence API JAXB: Java Architecture for XML Binding SDO: Service Data Objects 6
  • EclipseLink Project · Object-Relational: Java Persistence API (JPA) ­JPA 1.0 part of EJB 3.0 standard (JSR 220) ­JPA 2.0 standardized in JSR 317 ­EclipseLink is JPA 2.0 Reference Implementation · Object-XML: Java Architecture for XML Binding (JAXB) ­JAXB 2.2 Certified Implementation · Object-XML: Service Data Objects ­SDO 2.1.1 standardized in JSR 235 ­EclipseLink is SDO 2.1.1 Reference Implementation
  • EclipseLink Project Java SE Java EE OSGi Spring ADF JPA MOXy DBWS Databases XML Data Legacy Systems 8
  • Multitenant Topologies Application Dedicated T1 T2 T3 Shared T1 T2 T3 Dedicated Database 1 2 T2 3 T3 1 2 3 T1 T1 T2 T3 Shared 1 2 3 1 2 3 Note: Single application deployed to support various MT architectures 9
  • Multitenant: Dedicated Application Dedicated DB · Dedicated application Instance ­Application instance per tenant · unique container or application class-loader 1 2 3 T1 T2 T3 ­Caching supported · Dedicated database ­Unique tables (tablespace/schema/db) per tenant ­Tenant specific data source required 10
  • Multitenant: Shared App Dedicated DB · Shared Application Instance ­Application instances handle multiple tenants ­Caching must isolate by tenant T1 T2 T3 1 2 3 · Dedicated Database ­Common data source · Unique schema/tablespace per tenant · Common schema with table per tenant · Proxy Authentication ­Data source per tenant 11
  • Shared Database · @Multitenant ­Application's persistence layer manages access ­Row data includes tenant identifier values ­Queries augmented to limit results based on current tenant ­Database vendor independent T1 T2 T3 1 2 3 T1 T2 T3 1 2 3 · @Multitenant(VPD) ­Row data includes tenant identifier values ­Database provides client limited view of database tables · Shared solution for all database clients · Native queries (SQL) supported 12
  • Multitenant Entity Strategies · GOAL: support storage of entities from multiple tenants in a single shared schema · @Multitenant Strategies ­@Multitenant(SINGLE_TABLE) - default ­@Multitenant(VPD) · SINGLE_TABLE + includeCriteria=false · SET_IDENTIFIER(property) & CLEAR_IDENTIFIER · DDL Gen of predicate function and ADD_POLICY ­@Multitenant(TABLE_PER_TENANT) 13
  • In the beginning... · Application dedicated for single tenant · All rows available to all queries @Entity public class Player { PLAYER ID 1 2 VERSION 1 3 F_NAME John Jane L_NAME Doe Doe LEAGUE HTHL OSL 14
  • DEMO--JPA No Tenancy
  • Multitenant: SINGLE_TABLE · Simple configuration: Annotation or XML · Flexible tenant identifier support · EclipseLink augments generated SQL @Entity @Multitenant @TenantDiscriminatorColumn(name="league-id", columnName="LEAGUE") public class Player { PLAYER ID 1 2 VERSION 1 3 F_NAME John Jane L_NAME Doe Doe LEAGUE HTHL OSL 16
  • DEMO--SINGLE TABLE Multitenancy
  • Multitenant using Oracle VPD · Leverage the Oracle Database @Entity @Multitenant(VPD) @TenantDiscriminatorColumn(name="league-id", columnName="LEAGUE") public class Player { PLAYER ID 1 2 VERSION 1 3 F_NAME John Jane L_NAME Doe Doe LEAGUE HTHL OSL 18
  • Multitenant: TENANT_PER_TABLE · Planned Feature @Entity @Multitenant(TABLE_PER_TENANT) public class Player { HTHL.PLAYER ID 1 VERSION 1 F_NAME John L_NAME Doe OSL.PLAYER ID 2 VERSION 3 F_NAME Jane L_NAME Doe 19
  • Caching & Multitenancy · EntityManagerFactory/Tenant--Shared Cache Tenant 1 / Thread 1 EntityManager Persistence Context Tenant 1 / Thread 2 EntityManager Tenant 2 Thread EntityManager Persistence Context ... Persistence Context Tenant 1-EntityManagerFactory Shared Cache Tenant 2-EntityManagerFactory Shared Cache 20
  • Caching & Multitenancy · EntityManager/Tenant--Shared Cache Disabled Tenant 1 / Thread 1 EntityManager Persistence Context Tenant 1 / Thread N EntityManager Tenant 2 / Thread 1 EntityManager Persistence Context ... Persistence Context Tenant 1-EntityManagerFactory 21
  • Caching & Multitenancy · EntityManagerFactory/Tenant--Shared Cache Tenant 1 / Thread 1 EntityManager Persistence Context Tenant 1 / Thread 2 EntityManager Tenant 2 Thread EntityManager Persistence Context ... Persistence Context Tenant 1-EntityManagerFactory Shared Cache Tenant 2-EntityManagerFactory Shared Cache 20
  • MySports Demo · Introduced in EclipseLink Indigo (2.3) · Features ­@Multitenant · EMF per tenant (shared cache enabled) ­@VirtualAccessMethods (Extensions per Tenant) ­External Metadata Sources ­JSF, EJB, JPA ­Admin: JSF + JAX-RS + JPA · Wiki ­http://wiki.eclipse.org/EclipseLink/Examples/MySports 22
  • MySports Demo Model 23
  • Multi-Tenancy at Runtime App Session Scope league-id=1 App Services JPA EMF EntityManager EntityMar <<per app txn>> <<per app txn>> <<app singleton>> SELECT * FROM MYS_PLAYER WHERE L_NAME = `Doe' AND LEAGUE = 1
  • Domain Model Extensions
  • Domain Model Extensions · Storage and querying of extended properties ­Application developer enables extensions in entity ­Schema created with extension columns/table(s) ­Application Admin stores extension definitions ­Application instances made aware of extension definitions ­Application users make use of extensions Employee id firstName lastName extensions * name value 28 Map<String, Object>
  • Flex Extensions @VirtualAccessMethods public class Player{ ... @Transient private Map<String, Object> attributes; public <T> T get(String attributeName) { return (T) this.attributes.get(attributeName); } public Object set(String attributeName, Object value) { return this.attributes.put(attributeName, value); } PLAYER ID 1 2 F_NAME John Jane L_NAME Doe Smith FLEX_1 `R' `NONE' FLEX_2 '22'
  • Virtual Access Mappings <entity class="example.mysports.model.Player"> <attributes> <basic name="penaltyMinutes" access="VIRTUAL" attribute-type="java.lang.Integer"> <column name="flex_1"/> </basic> <basic name="position" access="VIRTUAL" attribute-type="java.lang.String"> <column name="flex_2"/> </basic> </attributes> </entity>
  • MySports Architecture Users Admin Create / Update / Remove Extension Definitions JSF EJB/JPA EclipseLink deploy REST -> <- XML Admin App JSF/JAX-RS/EJB/JPA EclipseLink Metadata Repository
  • EclipseLink Roadmap · Implement JPA 2.1 Multitenancy specification · Broaden Multitenancy support for other data storage architectures, e.g. Table per Tenant · Broaden Entity Extensions support for alternate metadata and extended data database architectures · Other EclipseLink Initiatives ­JSON Binding--Java/JSON marshall/unmarshall ­JPA-RS: Expose JPA data services over REST (XML or JSON) ­Resource Mapping--Declarative REST Resource Model ­EclipseLink NoSQL--JPA for NoSQL Databases 33 ­Dynamic Persistence--100% metadata driven
  • Summary · Starting thinking about SaaS/PaaS and your applications · Java EE 7 targeting Cloud deployment · JPA 2.1 to include multitenancy support · Download EclipseLink Indigo (2.3.1) ­ http://wiki.eclipse.org/EclipseLink/Examples/MySports ­ http://wiki.eclipse.org/EclipseLink/Examples/JPA/Multitenan t/Tutorial · Provide Feedback, Get Involved! ­ http://eclipse.org/eclipselink for user forums and lists

Comments

Be the first one to add a comment