1 /* -*- mode: java; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
4 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
20 package com.mysql.cluster.benchmark.tws;
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;
28 class JdbcLoad extends TwsLoad {
31 protected String jdbcDriver;
33 protected String username;
34 protected String password;
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;
45 public JdbcLoad(TwsDriver driver, MetaData md) {
49 // ----------------------------------------------------------------------
50 // JDBC intializers/finalizers
51 // ----------------------------------------------------------------------
53 protected void initProperties() {
55 out.print("setting jdbc properties ...");
57 final StringBuilder msg = new StringBuilder();
58 final String eol = System.getProperty("line.separator");
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");
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);
74 url = driver.props.getProperty("jdbc.url");
76 throw new RuntimeException("Missing property: jdbc.url");
79 username = driver.props.getProperty("jdbc.user");
80 password = driver.props.getProperty("jdbc.password");
82 if (msg.length() == 0) {
86 out.print(msg.toString());
89 // have url initialized first
90 descr = "jdbc(" + url + ")";
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 + "\"");
100 public void init() throws Exception {
102 assert (jdbcDriverClass == null);
104 // load the JDBC driver class
105 out.print("loading jdbc driver ...");
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);
115 out.println(" [ok: " + jdbcDriverClass.getName() + "]");
118 public void close() throws Exception {
119 assert (jdbcDriverClass != null);
122 jdbcDriverClass = null;
127 // ----------------------------------------------------------------------
128 // JDBC datastore operations
129 // ----------------------------------------------------------------------
131 public void initConnection() throws SQLException {
132 assert (jdbcDriverClass != null);
133 assert (connection == null);
136 out.println("initializing jdbc resources ...");
138 // create a connection to the database
139 out.print("starting jdbc connection ...");
142 connection = DriverManager.getConnection(url, username, password);
143 } catch (SQLException e) {
144 out.println("Cannot connect to database '" + url + "'");
145 throw new RuntimeException(e);
147 out.println(" [ok: " + url + "]");
149 out.print("setting isolation level ...");
151 // ndb storage engine only supports READ_COMMITTED
152 final int il = Connection.TRANSACTION_READ_COMMITTED;
153 connection.setTransactionIsolation(il);
155 switch (connection.getTransactionIsolation()) {
156 case Connection.TRANSACTION_READ_UNCOMMITTED:
157 out.print("READ_UNCOMMITTED");
159 case Connection.TRANSACTION_READ_COMMITTED:
160 out.print("READ_COMMITTED");
162 case Connection.TRANSACTION_REPEATABLE_READ:
163 out.print("REPEATABLE_READ");
165 case Connection.TRANSACTION_SERIALIZABLE:
166 out.print("SERIALIZABLE");
173 initPreparedStatements();
176 public void closeConnection() throws SQLException {
177 assert (connection != null);
180 out.println("releasing jdbc resources ...");
182 closePreparedStatements();
184 out.print("closing jdbc connection ...");
188 out.println(" [ok]");
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);
198 out.print("using lock mode for reads ...");
201 switch (driver.lockMode) {
206 lm = " LOCK IN share mode";
215 out.println(" [ok: " + "SELECT" + lm + ";]");
217 out.print("compiling jdbc statements ...");
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);
224 final String sqlSel0 = ("SELECT * FROM mytable where c0=?" + lm);
225 sel0 = connection.prepareStatement(sqlSel0);
227 final String sqlUpd0 = "UPDATE mytable "
228 + "SET c1 = ?, c2 = ?, c3 = ?, c5 = ?, c6 = ?, c7 = ?, c8 = ?, c9 = ?, c10 = ?, c11 = ?, c12 = ?, c13 = ?, c14 = ? "
230 upd0 = connection.prepareStatement(sqlUpd0);
232 final String sqlDel0 = "DELETE FROM mytable WHERE c0=?";
233 del0 = connection.prepareStatement(sqlDel0);
235 delAll = connection.prepareStatement("DELETE FROM mytable");
237 out.println(" [ok]");
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);
247 out.print("closing jdbc statements ...");
265 out.println(" [ok]");
268 // ----------------------------------------------------------------------
270 public void runOperations() throws SQLException {
272 out.println("running JDBC operations ..."
273 + " [nRows=" + driver.nRows + "]");
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);
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);
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);
295 // ----------------------------------------------------------------------
297 protected void runJdbcInsert(TwsDriver.XMode mode) throws SQLException {
298 final String name = "insert_" + mode.toString().toLowerCase();
301 connection.setAutoCommit(mode == TwsDriver.XMode.SINGLE);
302 for(int i = 0; i < driver.nRows; i++) {
305 if (mode == TwsDriver.XMode.BATCH)
307 if (mode != TwsDriver.XMode.SINGLE)
313 protected void jdbcInsert(int c0, TwsDriver.XMode mode) {
314 // include exception handling as part of jdbc pattern
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));
324 for(int j = 5; j < metaData.getColumnCount(); j++) {
325 width = metaData.getColumnWidth(j);
326 ins0.setString(j, fixedStr.substring(0, width));
329 if (mode == TwsDriver.XMode.BATCH) {
332 int cnt = ins0.executeUpdate();
335 } catch (SQLException e) {
336 throw new RuntimeException(e);
340 // ----------------------------------------------------------------------
342 protected void runJdbcLookup(TwsDriver.XMode mode) throws SQLException {
343 assert(mode != TwsDriver.XMode.BATCH);
345 final String name = "lookup_" + mode.toString().toLowerCase();
348 connection.setAutoCommit(mode == TwsDriver.XMode.SINGLE);
349 for(int i = 0; i < driver.nRows; i++) {
352 if (mode != TwsDriver.XMode.SINGLE)
358 protected void jdbcLookup(int c0) {
359 // include exception handling as part of jdbc pattern
361 sel0.setString(1, Integer.toString(c0)); // key
362 ResultSet resultSet = sel0.executeQuery();
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);
382 assert (!resultSet.next());
385 } catch (SQLException e) {
386 throw new RuntimeException(e);
390 // ----------------------------------------------------------------------
392 protected void runJdbcUpdate(TwsDriver.XMode mode) throws SQLException {
393 final String name = "update_" + mode.toString().toLowerCase();
396 connection.setAutoCommit(mode == TwsDriver.XMode.SINGLE);
397 for(int i = 0; i < driver.nRows; i++) {
400 if (mode == TwsDriver.XMode.BATCH)
402 if (mode != TwsDriver.XMode.SINGLE)
408 protected void jdbcUpdate(int c0, TwsDriver.XMode mode) {
409 final String str0 = Integer.toString(c0);
411 final String str1 = Integer.toString(r);
413 // include exception handling as part of jdbc pattern
415 upd0.setString(1, str1);
419 for(int j = 5; j < metaData.getColumnCount(); j++) {
420 int width = metaData.getColumnWidth(j);
421 upd0.setString(j - 1, fixedStr.substring(0, width));
424 upd0.setString(14, str0); // key
426 if (mode == TwsDriver.XMode.BATCH) {
429 int cnt = upd0.executeUpdate();
432 } catch (SQLException e) {
433 throw new RuntimeException(e);
437 // ----------------------------------------------------------------------
439 protected void runJdbcDelete(TwsDriver.XMode mode) throws SQLException {
440 final String name = "delete_" + mode.toString().toLowerCase();
443 connection.setAutoCommit(mode == TwsDriver.XMode.SINGLE);
444 for(int i = 0; i < driver.nRows; i++) {
447 if (mode == TwsDriver.XMode.BATCH)
449 if (mode != TwsDriver.XMode.SINGLE)
455 protected void jdbcDelete(int c0, TwsDriver.XMode mode) {
456 // include exception handling as part of jdbc pattern
458 final String str = Integer.toString(c0);
459 del0.setString(1, str);
460 if (mode == TwsDriver.XMode.BATCH) {
463 int cnt = del0.executeUpdate();
466 } catch (SQLException e) {
467 throw new RuntimeException(e);