1. Scale Your JPA Applications with Distributed Database Partitions Dr. Pinaki Poddar [email_address] Session Number: D05
2.
3.
4.
5. How to obtain a Persistence Unit? Instantiate via bootstrap EntityManagerFactory construction is costly WARNING EntityManagerFactory emf = Persistence .createEntityManagerFactory( “test” ) ; InitialiContext ctx = new InitialContext(); EntityManagerFactory emf = ctx .lookup( “myEMF” ) ); Look up in JNDI @PersistenceUnit (unitName= “test” ) private EntityManagerFactory emf; Inject as a resource
6.
7. How to obtain a Persistence Context? EntityManager em = emf.createEntityManager() Construct from Persistence Unit @PersistenceContext private EntityManager em; Inject as a resource
8. Persistence Context manages instances in a group em1 = emf.createEntityManager(); em2 = emf.createEntityManager(); Account ID NAME AMOUNT SELECT ID,NAME FROM ACCOUNT t WHERE t.ID=1245 Persistence Unit Persistence Context Account pc1 = em1.find(Account.class,1245); Persistence Context Account pc2 = em2.find(Account.class,1245); 2347 John $ 12000.57 1245 Mary $ 34568.89
9.
10. Life of Persistence Context em = emf.createEntityManager(); begin(); commit(); flush(); clear(); close(); begin(); commit(); Extended Persistence Context Transactional Persistence Context Time
11.
12. Transaction Scaling in JPA em = emf.createEntityManager() begin() commit() flush() clear() close() begin() commit() Extended Persistence Context Transaction-scoped Persistence Context L2 Data Cache Database Transaction Time
13. Optimistic Versioning scales transaction begin(); commit(); pc1 = find(Item.class, 1234); qty:5 version: 56 Item:1234 qty:30 version: 56 Item:1234 begin(); commit(); pc2 = find(Item.class, 1234); qty:5 version: 56 Item:1234 qty:87 version: 56 Item:1234 UPDATE ITEM SET QTY=30, VERSION=57 WHERE ID=1234 AND VERSION=56 ID 1234 QTY 5 VERSION 56 ID 1234 QTY 30 VERSION 57 pc2.setQty(87); pc1.setQty(30); 1 3 4 2 5 6 7 9 8 UPDATE ITEM SET QTY=87, VERSION=57 WHERE ID=1234 AND VERSION=56 10 Optimistic Exception
21. Architectural Tiers of JPA-based service JPA-based User Application OpenJPA Standard JPA API JDBC API
22. Architectural Tiers of Slice-based service Slice-based User Application OpenJPA Standard JPA API JDBC API Slice OpenJPA is a plugabble platform
23. Features of Slice Slice-based User Application OpenJPA Standard JPA API JDBC API Slice No change to Application code or Domain Model User-defined Distribution & Replication Policy Flexible per-Slice Configuration Parallel Query Execution Heterogeneous Databases Master-based Sequence Targeted Query
24.
25.
26. Persistence Unit Configuration <? 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 " version="1.0" xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd "> < persistence-unit name=" test “ transaction=“ RESOURCE_LOCAL ”> < provider > org.apache.openjpa.persistence.PersistenceProviderImpl </provider> < class > domain.EntityA </class> < class > domain.EntityB </class> < properties > < property name=" openjpa.ConnectionDriverName " value=" com.mysql.jdbc.Driver "/> < property name=" openjpa.ConnectionURL " value=" jdbc:mysql://localhost/test "/> < property name=" openjpa.jdbc.SynchronizeMappings " value=" buildSchema "/> < property name=" openjpa.Log " value=" SQL=TRACE "/> </ properties > </ persistence-unit > List of known Persistent types Vendor-specific configuration Governed by XML Schema JPA Provider is pluggable META-INF/persistence.xml Identified by Unit Name
30. Distribution Policy public interface DistributionPolicy { /** * Gets the name of the slice where the given newly persistent * instance will be stored. * * @param pc The newly persistent or to-be-merged object. * @param slices name of the configured slices. * @param context persistence context managing the given instance. * * @return identifier of the slice. This name must match one of the * configured slice names. * @see DistributedConfiguration#getSliceNames() */ String distribute ( Object pc, List<String> slices, Object context); } Slice will call this method while persisting or merging a root instance. The instance and its closure will be stored in the returned slice.
31.
32.
33.
34. Replication Policy public interface ReplicationPolicy { /** * Gets the name of the slices where the given newly persistent * instance will be replicated. * * @param pc The newly persistent or to-be-merged object. * @param slices name of the configured slices. * @param context persistence context managing the given instance. * * @return identifier(s) of the slice. Each name must match one of the * configured slice names. * @see DistributedConfiguration#getSliceNames() */ String[] replicate ( Object pc, List<String> slices, Object context); } Slice will call this method while persisting any @Replicated instance.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48. Core Architectural constructs of OpenJPA EntityManager Factory BrokerFactory EntityManager Broker StoreManager JDBCStore Manager JDBC API OpenJPA Configuration creates creates delegates delegates configured by
49.
50. Slice extends OpenJPA by Distributed Template EntityManager Factory BrokerFactory EntityManager Broker DistributedStoreManager JDBCStore Manager JDBC API JDBCStore Manager JDBCStore Manager Distributed Configuration delegates delegates creates creates configures applies Distributed Template Pattern Not aware of partitioned Databases