1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
/*
Derby - Class org.apache.derbyTesting.system.nstest.utils.DbUtil
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.system.nstest.utils;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Random;
import org.apache.derbyTesting.system.nstest.NsTest;
/**
* DbUtil - a database utility class for all IUD and Select operations
*/
public class DbUtil {
private String threadName = "";
public static final int TCHAR = 0;
public static final int TDATE = 1;
public static final int TDECIMAL = 2;
public static final int TDECIMALNN = 3;
public static final int TDOUBLE = 4;
public static final int TFLOAT = 5;
public static final int TINT = 6;
public static final int TLONGINT = 7;
public static final int TNUMERICLARGE = 8;
public static final int TREAL = 9;
public static final int TSMALLINT = 10;
public static final int TTIME = 11;
public static final int TTIMESTAMP = 12;
public static final int TVARCHAR = 13;
public static final int NUMTYPES = 14;
public static String[] colnames = { "t_char", "t_date", "t_decimal",
"t_decimal_nn", "t_double", "t_float", "t_int", "t_longint",
"t_numeric_large", "t_real", "t_smallint", "t_time", "t_timestamp",
"t_varchar" };
public DbUtil(String thName) {
threadName = thName;
}
/*
* Add a row for each iteration
*/
public int add_one_row(Connection conn, String thread_id) throws Exception {
PreparedStatement ps = null;
int rowsAdded = 0;
try {
// autoincrement feature added, so we need to specify the column
// name for prepared statement, otherwise auto increment column
// will think it is trying to update/insert a null value to the
// column.
ps = conn
.prepareStatement(" insert into nstesttab (id, t_char,"
+ " t_date, t_decimal, t_decimal_nn, t_double, "
+ " t_float, t_int, t_longint, t_numeric_large,"
+ " t_real, t_smallint, t_time, t_timestamp,"
+ " t_varchar,t_clob,t_blob,sequenceColumn) values ("
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,cast('00000000000000000000000000000000031' as clob(1K)),cast(X'000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031' as blob(10K)), next value for nstesttab_seq)");
Random rand = new Random();
int ind = rand.nextInt();
int id_ind = ind;
Date dt = new Date(1);
Time tt = new Time(1);
Timestamp ts = new Timestamp(1);
String cs = "asdf qwerqwer 12341234 ZXCVZXCVZXCV !@#$!@#$ asdfasdf 1 q a z asdf ASDF qwerasdfzxcvasdfqwer1234asd#";
// Integer ji = null;
// Set value of column "id"
ps.setInt(1, ind);
// NsTest.logger.println("set int col 1 to " + ind);
// Set value of column "t_char"
// scramble the string
int i1 = Math.abs(ind % 100);
String cs2 = cs.substring(i1, 99) + cs.substring(0, i1);
int i2 = i1 < 89 ? i1 + 10 : i1;
ps.setString(2, cs2.substring(0, i2));
// NsTest.logger.println("set t_Char to " + cs2.substring(0,i2));
// NsTest.logger.println("now setting date");
// Set value of column "t_date"
dt.setTime(Math.abs(rand.nextLong() / 150000));
ps.setDate(3, dt);
// NsTest.logger.println("set t_date to " + dt.toString());
// Set value of column "t_decimal"
double t_dec = rand.nextDouble()
* Math.pow(10, Math.abs(rand.nextInt() % 6));
ps.setDouble(4, t_dec);
// NsTest.logger.println("set t_decimal to "+ t_dec);
// Set value of column "t_decimal_nn"
double t_dec_nn = rand.nextDouble();
ps.setDouble(5, t_dec_nn);
// NsTest.logger.println("set t_decimal_nn " + t_dec_nn);
// Set value of column "t_double"
double t_doub = rand.nextDouble()
* Math.pow(10, Math.abs(rand.nextInt() % 300));
ps.setDouble(6, t_doub);
// NsTest.logger.println("set t_double to "+ t_doub);
// Set value of column "t_float"
float t_flt = rand.nextFloat()
* (float) Math.pow(10, Math.abs(rand.nextInt() % 30));
ps.setFloat(7, t_flt);
// NsTest.logger.println("set t_float to " + t_flt);
// Set value of column "t_int"
int t_intval = rand.nextInt();
ps.setInt(8, t_intval);
// NsTest.logger.println("set t_int to " + t_intval);
// Set value of column "t_longint"
long t_longval = rand.nextLong();
ps.setLong(9, t_longval);
// NsTest.logger.println("set t_longint " + t_longval);
// Set value of column "t_numeric_large"
double t_num_lrg = rand.nextDouble()
* Math.pow(10, Math.abs(rand.nextInt() % 20));
ps.setDouble(10, t_num_lrg);
// NsTest.logger.println("set t_numeric large to " + t_num_lrg);
// Set value of column "t_real"
float t_fltval = rand.nextFloat()
* (float) Math.pow(10, Math.abs(rand.nextInt() % 7));
ps.setFloat(11, t_fltval);
// NsTest.logger.println("set t_real to " + t_fltval);
// Set value of column "t_smallint"
int t_smlint = rand.nextInt() % (256 * 128);
ps.setInt(12, t_smlint);
// NsTest.logger.println("set t_smallint to " + t_smlint);
// Set value of column "t_time"
tt.setTime(Math.abs(rand.nextInt()));
ps.setTime(13, tt);
// NsTest.logger.println("set t_time to " + tt.toString());
// Set value of column "t_timestamp"
ts.setTime(Math.abs(rand.nextLong() / 50000));
ps.setTimestamp(14, ts);
// NsTest.logger.println("set t_timestamp to " + ts.toString());
// Set value of column "t_varchar"
ps.setString(15, cs.substring(Math.abs(rand.nextInt() % 100)));
// NsTest.logger.println("set t_varchar, now executing update stmt");
try {
rowsAdded = ps.executeUpdate();
} catch (SQLException sqe) {
if ( NsTest.justCountErrors() )
{
NsTest.addError( sqe );
}
else
{
if (sqe.getSQLState().equalsIgnoreCase("40XL1")) {
NsTest.logger
.println("LOCK TIMEOUT obtained during insert - add_one_row() "
+ sqe.getSQLState());
}
else if (sqe.getSQLState().equalsIgnoreCase("23505")) {
NsTest.logger
.println("prevented duplicate row - add_one_row(): "
+ sqe.getSQLState() + "; " + sqe.getMessage());
} else {
throw sqe;
}
}
}
if (rowsAdded == 1) {
NsTest.logger.println(thread_id + " inserted 1 row with id "
//+ id_ind + NsTest.SUCCESS);
+ id_ind);
} else
NsTest.logger.println("FAIL: " + thread_id + " inserted " + rowsAdded + "rows");
} catch (Exception e) {
NsTest.logger
.println("Exception when preparing or executing insert prepared stmt");
printException("executing/preparing insert stmt in dbUtil", e);
e.printStackTrace( NsTest.logger );
// ps.close();
}
return rowsAdded;
}
/*
* Update a random row. This method is common to all the worker threads
*/
public int update_one_row(Connection conn, String thread_id)
throws Exception {
PreparedStatement ps2 = null;
String column = null;
int ind = 0;
Random rand = new Random();
int rowsUpdated = 0;
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
long skey = pick_one(conn, thread_id);
if (skey == 0) { // means we did not find a row
NsTest.logger.println(thread_id
+ " could not find a row to update or there was an error.");
return rowsUpdated;
}
ind = Math.abs(rand.nextInt());
column = colnames[ind % NUMTYPES]; // randomly gets one of the columns
// of the table
try {
ps2 = conn.prepareStatement(" update nstesttab set " + column
+ " = ? " + " where serialkey = " + skey);
} catch (Exception e) {
printException(
"closing update prepared stmt in dbUtil.update_one_row() ",
e);
return rowsUpdated;
}
String ds2 = null;
String cs = "asdf qwerqwer 12341234 ZXCVZXCVZXCV !@#$!@#$ asdfasdf 1 q a z asdf ASDF qwerasdfzxcvasdfqwer1234asd#";
double d = 0.0;
float f = 0;
int type = (ind % NUMTYPES);
switch (type) {
case TCHAR:
ds2 = cs.substring(Math.abs(rand.nextInt() % 100));
ps2.setString(1, ds2);
break;
case TDATE:
Date dt = new Date(1);
dt.setTime(Math.abs(rand.nextLong() / 150000));
dt.setTime(Math.abs(rand.nextLong() / 150000));
ps2.setDate(1, dt);
ds2 = dt.toString();
break;
case TDECIMAL:
d = rand.nextDouble() * Math.pow(10, Math.abs(rand.nextInt() % 6));
ps2.setDouble(1, d);
ds2 = String.valueOf(d);
break;
case TDECIMALNN:
d = rand.nextDouble();
ps2.setDouble(1, d);
ds2 = String.valueOf(d);
break;
case TDOUBLE:
d = rand.nextDouble() * Math.pow(10, rand.nextInt() % 300);
ps2.setDouble(1, d);
ds2 = String.valueOf(d);
break;
case TFLOAT:
f = rand.nextFloat() * (float) Math.pow(10, rand.nextInt() % 30);
ps2.setFloat(1, f);
ds2 = String.valueOf(f);
break;
case TINT:
int i = rand.nextInt();
ds2 = String.valueOf(i);
ps2.setInt(1, i);
break;
case TLONGINT:
long l = rand.nextLong();
ds2 = String.valueOf(l);
ps2.setLong(1, l);
break;
case TNUMERICLARGE:
d = rand.nextDouble() * Math.pow(10, rand.nextInt() % 20);
ps2.setDouble(1, d);
ds2 = String.valueOf(d);
break;
case TREAL:
f = rand.nextFloat() * (float) Math.pow(10, rand.nextInt() % 7);
ps2.setFloat(1, f);
ds2 = String.valueOf(f);
break;
case TSMALLINT:
i = rand.nextInt() % (256 * 128);
short si = (short) i;
ps2.setShort(1, si);
ds2 = String.valueOf(si);
break;
case TTIME:
Time tt = new Time(1);
tt.setTime(Math.abs(rand.nextInt()));
ps2.setTime(1, tt);
ds2 = tt.toString();
break;
case TTIMESTAMP:
Timestamp ts = new Timestamp(1);
ts.setTime(Math.abs(rand.nextLong() / 50000));
ps2.setTimestamp(1, ts);
ds2 = ts.toString();
break;
case TVARCHAR:
ds2 = cs.substring(Math.abs(rand.nextInt() % 100));
ps2.setString(1, ds2);
break;
} // end of switch(type)
//NsTest.logger.println(thread_id + " attempting to update col " + column
// + " to " + ds2);
try {
rowsUpdated = ps2.executeUpdate();
} catch (SQLException sqe) {
NsTest.logger.println(sqe.getSQLState() + " " + sqe.getErrorCode()
+ " " + sqe.getMessage());
if ( NsTest.justCountErrors() ) { NsTest.printException( DbUtil.class.getName(), sqe ); }
else { sqe.printStackTrace( NsTest.logger ); }
} catch (Exception e) {
printException("Error in update_one_row()", e);
e.printStackTrace( NsTest.logger );
} finally {
conn
.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
}
if (rowsUpdated > 0)
NsTest.logger.println(thread_id + " updated " + rowsUpdated
+ " row with serialkey " + skey + NsTest.SUCCESS);
else
NsTest.logger
.println(thread_id + " update failed, no such row exists");
return rowsUpdated;
}
//
// Delete one row from the table. The row to be deleted is chosen randomly
// using the
// pick_one method which randomly returns a number between the max of
// serialkey and
// the minimum serialkey value that is untouched (nstest.NUM_UNTOUCHED_ROWS)
//
public int delete_one_row(Connection conn, String thread_id)
throws Exception {
PreparedStatement ps = null;
int rowsDeleted = 0;
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
try {
ps = conn
.prepareStatement(" delete from nstesttab where serialkey = ?");
} catch (Exception e) {
NsTest.logger
.println("Unexpected error preparing the statement in delete_one()");
printException("delete_one_row prepare ", e);
return rowsDeleted;
}
long skey = pick_one(conn, thread_id);
//NsTest.logger.println(thread_id
// + " attempting to delete a row with serialkey = " + skey);
if (skey == 0) { // means we did not find a row
NsTest.logger.println(thread_id
+ " could not find a row to delete or there was an error.");
return rowsDeleted;
}
try {
ps.setLong(1, skey);
rowsDeleted = ps.executeUpdate();
} catch (Exception e) {
NsTest.logger
.println("Error in delete_one(): either with setLong() or executeUpdate");
printException("failure to execute delete stmt", e);
} finally {
conn
.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
// set it back to read uncommitted
}
if (rowsDeleted > 0)
NsTest.logger.println(thread_id + " deleted row with serialkey "
+ skey + NsTest.SUCCESS);
else
NsTest.logger.println(thread_id + " delete for serialkey " + skey
+ " failed, no such row exists.");
return rowsDeleted;
}// end of method delete_one()
//
// get a random serialkey value that matches the criteria:
// - should not be one of the "protected" rows (set by
// nstest.NUM_UNTOUCHED_ROWS)
// - should be less than the current value of the max(serialkey)
//
public long pick_one(Connection conn, String thread_id) throws Exception {
Random rand = new Random();
long minVal = NsTest.NUM_UNTOUCHED_ROWS + 1;//the max we start with
long maxVal = NsTest.numInserts;// this is an almost accurate count of
// the max serialkey since it keeps a count of the num of inserts made
// so far
// Now choose a random value between minVal and maxVal. We use this
// value even if the row does not exist (i.e. in a situation where some
// other thread has deleted this row).
// The test should just complain and exit with a row not found exception
// now get a value between the original max, and the current max
long rowToReturn = minVal + (long)(rand.nextDouble() * (maxVal - minVal));
return rowToReturn;
}//of method pick_one(...)
// ** This method abstracts exception message printing for all exception
// messages. You may want to change
// ****it if more detailed exception messages are desired.
// ***Method is synchronized so that the output file will contain sensible
// stack traces that are not
// ****mixed but rather one exception printed at a time
public synchronized void printException(String where, Exception e) {
if ( NsTest.justCountErrors() )
{
NsTest.addError( e );
return;
}
NsTest.logger.println(e.toString());
if (e instanceof SQLException) {
SQLException se = (SQLException) e;
if (se.getSQLState().equals("40001"))
NsTest.logger.println(getThreadName()
+ " dbUtil --> deadlocked detected");
if (se.getSQLState().equals("40XL1"))
NsTest.logger.println(getThreadName()
+ " dbUtil --> lock timeout exception");
if (se.getSQLState().equals("23500"))
NsTest.logger.println(getThreadName()
+ " dbUtil --> duplicate key violation");
if (se.getNextException() != null) {
String m = se.getNextException().getSQLState();
NsTest.logger.println(se.getNextException().getMessage()
+ " SQLSTATE: " + m);
NsTest.logger.println(getThreadName()
+ " dbUtil ---> Details of exception: " + se.toString()
+ " " + se.getErrorCode());
}
}
if (e.getMessage() == null) {
NsTest.logger.println(getThreadName()
+ " dbUtil --> NULL error message detected");
NsTest.logger
.println(getThreadName()
+ " dbUtil --> Here is the NULL exection - "
+ e.toString());
NsTest.logger.println(getThreadName()
+ " dbUtil --> Stack trace of the NULL exception - ");
if ( NsTest.justCountErrors() ) { NsTest.printException( DbUtil.class.getName(), e ); }
else { e.printStackTrace( NsTest.logger ); }
}
NsTest.logger.println(getThreadName() + " dbUtil ----> During " + where
+ ", exception thrown was : " + e.toString());
}
public String getThreadName() {
return threadName;
}
}
|