Easy to Use Highly Available JavaTM Database Access REALLY??? GeeCON 2010 Poznań Craig L Russell Craig L Russell Architect, Oracle Corp. Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Presenter's "Baggage" • • • • Editor, ODMG JavaTM Specification Lead, Java Data Objects Architect, Container Managed Persistence Lead, ClusterJ and ClusterJPA Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Agenda • MySQL Cluster Overview • High Performance • High Availability • MySQL Cluster Connector for Java TM • ClusterJ and ClusterJPA Copyright © 2010, Oracle and/or its affiliates. All rights reserved. MySQL Cluster – Storage Engine • • Acquired from Ericsson in 2003, released as a MySQL Storage Engine in 2004 Originally designed as an in-memory data store for network equipment, e.g. switches Copyright © 2010, Oracle and/or its affiliates. All rights reserved. High Availability "5 9's" ~ 5 minutes down time per year Sub-second auto reconfiguration High Performance Max out 1Gb Ethernet Millions of operations per minute on commodity hardware Copyright © 2010, Oracle and/or its affiliates. All rights reserved. MySQL Cluster – Customers & Applications High Availability, Transactional Services: Web & Telecoms • • • • • • • • • • • • User & Subscriber Databases Service Delivery Platforms Application Servers Web Session Stores eCommerce VoIP, IPTV & VoD Mobile Content Delivery On-Line app stores and portals On-Line Gaming DNS/DHCP for Broadband Payment Gateways Data Store for LDAP Directories http://www.mysql.com/customers/cluster/ Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Node Failure Detection & Self-Healing Recovery Copyright © 2010, Oracle and/or its affiliates. All rights reserved. MySQL Cluster Architecture Parallel Database with no Single Point of Failure High Read & Write Performance & 99.999% uptime Clients MySQL Cluster Application Nodes MySQL Cluster Mgmt MySQL Cluster Mgmt MySQL Cluster Data Nodes Copyright © 2010, Oracle and/or its affiliates. All rights reserved. MySQL Cluster Connector for Java • MySQL Cluster Connector for Java • • New Domain Object Model Persistence API (ClusterJ) • • • • Native Java interfaces into MySQL Cluster, avoiding transformations into SQL High Performance Embedded in Java applications Uses JNI into C++ NDB API ClusterJPA provides JPA solution • • • MySQL Cluster Plug-in for OpenJPA Uses ClusterJ for PRIMARY KEY READS, INSERTS, UPDATES & DELETES JDBC used for remaining operations Key Benefits: > Up to 10x Higher Throughput with Lower Latency > Faster time to market: More natural development environment for Java programmers Copyright © 2010, Oracle and/or its affiliates. All rights reserved. ClusterJ • High Performance, Easy to Use • In the style of Hibernate / JPA / JDO • Domain Object Model DataMapper pattern – Data is represented as domain objects – Domain objects are separate from business logic – Domain objects are mapped to database tables • Does not support relationships or inheritance – Look at JDO / JPA for these modeling patterns • Tables map to Persistent Interfaces • Annotations on Interfaces define mappings • Columns map to Persistent Properties – column name defaults to property name Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 10 Cluster/J Interfaces • Session Factory Configuration properties – Instance per connection to cluster (one per Cluster per JVM) • Session & Transaction Session & Transaction Session & Transaction Domain Object Domain Object Domain Domain Object Object Domain Domain Object Object Domain Domain Object Object Domain Object Domain Object Session – Instance per user; represents a Cluster connection • Domain Domain Object Object SessionFactory Transaction – Instance per Session • Query – Multiple instances per Session Annotated interface Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 11 ClusterJ Annotated interface example @PersistenceCapable(table="employee") public interface Employee { CREATE TABLE employee ( id INT NOT NULL PRIMARY KEY, first VARCHAR(64) DEFAULT NULL, last VARCHAR(64) DEFAULT NULL, municipality VARCHAR(64) DEFAULT NULL, started VARCHAR(64) DEFAULT NULL, ended VARCHAR(64) DEFAULT NULL, department INT NOT NULL DEFAULT 1, UNIQUE KEY idx_u_hash (first,last) USING HASH, KEY idx_municipality (municipality) ) ENGINE=NDBCLUSTER; int getId(); void setId(int id); String getFirst(); void setFirst(String first); String getLast(); void setLast(String last); @Column(name="municipality") String getCity(); void setCity(String city) String getStarted(); void setStarted(String date); String getEnded(); void setEnded(String date); Integer getDepartment(); void setDepartment( Integer department);} Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 12 ClusterJ Configuration properties example com.mysql.clusterj.connectstring=192.168.0.44:1186 com.mysql.clusterj.database=clusterdb com.mysql.clusterj.connect.retries=4 com.mysql.clusterj.connect.delay=5 com.mysql.clusterj.connect.verbose=1 com.mysql.clusterj.connect.timeout.before=30 com.mysql.clusterj.connect.timeout.after=20 com.mysql.clusterj.max.transactions=1024 com.mysql.clusterj.username=root com.mysql.clusterj.password= Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 13 ClusterJ Java Type Support • • • • • • • • boolean, Boolean byte, Byte short, Short int, Integer long, Long float, Float double, Double byte[ ] Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems • • • • • • • java.util.Date java.sql.Date java.sql.Time java.sql.Timestamp java.lang.String java.math.BigDecimal java.math.BigInteger The World’s Most Popular Open Source Database 14 ClusterJ Application code example • Bootstrap via ClusterJHelper and Properties • SessionFactory is per-VM (thread-safe) • Sessions are per-user (single thread) // Create a session (connection to the database) SessionFactory factory = ClusterJHelper.getSessionFactory(props); Session session = factory.getSession(); Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 15 ClusterJ Application code example • Session is Persistent Instance Factory • Session manages life cycle – insert, delete // Create and initialize an Employee Employee newEmployee = session.newInstance(Employee.class); newEmployee.setId(988); newEmployee.setFirst("John"); newEmployee.setLast("Jones"); newEmployee.setStarted("1 February 2009"); newEmployee.setDepartment(666); // Write the Employee to the database session.persist(newEmployee); // Delete the Employee from the database session.remove(newEmployee); Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 16 ClusterJ Application code example • Session Finds by Class and Primary Key • Session Updates Instances // Fetch the Employee from the database Employee theEmployee = session.find(Employee.class, 988); // Make some changes to the Employee & write back to the database theEmployee.setDepartment(777); theEmployee.setCity("London"); session.updatePersistent(theEmployee); Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 17 ClusterJ Application code example • Session is Query Builder Factory • Query operates on Single Table (no joins) • Query is "arbitrarily complex" – attribute comparisons (including NULL) – AND, OR, NOT // Retrieve the set all of Employees in department 777 QueryBuilder builder = session.getQueryBuilder(); QueryDomainType<Employee> domain = builder.createQueryDefinition(Employee.class); domain.where(domain.get("department").equal(domain.param("department")) ); Query<Employee> query = session.createQuery(domain); query.setParameter("department",777); List<Employee> results = query.getResultList(); Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 18 ClusterJ Transactions • Without transactions, ClusterJ uses auto-transaction – each operation is in its own transaction • User can delimit transactions – ACID properties (atomic, consistent, isolated, durable) // Create and initialize Employees session.currentTransaction().begin(); for (int i = 0; i < numberOfEmployees; ++i) { Employee newEmployee = session.newInstance(Employee.class); newEmployee.setId(988); newEmployee.setFirst("First" + i); newEmployee.setLast("Last" + i); newEmployee.setStarted("1 February 2009"); newEmployee.setDepartment(666); // Write the Employee to the database session.persist(newEmployee); } session.currentTransaction().commit(); Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 19 ClusterJ Limitations • Persistent Interfaces – no persistent classes • No Relationships – primitive types only, including Large Objects (LOBs) • No Multi-table inheritance – single table per persistent interface • No joins in queries – column comparisons and boolean operators • No Table creation – user needs to create tables and indexes • No Lazy Loading – entire record is loaded at one time, including LOBs Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 20 ClusterJPA • Removes Some ClusterJ limitations: – – – – – Persistent classes Relationships Joins in queries Lazy loading Table and index creation from object model • Implemented as an OpenJPA plugin • Uses standard JDBC/MySQL code path for queries – (not simple find) • Configure as standard JDBC connection • Just add properties to persistence.xml • Better JPA performance for insert, update, delete Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 21 JPA (Java Persistence API) • The Java standard for persistence – Works in server containers and outside (not CMP!) • Choice of implementations: OpenJPA, Hibernate, TopLink, or...... – Depends on application requirements – Select the implementation via configuration file (persistence.xml) • ClusterJPA optimized for OpenJPA today – Other implementations (Hibernate, TopLink, etc) access MySQL Cluster via JDBC Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 22 ClusterJPA – persistence.xml example <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> <persistence-unit name="clusterdb" transaction-type="RESOURCE_LOCAL“> <provider> org.apache.openjpa.persistence.PersistenceProviderImpl </provider> <class>Employee</class> <class>Department</class> <properties> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" > <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/clusterdb" /> <property name="openjpa.ConnectionUserName" value="root" /> <property name="openjpa.ConnectionPassword" value="" /> <property name="openjpa.BrokerFactory" value="ndb" /> <property name="openjpa.ndb.connectString" value="localhost:1186" /> <property name="openjpa.ndb.database" value="clusterdb" / </properties> </persistence-unit> </persistence> Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 23 ClusterJPA Annotated class example @Entity(name = "department") public class Department { @Id private int Id; @Column(name="location") private String Site; public Department(){} public int getId() {return Id;} public void setId(int id) {Id=id;} mysql> describe department; +----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | location | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+ public String getSite() {return Site;} public void setSite(String site) {Site=site;} } Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 24 ClusterJPA Application code example EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("clusterdb"); EntityManager em = entityManagerFactory.createEntityManager(); EntityTransaction userTransaction = em.getTransaction(); userTransaction.begin(); Department dept = new Department(); dept.setId(101); dept.setSite(“London”); em.persist(dept); userTransaction.commit(); userTransaction.begin(); Department theDepartment = em.find(Department.class, 101); theDepartment.setSite(“Edinburgh”); userTransaction.commit(); em.close(); entityManagerFactory.close(); Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 25 Up to 10.5x Higher Throughput 1.8x 3x 3x 10.5x Copyright 2009 Sun Microsystems Copyright 2010 Sun Microsystems The World’s Most Popular Open Source Database 26 Q&A Craig L Russell Craig L Russell Architect, Oracle Corp. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.