MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JpaLoad.java
1 /* -*- mode: java; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
3  *
4  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 package com.mysql.cluster.crund;
21 
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.Arrays;
25 import java.util.ArrayList;
26 
27 import javax.persistence.Persistence;
28 import javax.persistence.EntityManagerFactory;
29 import javax.persistence.EntityManager;
30 import javax.persistence.Query;
31 import javax.persistence.PersistenceContextType;
32 
33 
37 public class JpaLoad extends CrundDriver {
38 
39  // JPA settings
40  protected String driver;
41  protected String url;
42  protected String user;
43  protected String password;
44  protected String connectionRetainMode;
45  protected String brokerFactory;
46  protected String ndbConnectString;
47  protected String ndbDatabase;
48 
49  // JPA resources
50  protected EntityManagerFactory emf;
51  protected EntityManager em;
52  protected Query delAllA;
53  protected Query delAllB0;
54 
55  // ----------------------------------------------------------------------
56  // JPA intializers/finalizers
57  // ----------------------------------------------------------------------
58 
59  protected void initProperties() {
60  super.initProperties();
61 
62  out.print("setting jpa properties ...");
63 
64  final StringBuilder msg = new StringBuilder();
65  final String eol = System.getProperty("line.separator");
66 
67  // load the JDBC driver class
68  driver = props.getProperty("openjpa.ConnectionDriverName");
69  if (driver == null) {
70  throw new RuntimeException("Missing property: "
71  + "openjpa.ConnectionDriverName");
72  }
73  try {
74  Class.forName(driver);
75  } catch (ClassNotFoundException e) {
76  out.println("Cannot load JDBC driver '" + driver
77  + "' from classpath '"
78  + System.getProperty("java.class.path") + "'");
79  throw new RuntimeException(e);
80  }
81 
82  url = props.getProperty("openjpa.ConnectionURL");
83  if (url == null) {
84  throw new RuntimeException("Missing property: "
85  + "openjpa.ConnectionURL");
86  }
87 
88  user = props.getProperty("openjpa.ConnectionUserName");
89  password = props.getProperty("openjpa.ConnectionPassword");
90 
91  connectionRetainMode = props.getProperty("openjpa.ConnectionRetainMode");
92  if (connectionRetainMode == null) {
93  throw new RuntimeException("Missing property: "
94  + "openjpa.ConnectionRetainMode");
95  }
96 
97  brokerFactory = props.getProperty("openjpa.BrokerFactory");
98  ndbConnectString = props.getProperty("openjpa.ndb.connectString");
99  ndbDatabase = props.getProperty("openjpa.ndb.database");
100  if ("ndb".equals(brokerFactory)) {
101  if (ndbConnectString == null) {
102  throw new RuntimeException("Missing property: "
103  + "openjpa.ndb.connectString");
104  }
105  if (ndbDatabase == null) {
106  throw new RuntimeException("Missing property: "
107  + "openjpa.ndb.database");
108  }
109  }
110 
111  if (msg.length() == 0) {
112  out.println(" [ok]");
113  } else {
114  out.println();
115  out.print(msg.toString());
116  }
117 
118  // have brokerFactory... initialized first
119  final String c = ("ndb".equals(brokerFactory)
120  ? ("clusterj(" + ndbConnectString + ")")
121  : url);
122  descr = "->jpa->" + c;
123  }
124 
125  protected void printProperties() {
126  super.printProperties();
127 
128  out.println();
129  out.println("jpa settings ...");
130  out.println("openjpa.ConnectionDriverName: " + driver);
131  out.println("openjpa.ConnectionURL: " + url);
132  out.println("openjpa.ConnectionUserName: \"" + user + "\"");
133  out.println("openjpa.ConnectionPassword: \"" + password + "\"");
134  out.println("openjpa.ConnectionRetainMode: " + connectionRetainMode);
135  out.println("openjpa.BrokerFactory: " + brokerFactory);
136  out.println("openjpa.ndb.connectString: " + ndbConnectString);
137  out.println("openjpa.ndb.database: " + ndbDatabase);
138  }
139 
140  protected void initLoad() throws Exception {
141  // XXX support generic load class
142  //super.init();
143 
144  out.println();
145  out.print("creating JPA EMFactory ...");
146  out.flush();
147  // create EMF by standard API, which allows vendors to pool factories
148  emf = Persistence.createEntityManagerFactory("crundjpa", props);
149  out.println(" [EMF: 1]");
150  }
151 
152  protected void closeLoad() throws Exception {
153  out.println();
154  out.print("closing JPA EMFactory ...");
155  out.flush();
156  if (emf != null)
157  emf.close();
158  emf = null;
159  out.println(" [ok]");
160 
161  // XXX support generic load class
162  //super.close();
163  }
164 
165  // ----------------------------------------------------------------------
166  // JPA operations
167  // ----------------------------------------------------------------------
168 
169  protected abstract class JpaOp extends Op {
170  public JpaOp(String name) {
171  super(name);
172  }
173 
174  public void init() {}
175 
176  public void close() {}
177  };
178 
179  protected void setCommonFields(A o, int i) {
180  assert o != null;
181  o.setCint(i);
182  o.setClong((long)i);
183  o.setCfloat((float)i);
184  o.setCdouble((double)i);
185  }
186 
187  protected void setCommonFields(B0 o, int i) {
188  assert o != null;
189  o.setCint(i);
190  o.setClong((long)i);
191  o.setCfloat((float)i);
192  o.setCdouble((double)i);
193  }
194 
195  protected void verifyCommonFields(A o, int i) {
196  assert o != null;
197  final int id = o.getId();
198  verify(id == i);
199  final int cint = o.getCint();
200  verify(cint == i);
201  final long clong = o.getClong();
202  verify(clong == i);
203  final float cfloat = o.getCfloat();
204  verify(cfloat == i);
205  final double cdouble = o.getCdouble();
206  verify(cdouble == i);
207  }
208 
209  protected void verifyCommonFields(B0 o, int i) {
210  assert o != null;
211  final int id = o.getId();
212  verify(id == i);
213  final int cint = o.getCint();
214  verify(cint == i);
215  final long clong = o.getClong();
216  verify(clong == i);
217  final float cfloat = o.getCfloat();
218  verify(cfloat == i);
219  final double cdouble = o.getCdouble();
220  verify(cdouble == i);
221  }
222 
223  protected void initOperations() {
224  out.print("initializing operations ...");
225  out.flush();
226 
227  ops.add(
228  new JpaOp("insA") {
229  public void run(int nOps) {
230  beginTransaction();
231  for (int i = 0; i < nOps; i++) {
232  final A o = new A();
233  o.setId(i);
234  em.persist(o);
235  }
236  commitTransaction();
237  }
238  });
239 
240  ops.add(
241  new JpaOp("insB0") {
242  public void run(int nOps) {
243  beginTransaction();
244  for (int i = 0; i < nOps; i++) {
245  final B0 o = new B0();
246  o.setId(i);
247  em.persist(o);
248  }
249  commitTransaction();
250  }
251  });
252 
253  ops.add(
254  new JpaOp("setAByPK_bulk") {
255  public void run(int nOps) {
256  beginTransaction();
257  // OpenJPA 1.2.1 fails to parse a unary '-' operator
258  final int upd = em.createQuery("UPDATE A o SET o.cint = 0-(o.id), o.clong = 0-(o.id), o.cfloat = 0-(o.id), o.cdouble = 0-(o.id)").executeUpdate();
259  assert upd == nOps;
260  commitTransaction();
261  }
262  });
263 
264  ops.add(
265  new JpaOp("setB0ByPK_bulk") {
266  public void run(int nOps) {
267  beginTransaction();
268  // OpenJPA 1.2.1 fails to parse a unary '-' operator
269  final int upd = em.createQuery("UPDATE B0 o SET o.cint = 0-(o.id), o.clong = 0-(o.id), o.cfloat = 0-(o.id), o.cdouble = 0-(o.id)").executeUpdate();
270  assert upd == nOps;
271  commitTransaction();
272  }
273  });
274 
275  ops.add(
276  new JpaOp("setAByPK") {
277  public void run(int nOps) {
278  beginTransaction();
279  for (int i = 0; i < nOps; i++) {
280  final A o = em.find(A.class, i);
281  setCommonFields(o, i);
282  }
283  commitTransaction();
284  }
285  });
286 
287  ops.add(
288  new JpaOp("setB0ByPK") {
289  public void run(int nOps) {
290  beginTransaction();
291  for (int i = 0; i < nOps; i++) {
292  final B0 o = em.find(B0.class, i);
293  setCommonFields(o, i);
294  }
295  commitTransaction();
296  }
297  });
298 
299  ops.add(
300  new JpaOp("getAByPK") {
301  public void run(int nOps) {
302  beginTransaction();
303  for (int i = 0; i < nOps; i++) {
304  final A o = em.find(A.class, i);
305  verifyCommonFields(o, i);
306  }
307  commitTransaction();
308  }
309  });
310 
311  ops.add(
312  new JpaOp("getB0ByPK") {
313  public void run(int nOps) {
314  beginTransaction();
315  for (int i = 0; i < nOps; i++) {
316  final B0 o = em.find(B0.class, i);
317  verifyCommonFields(o, i);
318  }
319  commitTransaction();
320  }
321  });
322 
323  for (int i = 0, l = 1; l <= maxVarbinaryBytes; l *= 10, i++) {
324  final byte[] b = bytes[i];
325  assert l == b.length;
326 
327  ops.add(
328  new JpaOp("setVarbinary" + l) {
329  public void run(int nOps) {
330  beginTransaction();
331  for (int i = 0; i < nOps; i++) {
332  final B0 o = em.find(B0.class, i);
333  assert o != null;
334  //o.cvarbinary_def = b; // not detected by OpenJPA
335  o.setCvarbinary_def(b);
336  }
337  commitTransaction();
338  }
339  });
340 
341  ops.add(
342  new JpaOp("getVarbinary" + l) {
343  public void run(int nOps) {
344  beginTransaction();
345  for (int i = 0; i < nOps; i++) {
346  final B0 o = em.find(B0.class, i);
347  assert o != null;
348  verify(Arrays.equals(b, o.getCvarbinary_def()));
349  }
350  commitTransaction();
351  }
352  });
353 
354  ops.add(
355  new JpaOp("clearVarbinary" + l) {
356  public void run(int nOps) {
357  beginTransaction();
358  for (int i = 0; i < nOps; i++) {
359  final B0 o = em.find(B0.class, i);
360  assert o != null;
361  //o.cvarbinary_def = null; // not detected by OpenJPA
362  o.setCvarbinary_def(null);
363  }
364  commitTransaction();
365  }
366  });
367  }
368 
369  for (int i = 0, l = 1; l <= maxVarcharChars; l *= 10, i++) {
370  final String s = strings[i];
371  assert l == s.length();
372 
373  ops.add(
374  new JpaOp("setVarchar" + l) {
375  public void run(int nOps) {
376  beginTransaction();
377  for (int i = 0; i < nOps; i++) {
378  final B0 o = em.find(B0.class, i);
379  assert o != null;
380  //o.cvarchar_def = s; // not detected by OpenJPA
381  o.setCvarchar_def(s);
382  }
383  commitTransaction();
384  }
385  });
386 
387  ops.add(
388  new JpaOp("getVarchar" + l) {
389  public void run(int nOps) {
390  beginTransaction();
391  for (int i = 0; i < nOps; i++) {
392  final B0 o = em.find(B0.class, i);
393  assert o != null;
394  verify(s.equals(o.getCvarchar_def()));
395  }
396  commitTransaction();
397  }
398  });
399 
400  ops.add(
401  new JpaOp("clearVarchar" + l) {
402  public void run(int nOps) {
403  beginTransaction();
404  for (int i = 0; i < nOps; i++) {
405  final B0 o = em.find(B0.class, i);
406  assert o != null;
407  //o.cvarchar_def = null; // not detected by OpenJPA
408  o.setCvarchar_def(null);
409  }
410  commitTransaction();
411  }
412  });
413  }
414 
415  ops.add(
416  new JpaOp("setB0->A") {
417  public void run(int nOps) {
418  beginTransaction();
419  for (int i = 0; i < nOps; i++) {
420  final B0 b0 = em.find(B0.class, i);
421  assert b0 != null;
422  int aId = i % nOps;
423  final A a = em.find(A.class, aId);
424  assert a != null;
425  b0.setA(a);
426  }
427  commitTransaction();
428  }
429  });
430 
431  ops.add(
432  new JpaOp("navB0->A") {
433  public void run(int nOps) {
434  beginTransaction();
435  for (int i = 0; i < nOps; i++) {
436  final B0 b0 = em.find(B0.class, i);
437  assert b0 != null;
438  final A a = b0.getA();
439  verifyCommonFields(a, i % nOps);
440  }
441  commitTransaction();
442  }
443  });
444 
445  ops.add(
446  new JpaOp("navA->B0") {
447  public void run(int nOps) {
448  beginTransaction();
449  for (int i = 0; i < nOps; i++) {
450  final A a = em.find(A.class, i);
451  assert a != null;
452  final Collection<B0> b0s = a.getB0s();
453  assert b0s != null;
454  verify(b0s.size() > 0);
455  for (B0 b0 : b0s) {
456  verifyCommonFields(b0, i % nOps);
457  }
458  }
459  commitTransaction();
460  }
461  });
462 
463  ops.add(
464  new JpaOp("nullB0->A") {
465  public void run(int nOps) {
466  beginTransaction();
467  for (int i = 0; i < nOps; i++) {
468  final B0 b0 = em.find(B0.class, i);
469  assert b0 != null;
470  b0.setA(null);
471  }
472  commitTransaction();
473  }
474  });
475 
476  // JPQL: cannot form a simple_entity_expression from an Id value
477  //ops.add(
478  // new JpaOp("setB0->A_bulk") {
479  // public void run(int nOps) {
480  // // these queries are OK but don't do what we need to:
481  // final int upd = em.createQuery("UPDATE B0 o SET o.cint = MOD(o.id, :p)").setParameter("p", nOps).executeUpdate();
482  // final int upd = em.createQuery("UPDATE B0 o SET o.a = o WHERE o.id = :id").setParameter("id", 1).executeUpdate();
483  // }
484  // });
485  final JpaOp setB0ToA = (JpaOp)ops.get(ops.size() - 4);
486  assert setB0ToA.getName().equals("setB0->A");
487  ops.add(setB0ToA
488  );
489 
490  ops.add(
491  new JpaOp("nullB0->A_bulk") {
492  public void run(int nOps) {
493  beginTransaction();
494  // OpenJPA 1.2.1 fails to parse a unary '-' operator
495  final int upd = em.createQuery("UPDATE B0 o SET o.a = NULL").executeUpdate();
496  assert upd == nOps;
497  commitTransaction();
498  }
499  });
500 
501  ops.add(
502  new JpaOp("delB0ByPK") {
503  public void run(int nOps) {
504  beginTransaction();
505  for (int i = 0; i < nOps; i++) {
506  final B0 o = em.find(B0.class, i);
507  assert o != null;
508  em.remove(o);
509  }
510  commitTransaction();
511  }
512  });
513 
514  ops.add(
515  new JpaOp("delAByPK") {
516  public void run(int nOps) {
517  beginTransaction();
518  for (int i = 0; i < nOps; i++) {
519  final A o = em.find(A.class, i);
520  assert o != null;
521  em.remove(o);
522  }
523  commitTransaction();
524  }
525  });
526 
527  ops.add(
528  new JpaOp("insA_attr") {
529  public void run(int nOps) {
530  beginTransaction();
531  for (int i = 0; i < nOps; i++) {
532  final A o = new A();
533  o.setId(i);
534  setCommonFields(o, -i);
535  em.persist(o);
536  }
537  commitTransaction();
538  }
539  });
540 
541  ops.add(
542  new JpaOp("insB0_attr") {
543  public void run(int nOps) {
544  beginTransaction();
545  for (int i = 0; i < nOps; i++) {
546  final B0 o = new B0();
547  o.setId(i);
548  setCommonFields(o, -i);
549  em.persist(o);
550  }
551  commitTransaction();
552  }
553  });
554 
555  ops.add(
556  new JpaOp("delAllB0") {
557  public void run(int nOps) {
558  beginTransaction();
559  int del = em.createQuery("DELETE FROM B0").executeUpdate();
560  assert del == nOps;
561  commitTransaction();
562  }
563  });
564 
565  ops.add(
566  new JpaOp("delAllA") {
567  public void run(int nOps) {
568  beginTransaction();
569  int del = em.createQuery("DELETE FROM A").executeUpdate();
570  assert del == nOps;
571  commitTransaction();
572  }
573  });
574 
575  // prepare queries
576  for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
577  ((JpaOp)i.next()).init();
578  }
579  out.println(" [JpaOp: " + ops.size() + "]");
580  }
581 
582  protected void closeOperations() {
583  out.print("closing operations ...");
584  out.flush();
585 
586  // close all queries
587  for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
588  ((JpaOp)i.next()).close();
589  }
590  ops.clear();
591 
592  out.println(" [ok]");
593  }
594 
595  protected void beginTransaction() {
596  em.getTransaction().begin();
597  }
598 
599  protected void commitTransaction() {
600  em.getTransaction().commit();
601  }
602 
603  // ----------------------------------------------------------------------
604  // JPA datastore operations
605  // ----------------------------------------------------------------------
606 
607  protected void initConnection() {
608  out.println();
609  out.print("creating JPA EntityManager ...");
610  out.flush();
611  // See: clearPersistenceContext() for !allowExtendedPC
612  // Tx-scope EM supported by JPA only by container injection:
613  // em = emf.createEntityManager(PersistenceContextType.TRANSACTION);
614  em = emf.createEntityManager();
615  // XXX check query.setHint(...) for standardized optimizations, e.g.:
616  //import org.eclipse.persistence.config.QueryHints;
617  //import org.eclipse.persistence.config.QueryType;
618  //import org.eclipse.persistence.config.PessimisticLock;
619  //import org.eclipse.persistence.config.HintValues;
620  //query.setHint(QueryHints.QUERY_TYPE, QueryType.ReadObject);
621  //query.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.LockNoWait);
622  //query.setHint("eclipselink.batch", "e.address");
623  //query.setHint("eclipselink.join-fetch", "e.address");
624  delAllA = em.createQuery("DELETE FROM A");
625  delAllB0 = em.createQuery("DELETE FROM B0");
626  out.println(" [EM: 1]");
627  }
628 
629  protected void closeConnection() {
630  out.println();
631  out.print("closing JPA EntityManager ...");
632  out.flush();
633  delAllB0 = null;
634  delAllA = null;
635  if (em != null)
636  em.close();
637  em = null;
638  out.println(" [ok]");
639  }
640 
641  protected void clearPersistenceContext() {
642  //out.println("clearing persistence context ...");
643  // as long as the EM was not created with a Tx PC scope, i.e.,
644  // em = emf.createEntityManager(PersistenceContextType.TRANSACTION)
645  // caching of objects beyond Tx scope can be effectively prevented
646  // by clearing the EM's PC
647  em.clear();
648  }
649 
650  protected void clearData() {
651  out.print("deleting all objects ...");
652  out.flush();
653 
654  em.getTransaction().begin();
655  int delB0 = delAllB0.executeUpdate();
656  out.print(" [B0: " + delB0);
657  out.flush();
658  int delA = delAllA.executeUpdate();
659  out.print(", A: " + delA);
660  out.flush();
661  em.getTransaction().commit();
662  em.clear();
663 
664  out.println("]");
665  }
666 
667  // ----------------------------------------------------------------------
668 
669  @SuppressWarnings("unchecked")
670  static public void main(String[] args) {
671  System.out.println("JpaLoad.main()");
672  parseArguments(args);
673  new JpaLoad().run();
674  System.out.println();
675  System.out.println("JpaLoad.main(): done.");
676  }
677 }