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;
37 protected Class jdbcDriverClass;
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) {
53 protected void initProperties() {
55 out.print(
"setting jdbc properties ...");
57 final StringBuilder
msg =
new StringBuilder();
58 final String eol = System.getProperty(
"line.separator");
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());
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);
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;
131 public void initConnection() throws SQLException {
132 assert (jdbcDriverClass != null);
133 assert (connection == null);
136 out.println(
"initializing jdbc resources ...");
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 ...");
152 final int il =
Connection.TRANSACTION_READ_COMMITTED;
153 connection.setTransactionIsolation(il);
155 switch (connection.getTransactionIsolation()) {
157 out.print(
"READ_UNCOMMITTED");
160 out.print(
"READ_COMMITTED");
163 out.print(
"REPEATABLE_READ");
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]");
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);
290 if (driver.doUpdate) runJdbcUpdate(
TwsDriver.XMode.BATCH);
291 if (driver.doDelete) runJdbcDelete(
TwsDriver.XMode.BATCH);
297 protected void runJdbcInsert(
TwsDriver.XMode
mode) throws SQLException {
302 for(
int i = 0;
i < driver.nRows;
i++) {
317 final String str = Integer.toString(i);
318 ins0.setString(1, str);
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));
332 int cnt = ins0.executeUpdate();
335 }
catch (SQLException e) {
336 throw new RuntimeException(e);
342 protected void runJdbcLookup(
TwsDriver.XMode
mode) throws SQLException {
345 final String name =
"lookup_" +
mode.toString().toLowerCase();
349 for(
int i = 0; i < driver.nRows; i++) {
358 protected void jdbcLookup(
int c0) {
361 sel0.setString(1, Integer.toString(c0));
362 ResultSet resultSet = sel0.executeQuery();
364 if (resultSet.next()) {
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);
392 protected void runJdbcUpdate(
TwsDriver.XMode
mode) throws SQLException {
393 final String name =
"update_" +
mode.toString().toLowerCase();
397 for(
int i = 0; i < driver.nRows; i++) {
409 final String str0 = Integer.toString(c0);
411 final String str1 = Integer.toString(r);
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);
429 int cnt = upd0.executeUpdate();
432 }
catch (SQLException e) {
433 throw new RuntimeException(e);
439 protected void runJdbcDelete(
TwsDriver.XMode
mode) throws SQLException {
440 final String name =
"delete_" +
mode.toString().toLowerCase();
444 for(
int i = 0; i < driver.nRows; i++) {
458 final String str = Integer.toString(c0);
459 del0.setString(1, str);
463 int cnt = del0.executeUpdate();
466 }
catch (SQLException e) {
467 throw new RuntimeException(e);