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, 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.benchmark.tws;
21 
22 import java.sql.Connection;
23 import java.sql.DriverManager;
24 import java.sql.SQLException;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 
28 class JdbcLoad extends TwsLoad {
29 
30  // JDBC settings
31  protected String jdbcDriver;
32  protected String url;
33  protected String username;
34  protected String password;
35 
36  // JDBC resources
37  protected Class jdbcDriverClass;
38  protected Connection connection;
39  protected PreparedStatement ins0;
40  protected PreparedStatement sel0;
41  protected PreparedStatement upd0;
42  protected PreparedStatement del0;
43  protected PreparedStatement delAll;
44 
45  public JdbcLoad(TwsDriver driver, MetaData md) {
46  super(driver, md);
47  }
48 
49  // ----------------------------------------------------------------------
50  // JDBC intializers/finalizers
51  // ----------------------------------------------------------------------
52 
53  protected void initProperties() {
54  out.println();
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  jdbcDriver = driver.props.getProperty("jdbc.driver");
62  if (jdbcDriver == null) {
63  throw new RuntimeException("Missing property: jdbc.driver");
64  }
65  try {
66  Class.forName(jdbcDriver);
67  } catch (ClassNotFoundException e) {
68  out.println("Cannot load JDBC driver '" + jdbcDriver
69  + "' from classpath '"
70  + System.getProperty("java.class.path") + "'");
71  throw new RuntimeException(e);
72  }
73 
74  url = driver.props.getProperty("jdbc.url");
75  if (url == null) {
76  throw new RuntimeException("Missing property: jdbc.url");
77  }
78 
79  username = driver.props.getProperty("jdbc.user");
80  password = driver.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 = "jdbc(" + url + ")";
91  }
92 
93  protected void printProperties() {
94  out.println("jdbc.driver: " + jdbcDriver);
95  out.println("jdbc.url: " + url);
96  out.println("jdbc.user: \"" + username + "\"");
97  out.println("jdbc.password: \"" + password + "\"");
98  }
99 
100  public void init() throws Exception {
101  super.init();
102  assert (jdbcDriverClass == null);
103 
104  // load the JDBC driver class
105  out.print("loading jdbc driver ...");
106  out.flush();
107  try {
108  jdbcDriverClass = Class.forName(jdbcDriver);
109  } catch (ClassNotFoundException e) {
110  out.println("Cannot load JDBC driver '" + jdbcDriver
111  + "' from classpath '"
112  + System.getProperty("java.class.path") + "'");
113  throw new RuntimeException(e);
114  }
115  out.println(" [ok: " + jdbcDriverClass.getName() + "]");
116  }
117 
118  public void close() throws Exception {
119  assert (jdbcDriverClass != null);
120 
121  //out.println();
122  jdbcDriverClass = null;
123 
124  super.close();
125  }
126 
127  // ----------------------------------------------------------------------
128  // JDBC datastore operations
129  // ----------------------------------------------------------------------
130 
131  public void initConnection() throws SQLException {
132  assert (jdbcDriverClass != null);
133  assert (connection == null);
134 
135  out.println();
136  out.println("initializing jdbc resources ...");
137 
138  // create a connection to the database
139  out.print("starting jdbc connection ...");
140  out.flush();
141  try {
142  connection = DriverManager.getConnection(url, username, password);
143  } catch (SQLException e) {
144  out.println("Cannot connect to database '" + url + "'");
145  throw new RuntimeException(e);
146  }
147  out.println(" [ok: " + url + "]");
148 
149  out.print("setting isolation level ...");
150  out.flush();
151  // ndb storage engine only supports READ_COMMITTED
152  final int il = Connection.TRANSACTION_READ_COMMITTED;
153  connection.setTransactionIsolation(il);
154  out.print(" [ok: ");
155  switch (connection.getTransactionIsolation()) {
156  case Connection.TRANSACTION_READ_UNCOMMITTED:
157  out.print("READ_UNCOMMITTED");
158  break;
159  case Connection.TRANSACTION_READ_COMMITTED:
160  out.print("READ_COMMITTED");
161  break;
162  case Connection.TRANSACTION_REPEATABLE_READ:
163  out.print("REPEATABLE_READ");
164  break;
165  case Connection.TRANSACTION_SERIALIZABLE:
166  out.print("SERIALIZABLE");
167  break;
168  default:
169  assert false;
170  }
171  out.println("]");
172 
173  initPreparedStatements();
174  }
175 
176  public void closeConnection() throws SQLException {
177  assert (connection != null);
178 
179  out.println();
180  out.println("releasing jdbc resources ...");
181 
182  closePreparedStatements();
183 
184  out.print("closing jdbc connection ...");
185  out.flush();
186  connection.close();
187  connection = null;
188  out.println(" [ok]");
189  }
190 
191  public void initPreparedStatements() throws SQLException {
192  assert (connection != null);
193  assert (ins0 == null);
194  assert (sel0 == null);
195  assert (upd0 == null);
196  assert (del0 == null);
197 
198  out.print("using lock mode for reads ...");
199  out.flush();
200  final String lm;
201  switch (driver.lockMode) {
202  case READ_COMMITTED:
203  lm = "";
204  break;
205  case SHARED:
206  lm = " LOCK IN share mode";
207  break;
208  case EXCLUSIVE:
209  lm = " FOR UPDATE";
210  break;
211  default:
212  lm = "";
213  assert false;
214  }
215  out.println(" [ok: " + "SELECT" + lm + ";]");
216 
217  out.print("compiling jdbc statements ...");
218  out.flush();
219 
220  final String sqlIns0 = "INSERT INTO mytable (c0, c1, c2, c3, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14) "
221  + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
222  ins0 = connection.prepareStatement(sqlIns0);
223 
224  final String sqlSel0 = ("SELECT * FROM mytable where c0=?" + lm);
225  sel0 = connection.prepareStatement(sqlSel0);
226 
227  final String sqlUpd0 = "UPDATE mytable "
228  + "SET c1 = ?, c2 = ?, c3 = ?, c5 = ?, c6 = ?, c7 = ?, c8 = ?, c9 = ?, c10 = ?, c11 = ?, c12 = ?, c13 = ?, c14 = ? "
229  + "WHERE c0=?";
230  upd0 = connection.prepareStatement(sqlUpd0);
231 
232  final String sqlDel0 = "DELETE FROM mytable WHERE c0=?";
233  del0 = connection.prepareStatement(sqlDel0);
234 
235  delAll = connection.prepareStatement("DELETE FROM mytable");
236 
237  out.println(" [ok]");
238  }
239 
240  protected void closePreparedStatements() throws SQLException {
241  assert (ins0 != null);
242  assert (sel0 != null);
243  assert (upd0 != null);
244  assert (del0 != null);
245  assert (delAll != null);
246 
247  out.print("closing jdbc statements ...");
248  out.flush();
249 
250  ins0.close();
251  ins0 = null;
252 
253  sel0.close();
254  sel0 = null;
255 
256  upd0.close();
257  upd0 = null;
258 
259  del0.close();
260  del0 = null;
261 
262  delAll.close();
263  delAll = null;
264 
265  out.println(" [ok]");
266  }
267 
268  // ----------------------------------------------------------------------
269 
270  public void runOperations() throws SQLException {
271  out.println();
272  out.println("running JDBC operations ..."
273  + " [nRows=" + driver.nRows + "]");
274 
275  if (driver.doSingle) {
276  if (driver.doInsert) runJdbcInsert(TwsDriver.XMode.SINGLE);
277  if (driver.doLookup) runJdbcLookup(TwsDriver.XMode.SINGLE);
278  if (driver.doUpdate) runJdbcUpdate(TwsDriver.XMode.SINGLE);
279  if (driver.doDelete) runJdbcDelete(TwsDriver.XMode.SINGLE);
280  }
281  if (driver.doBulk) {
282  if (driver.doInsert) runJdbcInsert(TwsDriver.XMode.BULK);
283  if (driver.doLookup) runJdbcLookup(TwsDriver.XMode.BULK);
284  if (driver.doUpdate) runJdbcUpdate(TwsDriver.XMode.BULK);
285  if (driver.doDelete) runJdbcDelete(TwsDriver.XMode.BULK);
286  }
287  if (driver.doBatch) {
288  if (driver.doInsert) runJdbcInsert(TwsDriver.XMode.BATCH);
289  //if (driver.doLookup) runJdbcLookup(TwsDriver.XMode.BATCH);
290  if (driver.doUpdate) runJdbcUpdate(TwsDriver.XMode.BATCH);
291  if (driver.doDelete) runJdbcDelete(TwsDriver.XMode.BATCH);
292  }
293  }
294 
295  // ----------------------------------------------------------------------
296 
297  protected void runJdbcInsert(TwsDriver.XMode mode) throws SQLException {
298  final String name = "insert_" + mode.toString().toLowerCase();
299  driver.begin(name);
300 
301  connection.setAutoCommit(mode == TwsDriver.XMode.SINGLE);
302  for(int i = 0; i < driver.nRows; i++) {
303  jdbcInsert(i, mode);
304  }
305  if (mode == TwsDriver.XMode.BATCH)
306  ins0.executeBatch();
307  if (mode != TwsDriver.XMode.SINGLE)
308  connection.commit();
309 
310  driver.finish(name);
311  }
312 
313  protected void jdbcInsert(int c0, TwsDriver.XMode mode) {
314  // include exception handling as part of jdbc pattern
315  try {
316  final int i = c0;
317  final String str = Integer.toString(i);
318  ins0.setString(1, str); // key
319  int width = metaData.getColumnWidth(1);
320  ins0.setString(2, fixedStr.substring(0, width));
321  ins0.setInt(3, i);
322  ins0.setInt(4, i);
323 
324  for(int j = 5; j < metaData.getColumnCount(); j++) {
325  width = metaData.getColumnWidth(j);
326  ins0.setString(j, fixedStr.substring(0, width));
327  }
328 
329  if (mode == TwsDriver.XMode.BATCH) {
330  ins0.addBatch();
331  } else {
332  int cnt = ins0.executeUpdate();
333  assert (cnt == 1);
334  }
335  } catch (SQLException e) {
336  throw new RuntimeException(e);
337  }
338  }
339 
340  // ----------------------------------------------------------------------
341 
342  protected void runJdbcLookup(TwsDriver.XMode mode) throws SQLException {
343  assert(mode != TwsDriver.XMode.BATCH);
344 
345  final String name = "lookup_" + mode.toString().toLowerCase();
346  driver.begin(name);
347 
348  connection.setAutoCommit(mode == TwsDriver.XMode.SINGLE);
349  for(int i = 0; i < driver.nRows; i++) {
350  jdbcLookup(i);
351  }
352  if (mode != TwsDriver.XMode.SINGLE)
353  connection.commit();
354 
355  driver.finish(name);
356  }
357 
358  protected void jdbcLookup(int c0) {
359  // include exception handling as part of jdbc pattern
360  try {
361  sel0.setString(1, Integer.toString(c0)); // key
362  ResultSet resultSet = sel0.executeQuery();
363 
364  if (resultSet.next()) {
365  // not verifying at this time
366  String ac0 = resultSet.getString(1);
367  String c1 = resultSet.getString(2);
368  int c2 = resultSet.getInt(3);
369  int c3 = resultSet.getInt(4);
370  int c4 = resultSet.getInt(5);
371  String c5 = resultSet.getString(6);
372  String c6 = resultSet.getString(7);
373  String c7 = resultSet.getString(8);
374  String c8 = resultSet.getString(9);
375  String c9 = resultSet.getString(10);
376  String c10 = resultSet.getString(11);
377  String c11 = resultSet.getString(12);
378  String c12 = resultSet.getString(13);
379  String c13 = resultSet.getString(14);
380  String c14 = resultSet.getString(15);
381  }
382  assert (!resultSet.next());
383 
384  resultSet.close();
385  } catch (SQLException e) {
386  throw new RuntimeException(e);
387  }
388  }
389 
390  // ----------------------------------------------------------------------
391 
392  protected void runJdbcUpdate(TwsDriver.XMode mode) throws SQLException {
393  final String name = "update_" + mode.toString().toLowerCase();
394  driver.begin(name);
395 
396  connection.setAutoCommit(mode == TwsDriver.XMode.SINGLE);
397  for(int i = 0; i < driver.nRows; i++) {
398  jdbcUpdate(i, mode);
399  }
400  if (mode == TwsDriver.XMode.BATCH)
401  upd0.executeBatch();
402  if (mode != TwsDriver.XMode.SINGLE)
403  connection.commit();
404 
405  driver.finish(name);
406  }
407 
408  protected void jdbcUpdate(int c0, TwsDriver.XMode mode) {
409  final String str0 = Integer.toString(c0);
410  final int r = -c0;
411  final String str1 = Integer.toString(r);
412 
413  // include exception handling as part of jdbc pattern
414  try {
415  upd0.setString(1, str1);
416  upd0.setInt(2, r);
417  upd0.setInt(3, r);
418 
419  for(int j = 5; j < metaData.getColumnCount(); j++) {
420  int width = metaData.getColumnWidth(j);
421  upd0.setString(j - 1, fixedStr.substring(0, width));
422  }
423 
424  upd0.setString(14, str0); // key
425 
426  if (mode == TwsDriver.XMode.BATCH) {
427  upd0.addBatch();
428  } else {
429  int cnt = upd0.executeUpdate();
430  assert (cnt == 1);
431  }
432  } catch (SQLException e) {
433  throw new RuntimeException(e);
434  }
435  }
436 
437  // ----------------------------------------------------------------------
438 
439  protected void runJdbcDelete(TwsDriver.XMode mode) throws SQLException {
440  final String name = "delete_" + mode.toString().toLowerCase();
441  driver.begin(name);
442 
443  connection.setAutoCommit(mode == TwsDriver.XMode.SINGLE);
444  for(int i = 0; i < driver.nRows; i++) {
445  jdbcDelete(i, mode);
446  }
447  if (mode == TwsDriver.XMode.BATCH)
448  del0.executeBatch();
449  if (mode != TwsDriver.XMode.SINGLE)
450  connection.commit();
451 
452  driver.finish(name);
453  }
454 
455  protected void jdbcDelete(int c0, TwsDriver.XMode mode) {
456  // include exception handling as part of jdbc pattern
457  try {
458  final String str = Integer.toString(c0);
459  del0.setString(1, str);
460  if (mode == TwsDriver.XMode.BATCH) {
461  del0.addBatch();
462  } else {
463  int cnt = del0.executeUpdate();
464  assert (cnt == 1);
465  }
466  } catch (SQLException e) {
467  throw new RuntimeException(e);
468  }
469  }
470 }