MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JdbcLoad.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.Iterator;
23 import java.util.Arrays;
24 
25 import java.sql.SQLException;
26 import java.sql.DriverManager;
27 import java.sql.Connection;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30 
31 
35 public class JdbcLoad extends CrundDriver {
36 
37  // JDBC settings
38  protected String driver;
39  protected String url;
40  protected String user;
41  protected String password;
42 
43  // JDBC resources
44  protected Connection conn;
45  protected PreparedStatement delAllA;
46  protected PreparedStatement delAllB0;
47 
48  // ----------------------------------------------------------------------
49  // JDBC intializers/finalizers
50  // ----------------------------------------------------------------------
51 
52  protected void initProperties() {
53  super.initProperties();
54 
55  out.print("setting jdbc properties ...");
56 
57  final StringBuilder msg = new StringBuilder();
58  final String eol = System.getProperty("line.separator");
59 
60  // load the JDBC driver class
61  driver = props.getProperty("jdbc.driver");
62  if (driver == null) {
63  throw new RuntimeException("Missing property: jdbc.driver");
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("jdbc.url");
75  if (url == null) {
76  throw new RuntimeException("Missing property: jdbc.url");
77  }
78 
79  user = props.getProperty("jdbc.user");
80  password = props.getProperty("jdbc.password");
81 
82  if (msg.length() == 0) {
83  out.println(" [ok]");
84  } else {
85  out.println();
86  out.print(msg.toString());
87  }
88 
89  // have url initialized first
90  descr = "->" + url;
91  }
92 
93  protected void printProperties() {
94  super.printProperties();
95 
96  out.println();
97  out.println("jdbc settings ...");
98  out.println("jdbc.driver: " + driver);
99  out.println("jdbc.url: " + url);
100  out.println("jdbc.user: \"" + user + "\"");
101  out.println("jdbc.password: \"" + password + "\"");
102  }
103 
104  protected void initLoad() throws Exception {
105  // XXX support generic load class
106  //super.init();
107  }
108 
109  protected void closeLoad() throws Exception {
110  // XXX support generic load class
111  //super.close();
112  }
113 
114  // ----------------------------------------------------------------------
115  // JDBC operations
116  // ----------------------------------------------------------------------
117 
118  protected abstract class JdbcOp extends Op {
119  final protected String sql;
120  protected PreparedStatement stmt;
121 
122  public JdbcOp(String name, String sql) {
123  super(name);
124  this.sql = sql;
125  }
126 
127  public void init() throws SQLException {
128  if (stmt == null)
129  stmt = conn.prepareStatement(sql);
130  }
131 
132  public void close() throws SQLException {
133  if (stmt != null)
134  stmt.close();
135  stmt = null;
136  }
137  };
138 
139  static protected void setCommonAttributes(PreparedStatement stmt, int i)
140  throws SQLException {
141  stmt.setInt(2, i);
142  stmt.setLong(3, (long)i);
143  stmt.setFloat(4, (float)i);
144  stmt.setDouble(5, (double)i);
145  }
146 
147  static protected int getCommonAttributes(ResultSet rs)
148  throws SQLException {
149  final int cint = rs.getInt(2);
150  final long clong = rs.getLong(3);
151  verify(clong == cint);
152  final float cfloat = rs.getFloat(4);
153  verify(cfloat == cint);
154  final double cdouble = rs.getDouble(5);
155  verify(cdouble == cint);
156  return cint;
157  }
158 
159  protected void initOperations() throws SQLException {
160  out.print("initializing statements ...");
161  out.flush();
162 
163  for (CrundDriver.XMode m : xMode) {
164  // inner classes can only refer to a constant
165  final CrundDriver.XMode mode = m;
166 
167  ops.add(
168  new JdbcOp("insA_" + mode.toString().toLowerCase(),
169  "INSERT INTO a (id) VALUES (?)") {
170  public void run(int nOps) throws SQLException {
171  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
172  for (int i = 1; i <= nOps; i++) {
173  stmt.setInt(1, i);
174  if (mode == CrundDriver.XMode.BULK) {
175  stmt.addBatch();
176  } else {
177  int cnt = stmt.executeUpdate();
178  verify(cnt == 1);
179  }
180  }
181  if (mode == CrundDriver.XMode.BULK) {
182  int[] cnts = stmt.executeBatch();
183  for (int i = 0; i < cnts.length; i++) {
184  verify(cnts[i] == 1);
185  }
186  }
187  if (mode != CrundDriver.XMode.INDY)
188  conn.commit();
189  }
190  });
191 
192  ops.add(
193  new JdbcOp("insB0_" + mode.toString().toLowerCase(),
194  "INSERT INTO b0 (id) VALUES (?)") {
195  public void run(int nOps) throws SQLException {
196  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
197  for (int i = 1; i <= nOps; i++) {
198  stmt.setInt(1, i);
199  if (mode == CrundDriver.XMode.BULK) {
200  stmt.addBatch();
201  } else {
202  int cnt = stmt.executeUpdate();
203  verify(cnt == 1);
204  }
205  }
206  if (mode == CrundDriver.XMode.BULK) {
207  int[] cnts = stmt.executeBatch();
208  for (int i = 0; i < cnts.length; i++) {
209  verify(cnts[i] == 1);
210  }
211  }
212  if (mode != CrundDriver.XMode.INDY)
213  conn.commit();
214  }
215  });
216 
217  ops.add(
218  new JdbcOp("setAByPK_" + mode.toString().toLowerCase(),
219  "UPDATE a a SET a.cint = ?, a.clong = ?, a.cfloat = ?, a.cdouble = ? WHERE (a.id = ?)") {
220  public void run(int nOps) throws SQLException {
221  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
222  for (int i = 1; i <= nOps; i++) {
223  // refactor by numbered args
224  stmt.setInt(1, i);
225  stmt.setInt(2, i);
226  stmt.setInt(3, i);
227  stmt.setInt(4, i);
228  stmt.setInt(5, i);
229  if (mode == CrundDriver.XMode.BULK) {
230  stmt.addBatch();
231  } else {
232  int cnt = stmt.executeUpdate();
233  verify(cnt == 1);
234  }
235  }
236  if (mode == CrundDriver.XMode.BULK) {
237  int[] cnts = stmt.executeBatch();
238  for (int i = 0; i < cnts.length; i++) {
239  verify(name + " " + i, 1, cnts[i]);
240  }
241  }
242  if (mode != CrundDriver.XMode.INDY)
243  conn.commit();
244  }
245  });
246 
247  ops.add(
248  new JdbcOp("setB0ByPK_" + mode.toString().toLowerCase(),
249  "UPDATE b0 b0 SET b0.cint = ?, b0.clong = ?, b0.cfloat = ?, b0.cdouble = ? WHERE (b0.id = ?)") {
250  public void run(int nOps) throws SQLException {
251  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
252  for (int i = 1; i <= nOps; i++) {
253  // refactor by numbered args
254  stmt.setInt(1, i);
255  stmt.setInt(2, i);
256  stmt.setInt(3, i);
257  stmt.setInt(4, i);
258  stmt.setInt(5, i);
259  if (mode == CrundDriver.XMode.BULK) {
260  stmt.addBatch();
261  } else {
262  int cnt = stmt.executeUpdate();
263  verify(cnt == 1);
264  }
265  }
266  if (mode == CrundDriver.XMode.BULK) {
267  int[] cnts = stmt.executeBatch();
268  for (int i = 0; i < cnts.length; i++) {
269  verify(cnts[i] == 1);
270  }
271  }
272  if (mode != CrundDriver.XMode.INDY)
273  conn.commit();
274  }
275  });
276 
277  ops.add(
278  new JdbcOp("getAByPK_" + mode.toString().toLowerCase(),
279  "SELECT id, cint, clong, cfloat, cdouble FROM a WHERE (id = ?)") {
280  public void run(int nOps) throws SQLException {
281  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
282  for (int i = 1; i <= nOps; i++) {
283  stmt.setInt(1, i);
284  ResultSet rs = stmt.executeQuery();
285  rs.next();
286  final int id = rs.getInt(1);
287  verify(id == i);
288  final int j = getCommonAttributes(rs);
289  verify(j == id);
290  verify(!rs.next());
291  rs.close();
292  }
293  if (mode != CrundDriver.XMode.INDY)
294  conn.commit();
295  }
296  });
297 
298  ops.add(
299  new JdbcOp("getB0ByPK_" + mode.toString().toLowerCase(),
300  "SELECT id, cint, clong, cfloat, cdouble FROM b0 WHERE (id = ?)") {
301  public void run(int nOps) throws SQLException {
302  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
303  for (int i = 1; i <= nOps; i++) {
304  stmt.setInt(1, i);
305  ResultSet rs = stmt.executeQuery();
306  rs.next();
307  final int id = rs.getInt(1);
308  verify(id == i);
309  final int j = getCommonAttributes(rs);
310  verify(j == id);
311  verify(!rs.next());
312  rs.close();
313  }
314  if (mode != CrundDriver.XMode.INDY)
315  conn.commit();
316  }
317  });
318 
319  for (int i = 0, l = 1; l <= maxVarbinaryBytes; l *= 10, i++) {
320  final byte[] b = bytes[i];
321  assert l == b.length;
322 
323  ops.add(
324  new JdbcOp("setVarbinary" + l + "_" + mode.toString().toLowerCase(),
325  "UPDATE b0 b0 SET b0.cvarbinary_def = ? WHERE (b0.id = ?)") {
326  public void run(int nOps) throws SQLException {
327  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
328  for (int i = 1; i <= nOps; i++) {
329  stmt.setBytes(1, b);
330  stmt.setInt(2, i);
331  if (mode == CrundDriver.XMode.BULK) {
332  stmt.addBatch();
333  } else {
334  int cnt = stmt.executeUpdate();
335  verify(cnt == 1);
336  }
337  }
338  if (mode == CrundDriver.XMode.BULK) {
339  int[] cnts = stmt.executeBatch();
340  for (int i = 0; i < cnts.length; i++) {
341  verify(cnts[i] == 1);
342  }
343  }
344  if (mode != CrundDriver.XMode.INDY)
345  conn.commit();
346  }
347  });
348 
349  ops.add(
350  new JdbcOp("getVarbinary" + l + "_" + mode.toString().toLowerCase(),
351  "SELECT cvarbinary_def FROM b0 WHERE (id = ?)") {
352  public void run(int nOps) throws SQLException {
353  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
354  for (int i = 1; i <= nOps; i++) {
355  stmt.setInt(1, i);
356  ResultSet rs = stmt.executeQuery();
357  rs.next();
358  final byte[] r = rs.getBytes(1);
359  verify(Arrays.equals(b, r));
360  verify(!rs.next());
361  rs.close();
362  }
363  if (mode != CrundDriver.XMode.INDY)
364  conn.commit();
365  }
366  });
367 
368  ops.add(
369  new JdbcOp("clearVarbinary" + l + "_" + mode.toString().toLowerCase(),
370  "UPDATE b0 b0 SET b0.cvarbinary_def = NULL WHERE (b0.id = ?)") {
371  public void run(int nOps) throws SQLException {
372  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
373  for (int i = 1; i <= nOps; i++) {
374  stmt.setInt(1, i);
375  if (mode == CrundDriver.XMode.BULK) {
376  stmt.addBatch();
377  } else {
378  int cnt = stmt.executeUpdate();
379  verify(cnt == 1);
380  }
381  }
382  if (mode == CrundDriver.XMode.BULK) {
383  int[] cnts = stmt.executeBatch();
384  for (int i = 0; i < cnts.length; i++) {
385  verify(cnts[i] == 1);
386  }
387  }
388  if (mode != CrundDriver.XMode.INDY)
389  conn.commit();
390  }
391  });
392  }
393 
394  for (int i = 0, l = 1; l <= maxVarcharChars; l *= 10, i++) {
395  final String s = strings[i];
396  assert l == s.length();
397 
398  ops.add(
399  new JdbcOp("setVarchar" + l + "_" + mode.toString().toLowerCase(),
400  "UPDATE b0 b0 SET b0.cvarchar_def = ? WHERE (b0.id = ?)") {
401  public void run(int nOps) throws SQLException {
402  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
403  for (int i = 1; i <= nOps; i++) {
404  stmt.setString(1, s);
405  stmt.setInt(2, i);
406  if (mode == CrundDriver.XMode.BULK) {
407  stmt.addBatch();
408  } else {
409  int cnt = stmt.executeUpdate();
410  verify(cnt == 1);
411  }
412  }
413  if (mode == CrundDriver.XMode.BULK) {
414  int[] cnts = stmt.executeBatch();
415  for (int i = 0; i < cnts.length; i++) {
416  verify(cnts[i] == 1);
417  }
418  }
419  if (mode != CrundDriver.XMode.INDY)
420  conn.commit();
421  }
422  });
423 
424  ops.add(
425  new JdbcOp("getVarchar" + l + "_" + mode.toString().toLowerCase(),
426  "SELECT cvarchar_def FROM b0 WHERE (id = ?)") {
427  public void run(int nOps) throws SQLException {
428  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
429  for (int i = 1; i <= nOps; i++) {
430  stmt.setInt(1, i);
431  ResultSet rs = stmt.executeQuery();
432  rs.next();
433  final String r = rs.getString(1);
434  verify(s.equals(r));
435  verify(!rs.next());
436  rs.close();
437  }
438  if (mode != CrundDriver.XMode.INDY)
439  conn.commit();
440  }
441  });
442 
443  ops.add(
444  new JdbcOp("clearVarchar" + l + "_" + mode.toString().toLowerCase(),
445  "UPDATE b0 b0 SET b0.cvarchar_def = NULL WHERE (b0.id = ?)") {
446  public void run(int nOps) throws SQLException {
447  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
448  for (int i = 1; i <= nOps; i++) {
449  stmt.setInt(1, i);
450  if (mode == CrundDriver.XMode.BULK) {
451  stmt.addBatch();
452  } else {
453  int cnt = stmt.executeUpdate();
454  verify(cnt == 1);
455  }
456  }
457  if (mode == CrundDriver.XMode.BULK) {
458  int[] cnts = stmt.executeBatch();
459  for (int i = 0; i < cnts.length; i++) {
460  verify(cnts[i] == 1);
461  }
462  }
463  if (mode != CrundDriver.XMode.INDY)
464  conn.commit();
465  }
466  });
467  }
468 
469  if (false) { // works but not implemented in other backends yet
470  for (int i = 3, l = 1000; l <= maxBlobBytes; l *= 10, i++) {
471  final byte[] b = bytes[i];
472  assert l == b.length;
473 
474  ops.add(
475  new JdbcOp("setBlob" + l + "_" + mode.toString().toLowerCase(),
476  "UPDATE b0 b0 SET b0.cblob_def = ? WHERE (b0.id = ?)") {
477  public void run(int nOps) throws SQLException {
478  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
479  for (int i = 1; i <= nOps; i++) {
480  stmt.setBytes(1, b);
481  stmt.setInt(2, i);
482  if (mode == CrundDriver.XMode.BULK) {
483  stmt.addBatch();
484  } else {
485  int cnt = stmt.executeUpdate();
486  verify(cnt == 1);
487  }
488  }
489  if (mode == CrundDriver.XMode.BULK) {
490  int[] cnts = stmt.executeBatch();
491  for (int i = 0; i < cnts.length; i++) {
492  verify(cnts[i] == 1);
493  }
494  }
495  if (mode != CrundDriver.XMode.INDY)
496  conn.commit();
497  }
498  });
499 
500  ops.add(
501  new JdbcOp("getBlob" + l + "_" + mode.toString().toLowerCase(),
502  "SELECT cblob_def FROM b0 WHERE (id = ?)") {
503  public void run(int nOps) throws SQLException {
504  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
505  for (int i = 1; i <= nOps; i++) {
506  stmt.setInt(1, i);
507  ResultSet rs = stmt.executeQuery();
508  rs.next();
509  final byte[] r = rs.getBytes(1);
510  verify(Arrays.equals(b, r));
511  verify(!rs.next());
512  rs.close();
513  }
514  if (mode != CrundDriver.XMode.INDY)
515  conn.commit();
516  }
517  });
518  }
519  }
520 
521  if (false) { // works but not implemented in other backends yet
522  for (int i = 3, l = 1000; l <= maxTextChars; l *= 10, i++) {
523  final String s = strings[i];
524  assert l == s.length();
525 
526  ops.add(
527  new JdbcOp("setText" + l + "_" + mode.toString().toLowerCase(),
528  "UPDATE b0 b0 SET b0.ctext_def = ? WHERE (b0.id = ?)") {
529  public void run(int nOps) throws SQLException {
530  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
531  for (int i = 1; i <= nOps; i++) {
532  stmt.setString(1, s);
533  stmt.setInt(2, i);
534  if (mode == CrundDriver.XMode.BULK) {
535  stmt.addBatch();
536  } else {
537  int cnt = stmt.executeUpdate();
538  verify(cnt == 1);
539  }
540  }
541  if (mode == CrundDriver.XMode.BULK) {
542  int[] cnts = stmt.executeBatch();
543  for (int i = 0; i < cnts.length; i++) {
544  verify(cnts[i] == 1);
545  }
546  }
547  if (mode != CrundDriver.XMode.INDY)
548  conn.commit();
549  }
550  });
551 
552  ops.add(
553  new JdbcOp("getText" + l + "_" + mode.toString().toLowerCase(),
554  "SELECT ctext_def FROM b0 WHERE (id = ?)") {
555  public void run(int nOps) throws SQLException {
556  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
557  for (int i = 1; i <= nOps; i++) {
558  stmt.setInt(1, i);
559  ResultSet rs = stmt.executeQuery();
560  rs.next();
561  final String r = rs.getString(1);
562  verify(s.equals(r));
563  verify(!rs.next());
564  rs.close();
565  }
566  if (mode != CrundDriver.XMode.INDY)
567  conn.commit();
568  }
569  });
570  }
571  }
572 
573  ops.add(
574  new JdbcOp("setB0->A_" + mode.toString().toLowerCase(),
575  "UPDATE b0 b0 SET b0.a_id = ? WHERE (b0.id = ?)") {
576  public void run(int nOps) throws SQLException {
577  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
578  for (int i = 1; i <= nOps; i++) {
579  int aId = ((i - 1) % nOps) + 1;
580  stmt.setInt(1, aId);
581  stmt.setInt(2, i);
582  if (mode == CrundDriver.XMode.BULK) {
583  stmt.addBatch();
584  } else {
585  int cnt = stmt.executeUpdate();
586  verify(cnt == 1);
587  }
588  }
589  if (mode == CrundDriver.XMode.BULK) {
590  int[] cnts = stmt.executeBatch();
591  for (int i = 0; i < cnts.length; i++) {
592  verify(cnts[i] == 1);
593  }
594  }
595  if (mode != CrundDriver.XMode.INDY)
596  conn.commit();
597  }
598  });
599 
600  ops.add(
601  new JdbcOp("navB0->A_subsel_" + mode.toString().toLowerCase(),
602  "SELECT id, cint, clong, cfloat, cdouble FROM a WHERE id = (SELECT b0.a_id FROM b0 b0 WHERE b0.id = ?)") {
603  public void run(int nOps) throws SQLException {
604  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
605  for (int i = 1; i <= nOps; i++) {
606  stmt.setInt(1, i);
607  ResultSet rs = stmt.executeQuery();
608  rs.next();
609  final int id = rs.getInt(1);
610  verify(id == ((i - 1) % nOps) + 1);
611  final int j = getCommonAttributes(rs);
612  verify(j == id);
613  verify(!rs.next());
614  rs.close();
615  }
616  if (mode != CrundDriver.XMode.INDY)
617  conn.commit();
618  }
619  });
620 
621  ops.add(
622  new JdbcOp("navB0->A_joinproj_" + mode.toString().toLowerCase(),
623  "SELECT a.id, a.cint, a.clong, a.cfloat, a.cdouble FROM a a, b0 b0 WHERE (a.id = b0.a_id AND b0.id = ?)") {
624  public void run(int nOps) throws SQLException {
625  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
626  for (int i = 1; i <= nOps; i++) {
627  stmt.setInt(1, i);
628  ResultSet rs = stmt.executeQuery();
629  rs.next();
630  final int id = rs.getInt(1);
631  verify(id == ((i - 1) % nOps) + 1);
632  final int j = getCommonAttributes(rs);
633  verify(j == id);
634  verify(!rs.next());
635  rs.close();
636  }
637  if (mode != CrundDriver.XMode.INDY)
638  conn.commit();
639  }
640  });
641 
642  ops.add(
643  new JdbcOp("navB0->A_2stmts_" + mode.toString().toLowerCase(),
644  "SELECT id, cint, clong, cfloat, cdouble FROM a WHERE id = ?") {
645 
646  protected PreparedStatement stmt0;
647 
648  public void init() throws SQLException {
649  super.init();
650  stmt0 = conn.prepareStatement("SELECT a_id FROM b0 WHERE id = ?");
651  }
652 
653  public void close() throws SQLException {
654  stmt0.close();
655  stmt0 = null;
656  super.close();
657  }
658 
659  public void run(int nOps) throws SQLException {
660  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
661  for (int i = 1; i <= nOps; i++) {
662  // fetch a.id
663  stmt0.setInt(1, i);
664  ResultSet rs0 = stmt0.executeQuery();
665  verify(rs0.next());
666  int aId = rs0.getInt(1);
667  verify(aId == ((i - 1) % nOps) + 1);
668  verify(!rs0.next());
669  rs0.close();
670 
671  stmt.setInt(1, aId);
672  ResultSet rs = stmt.executeQuery();
673  rs.next();
674  final int id = rs.getInt(1);
675  verify(id == aId);
676  final int j = getCommonAttributes(rs);
677  verify(j == aId);
678  verify(!rs.next());
679  rs.close();
680  }
681  if (mode != CrundDriver.XMode.INDY)
682  conn.commit();
683  }
684  });
685 
686  ops.add(
687  new JdbcOp("navA->B0_" + mode.toString().toLowerCase(),
688  "SELECT id, cint, clong, cfloat, cdouble FROM b0 WHERE (a_id = ?)") {
689  public void run(int nOps) throws SQLException {
690  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
691  int cnt = 0;
692  for (int i = 1; i <= nOps; i++) {
693  stmt.setInt(1, i);
694  ResultSet rs = stmt.executeQuery();
695  while (rs.next()) {
696  final int id = rs.getInt(1);
697  verify(((id - 1) % nOps) + 1 == i);
698  final int j = getCommonAttributes(rs);
699  verify(j == id);
700  cnt++;
701  }
702  rs.close();
703  }
704  verify(cnt == nOps);
705  if (mode != CrundDriver.XMode.INDY)
706  conn.commit();
707  }
708  });
709 
710  ops.add(
711  new JdbcOp("nullB0->A_" + mode.toString().toLowerCase(),
712  "UPDATE b0 b0 SET b0.a_id = NULL WHERE (b0.id = ?)") {
713  public void run(int nOps) throws SQLException {
714  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
715  for (int i = 1; i <= nOps; i++) {
716  stmt.setInt(1, i);
717  if (mode == CrundDriver.XMode.BULK) {
718  stmt.addBatch();
719  } else {
720  int cnt = stmt.executeUpdate();
721  verify(cnt == 1);
722  }
723  }
724  if (mode == CrundDriver.XMode.BULK) {
725  int[] cnts = stmt.executeBatch();
726  for (int i = 0; i < cnts.length; i++) {
727  verify(cnts[i] == 1);
728  }
729  }
730  if (mode != CrundDriver.XMode.INDY)
731  conn.commit();
732  }
733  });
734 
735  ops.add(
736  // MySQL rejects this syntax: "DELETE FROM b0 b0 WHERE b0.id = ?"
737  new JdbcOp("delB0ByPK_" + mode.toString().toLowerCase(),
738  "DELETE FROM b0 WHERE id = ?") {
739  public void run(int nOps) throws SQLException {
740  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
741  for (int i = 1; i <= nOps; i++) {
742  stmt.setInt(1, i);
743  if (mode == CrundDriver.XMode.BULK) {
744  stmt.addBatch();
745  } else {
746  int cnt = stmt.executeUpdate();
747  verify(cnt == 1);
748  }
749  }
750  if (mode == CrundDriver.XMode.BULK) {
751  int[] cnts = stmt.executeBatch();
752  for (int i = 0; i < cnts.length; i++) {
753  verify(cnts[i] == 1);
754  }
755  }
756  if (mode != CrundDriver.XMode.INDY)
757  conn.commit();
758  }
759  });
760 
761  ops.add(
762  // MySQL rejects this syntax: "DELETE FROM a a WHERE a.id = ?"
763  new JdbcOp("delAByPK_" + mode.toString().toLowerCase(),
764  "DELETE FROM a WHERE id = ?") {
765  public void run(int nOps) throws SQLException {
766  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
767  for (int i = 1; i <= nOps; i++) {
768  stmt.setInt(1, i);
769  if (mode == CrundDriver.XMode.BULK) {
770  stmt.addBatch();
771  } else {
772  int cnt = stmt.executeUpdate();
773  verify(cnt == 1);
774  }
775  }
776  if (mode == CrundDriver.XMode.BULK) {
777  int[] cnts = stmt.executeBatch();
778  for (int i = 0; i < cnts.length; i++) {
779  verify(cnts[i] == 1);
780  }
781  }
782  if (mode != CrundDriver.XMode.INDY)
783  conn.commit();
784  }
785  });
786 
787  ops.add(
788  new JdbcOp("insAattr_" + mode.toString().toLowerCase(),
789  "INSERT INTO a (id, cint, clong, cfloat, cdouble) VALUES (?, ?, ?, ?, ?)") {
790  public void run(int nOps) throws SQLException {
791  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
792  for (int i = 1; i <= nOps; i++) {
793  stmt.setInt(1, i);
794  setCommonAttributes(stmt, -i);
795  if (mode == CrundDriver.XMode.BULK) {
796  stmt.addBatch();
797  } else {
798  int cnt = stmt.executeUpdate();
799  verify(cnt == 1);
800  }
801  }
802  if (mode == CrundDriver.XMode.BULK) {
803  int[] cnts = stmt.executeBatch();
804  for (int i = 0; i < cnts.length; i++) {
805  verify(cnts[i] == 1);
806  }
807  }
808  if (mode != CrundDriver.XMode.INDY)
809  conn.commit();
810  }
811  });
812 
813  ops.add(
814  new JdbcOp("insB0attr_" + mode.toString().toLowerCase(),
815  "INSERT INTO b0 (id, cint, clong, cfloat, cdouble) VALUES (?, ?, ?, ?, ?)") {
816  public void run(int nOps) throws SQLException {
817  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
818  for (int i = 1; i <= nOps; i++) {
819  stmt.setInt(1, i);
820  setCommonAttributes(stmt, -i);
821  if (mode == CrundDriver.XMode.BULK) {
822  stmt.addBatch();
823  } else {
824  int cnt = stmt.executeUpdate();
825  verify(cnt == 1);
826  }
827  }
828  if (mode == CrundDriver.XMode.BULK) {
829  int[] cnts = stmt.executeBatch();
830  for (int i = 0; i < cnts.length; i++) {
831  verify(cnts[i] == 1);
832  }
833  }
834  if (mode != CrundDriver.XMode.INDY)
835  conn.commit();
836  }
837  });
838 
839  ops.add(
840  new JdbcOp("delAllB0_" + mode.toString().toLowerCase(),
841  "DELETE FROM b0") {
842  public void run(int nOps) throws SQLException {
843  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
844  int cnt = stmt.executeUpdate();
845  verify(cnt == nOps);
846  if (mode != CrundDriver.XMode.INDY)
847  conn.commit();
848  }
849  });
850 
851  ops.add(
852  new JdbcOp("delAllA_" + mode.toString().toLowerCase(),
853  "DELETE FROM a") {
854  public void run(int nOps) throws SQLException {
855  conn.setAutoCommit(mode == CrundDriver.XMode.INDY);
856  int cnt = stmt.executeUpdate();
857  verify(cnt == nOps);
858  if (mode != CrundDriver.XMode.INDY)
859  conn.commit();
860  }
861  });
862  }
863 
864  // prepare all statements
865  for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
866  ((JdbcOp)i.next()).init();
867  }
868  out.println(" [JdbcOp: " + ops.size() + "]");
869  }
870 
871  protected void closeOperations() throws SQLException {
872  out.print("closing statements ...");
873  out.flush();
874  // close all statements
875  for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
876  ((JdbcOp)i.next()).close();
877  }
878  ops.clear();
879  out.println(" [ok]");
880  }
881 
882  // ----------------------------------------------------------------------
883  // JDBC datastore operations
884  // ----------------------------------------------------------------------
885 
886  protected void initConnection() throws SQLException {
887  assert (conn == null);
888 
889  out.println();
890  out.println("initializing jdbc resources ...");
891 
892  out.println();
893  out.print("creating jdbc connection ...");
894  out.flush();
895  conn = DriverManager.getConnection(url, user, password);
896  // XXX remove this default when fully implemented all of XMode
897  conn.setAutoCommit(false);
898  delAllA = conn.prepareStatement("DELETE FROM a");
899  delAllB0 = conn.prepareStatement("DELETE FROM b0");
900  out.println(" [ok: " + url + "]");
901 
902  out.print("setting isolation level ...");
903  out.flush();
904  // ndb storage engine only supports READ_COMMITTED
905  final int il = Connection.TRANSACTION_READ_COMMITTED;
906  conn.setTransactionIsolation(il);
907  out.print(" [ok: ");
908  switch (conn.getTransactionIsolation()) {
909  case Connection.TRANSACTION_READ_UNCOMMITTED:
910  out.print("READ_UNCOMMITTED");
911  break;
912  case Connection.TRANSACTION_READ_COMMITTED:
913  out.print("READ_COMMITTED");
914  break;
915  case Connection.TRANSACTION_REPEATABLE_READ:
916  out.print("REPEATABLE_READ");
917  break;
918  case Connection.TRANSACTION_SERIALIZABLE:
919  out.print("SERIALIZABLE");
920  break;
921  default:
922  assert false;
923  }
924  out.println("]");
925  }
926 
927  protected void closeConnection() throws SQLException {
928  assert (conn != null);
929 
930  out.println();
931  out.println("releasing jdbc resources ...");
932 
933  out.println();
934  out.print("closing jdbc connection ...");
935  out.flush();
936  if (delAllB0 != null)
937  delAllB0.close();
938  delAllB0 = null;
939  if (delAllA != null)
940  delAllA.close();
941  delAllA = null;
942  if (conn != null)
943  conn.close();
944  conn = null;
945  out.println(" [ok]");
946  }
947 
948  protected void clearPersistenceContext() {
949  // nothing to do as long as we're not caching beyond Tx scope
950  }
951 
952  protected void clearData() throws SQLException {
953  conn.setAutoCommit(false);
954  out.print("deleting all rows ...");
955  out.flush();
956  int delB0 = delAllB0.executeUpdate();
957  out.print(" [B0: " + delB0);
958  out.flush();
959  int delA = delAllA.executeUpdate();
960  out.print(", A: " + delA);
961  out.flush();
962  conn.commit();
963  out.println("]");
964  }
965 
966  // ----------------------------------------------------------------------
967 
968  static public void main(String[] args) {
969  System.out.println("JdbcLoad.main()");
970  parseArguments(args);
971  new JdbcLoad().run();
972  System.out.println();
973  System.out.println("JdbcLoad.main(): done.");
974  }
975 }