MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JpaLoad.java
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 package com.mysql.clusterj.jpatest;
20 
21 import com.mysql.clusterj.jpatest.model.A;
22 import com.mysql.clusterj.jpatest.model.B0;
23 import java.util.Collection;
24 import java.util.Iterator;
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 
35 public class JpaLoad extends Driver {
36 
37  // JPA database connection
38  protected String driver;
39  protected String url;
40  protected EntityManagerFactory emf;
41  protected EntityManager em;
42  protected Query delAllA;
43  protected Query delAllB0;
44 
45  protected abstract class JpaOp extends Op {
46  public JpaOp(String name) {
47  super(name);
48  }
49 
50  public void init() {}
51 
52  public void close() {}
53  };
54 
55  @Override
56  protected void initProperties() {
57  super.initProperties();
58 
59  // load the JDBC driver class
60  driver = props.getProperty("openjpa.ConnectionDriverName");
61  if (driver == null) {
62  throw new RuntimeException("Missing property: "
63  + "openjpa.ConnectionDriverName");
64  }
65  try {
66  Class.forName(driver);
67  } catch (ClassNotFoundException e) {
68  out.println("Cannot load JDBC driver '" + driver
69  + "' from classpath '"
70  + System.getProperty("java.class.path") + "'");
71  throw new RuntimeException(e);
72  }
73 
74  url = props.getProperty("openjpa.ConnectionURL");
75  if (url == null) {
76  throw new RuntimeException("Missing property: "
77  + "openjpa.ConnectionURL");
78  }
79 
80  descr = "->JPA->" + url;
81  }
82 
83  @Override
84  protected void printProperties() {
85  super.printProperties();
86  out.println("openjpa.ConnectionDriverName: " + driver);
87  out.println("openjpa.ConnectionURL: " + url);
88  }
89 
90  @Override
91  protected void init() throws Exception {
92  super.init();
93  out.println();
94  out.print("creating EMFactory ...");
95  out.flush();
96  emf = Persistence.createEntityManagerFactory("crundjpa", props);
97  out.println(" [EMF: 1]");
98  }
99 
100  @Override
101  protected void close() throws Exception {
102  out.print("closing EMFactory ...");
103  out.flush();
104  if (emf != null)
105  emf.close();
106  emf = null;
107  out.println(" [ok]");
108  super.close();
109  }
110 
111  protected void initConnection() {
112  out.print("creating EntityManager ...");
113  out.flush();
114  em = emf.createEntityManager();
115  // not supported by, at least, OpenJPA (JPA spec? Hibernate?):
116  //em = emf.createEntityManager(PersistenceContextType.TRANSACTION);
117  // It seems that the only way to obtain an EntityManager with
118  // transaction persistence context is by container injection.
119  //
120  // However, if/where/when tx-PC is supported, we need to check here
121  // against property allowExtendedPC; no need then for em.clear()
122  // in clearPersistenceContext()
123  delAllA = em.createQuery("DELETE FROM A");
124  delAllB0 = em.createQuery("DELETE FROM B0");
125  out.println(" [EM: 1]");
126  }
127 
128  protected void closeConnection() {
129  out.print("closing EntityManager ...");
130  out.flush();
131  delAllB0 = null;
132  delAllA = null;
133  if (em != null)
134  em.close();
135  em = null;
136  out.println(" [ok]");
137  }
138 
139  protected int checkFields(A o) {
140  final int cint = o.getCint();
141  final long clong = o.getClong();
142  verify(clong == cint);
143  final float cfloat = o.getCfloat();
144  verify(cfloat == cint);
145  final double cdouble = o.getCdouble();
146  verify(cdouble == cint);
147  return cint;
148  }
149 
150  protected int checkFields(B0 o) {
151  final int cint = o.getCint();
152  final long clong = o.getClong();
153  verify(clong == cint);
154  final float cfloat = o.getCfloat();
155  verify(cfloat == cint);
156  final double cdouble = o.getCdouble();
157  verify(cdouble == cint);
158  return cint;
159  }
160 
161  protected void initOperations() {
162  out.print("initializing operations ...");
163  out.flush();
164 
165  ops.add(
166  new JpaOp("insA") {
167  public void run(int countA, int countB) {
168  for (int i = 0; i < countA; i++) {
169  final A o = new A();
170  o.setId(i);
171  em.persist(o);
172  }
173  }
174  });
175 
176  ops.add(
177  new JpaOp("insB0") {
178  public void run(int countA, int countB) {
179  for (int i = 0; i < countB; i++) {
180  final B0 o = new B0();
181  o.setId(i);
182  em.persist(o);
183  }
184  }
185  });
186 
187  ops.add(
188  new JpaOp("setAByPK") {
189  public void run(int countA, int countB) {
190  for (int i = 0; i < countA; i++) {
191  final A o = em.find(A.class, i);
192  assert o != null;
193  o.setCint((int)i);
194  o.setClong((long)i);
195  o.setCfloat((float)i);
196  o.setCdouble((double)i);
197  }
198  }
199  });
200 
201  ops.add(
202  new JpaOp("setB0ByPK") {
203  public void run(int countA, int countB) {
204  for (int i = 0; i < countB; i++) {
205  final B0 o = em.find(B0.class, i);
206  assert o != null;
207  o.setCint((int)i);
208  o.setClong((long)i);
209  o.setCfloat((float)i);
210  o.setCdouble((double)i);
211  }
212  }
213  });
214 
215  ops.add(
216  new JpaOp("getAByPK") {
217  public void run(int countA, int countB) {
218  for (int i = 0; i < countA; i++) {
219  final A o = em.find(A.class, i);
220  assert o != null;
221  final int id = o.getId();
222  verify(id == i);
223  final int j = checkFields(o);
224  verify(j == id);
225  }
226  }
227  });
228 
229  ops.add(
230  new JpaOp("getB0ByPK") {
231  public void run(int countA, int countB) {
232  for (int i = 0; i < countB; i++) {
233  final B0 o = em.find(B0.class, i);
234  assert o != null;
235  final int id = o.getId();
236  verify(id == i);
237  final int j = checkFields(o);
238  verify(j == id);
239  }
240  }
241  });
242 
243  ops.add(
244  new JpaOp("setB0->A") {
245  public void run(int countA, int countB) {
246  for (int i = 0; i < countB; i++) {
247  final B0 b0 = em.find(B0.class, i);
248  assert b0 != null;
249  int aId = i % countA;
250  final A a = em.find(A.class, aId);
251  assert a != null;
252  b0.setA(a);
253  }
254  }
255  });
256 
257  ops.add(
258  new JpaOp("navB0->A") {
259  public void run(int countA, int countB) {
260  for (int i = 0; i < countB; i++) {
261  final B0 b0 = em.find(B0.class, i);
262  assert b0 != null;
263  final A a = b0.getA();
264  assert a != null;
265  final int id = a.getId();
266  verify(id == i % countA);
267  final int j = checkFields(a);
268  verify(j == id);
269  }
270  }
271  });
272 
273  ops.add(
274  new JpaOp("nullB0->A") {
275  public void run(int countA, int countB) {
276  for (int i = 0; i < countB; i++) {
277  final B0 b0 = em.find(B0.class, i);
278  assert b0 != null;
279  b0.setA(null);
280  }
281  }
282  });
283 
284  ops.add(
285  new JpaOp("setA->B0") {
286  public void run(int countA, int countB) {
287  for (int i = 0; i < countA; i++) {
288  final A a = em.find(A.class, i);
289  assert a != null;
290  final Collection<B0> b0s = new ArrayList<B0>();
291  for (int j = i; j < countB; j += countA) {
292  //assert i == j % countA;
293  final B0 b0 = em.find(B0.class, j);
294  assert b0 != null;
295  b0s.add(b0);
296  }
297  a.setB0s(b0s);
298  }
299  }
300  });
301 
302  // this operation fails on an empty PersistenceContext
303  if (allowExtendedPC) {
304  ops.add(
305  new JpaOp("navA->B0") {
306  public void run(int countA, int countB) {
307  for (int i = 0; i < countA; i++) {
308  final A a = em.find(A.class, i);
309  assert a != null;
310  final Collection<B0> b0s = a.getB0s();
311  assert b0s != null;
312  // fails on an empty PC (no managed relationships)
313  verify(b0s.size() > 0);
314  for (B0 b0 : b0s) {
315  assert b0 != null;
316  final int id = b0.getId();
317  verify(id % countA == i);
318  final int j = checkFields(b0);
319  verify(j == id);
320  }
321  }
322  }
323  });
324  }
325 
326  ops.add(
327  new JpaOp("nullA->B0") {
328  public void run(int countA, int countB) {
329  for (int i = 0; i < countA; i++) {
330  final A a = em.find(A.class, i);
331  assert a != null;
332  a.setB0s(null);
333  }
334  }
335  });
336 
337  ops.add(
338  new JpaOp("delB0ByPK") {
339  public void run(int countA, int countB) {
340  for (int i = 0; i < countB; i++) {
341  final B0 o = em.find(B0.class, i);
342  assert o != null;
343  em.remove(o);
344  }
345  }
346  });
347 
348  ops.add(
349  new JpaOp("delAByPK") {
350  public void run(int countA, int countB) {
351  for (int i = 0; i < countA; i++) {
352  final A o = em.find(A.class, i);
353  assert o != null;
354  em.remove(o);
355  }
356  }
357  });
358 
359  ops.add(
360  new JpaOp("insA_attr") {
361  public void run(int countA, int countB) {
362  for (int i = 0; i < countA; i++) {
363  final A o = new A();
364  o.setId(i);
365  o.setCint((int)-i);
366  o.setClong((long)-i);
367  o.setCfloat((float)-i);
368  o.setCdouble((double)-i);
369  o.setCstring(String.valueOf(i));
370  em.persist(o);
371  }
372  }
373  });
374 
375  ops.add(
376  new JpaOp("insB0_attr") {
377  public void run(int countA, int countB) {
378  for (int i = 0; i < countB; i++) {
379  final B0 o = new B0();
380  o.setId(i);
381  o.setCint((int)-i);
382  o.setClong((long)-i);
383  o.setCfloat((float)-i);
384  o.setCdouble((double)-i);
385  o.setCstring(String.valueOf(i));
386  em.persist(o);
387  }
388  }
389  });
390 
391  ops.add(
392  new JpaOp("delAllB0") {
393  public void run(int countA, int countB) {
394  int del = em.createQuery("DELETE FROM B0").executeUpdate();
395  assert del == countB;
396  }
397  });
398 
399  ops.add(
400  new JpaOp("delAllA") {
401  public void run(int countA, int countB) {
402  int del = em.createQuery("DELETE FROM A").executeUpdate();
403  assert del == countA;
404  }
405  });
406 
407  // prepare queries
408  for (Iterator<Driver.Op> i = ops.iterator(); i.hasNext();) {
409  ((JpaOp)i.next()).init();
410  }
411  out.println(" [JpaOp: " + ops.size() + "]");
412  }
413 
414  protected void closeOperations() {
415  out.print("closing operations ...");
416  out.flush();
417 
418  // close all queries
419  for (Iterator<Driver.Op> i = ops.iterator(); i.hasNext();) {
420  ((JpaOp)i.next()).close();
421  }
422  ops.clear();
423 
424  out.println(" [ok]");
425  }
426 
427  protected void beginTransaction() {
428  em.getTransaction().begin();
429  }
430 
431  protected void commitTransaction() {
432  em.getTransaction().commit();
433  }
434 
435  protected void rollbackTransaction() {
436  em.getTransaction().rollback();
437  }
438 
439  protected void clearPersistenceContext() {
440  // as long as we haven't created the EM with a Tx PC scope
441  // (i.e. emf.createEntityManager(PersistenceContextType.TRANSACTION))
442  // we effectively prevent caching beyond Tx scope by clearing
443  // the EM's PC
444  em.clear();
445  }
446 
447  protected void clearData() {
448  out.print("deleting all objects ...");
449  out.flush();
450 
451  em.getTransaction().begin();
452  int delB0 = delAllB0.executeUpdate();
453  out.print(" [B0: " + delB0);
454  out.flush();
455  int delA = delAllA.executeUpdate();
456  out.print(", A: " + delA);
457  out.flush();
458  em.getTransaction().commit();
459  em.clear();
460 
461  out.println("]");
462  }
463 
464  // ----------------------------------------------------------------------
465 
466  static public void main(String[] args) {
468  System.out.println("JpaLoad.main()");
469  parseArguments(args);
470  new JpaLoad().run();
471  System.out.println();
472  System.out.println("JpaLoad.main(): done.");
473  }
474 }