db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fuzzylo...@apache.org
Subject svn commit: r514882 [2/2] - in /db/derby/code/trunk/java/testing: ./ org/apache/derbyTesting/system/nstest/ org/apache/derbyTesting/system/nstest/init/ org/apache/derbyTesting/system/nstest/tester/ org/apache/derbyTesting/system/nstest/utils/
Date Mon, 05 Mar 2007 22:01:25 GMT
Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/tester/TesterObject.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/tester/TesterObject.java?view=auto&rev=514882
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/tester/TesterObject.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/tester/TesterObject.java
Mon Mar  5 14:01:23 2007
@@ -0,0 +1,380 @@
+/*
+
+ Derby - Class org.apache.derbyTesting.system.nstest.tester.TesterObject
+
+ 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.tester;
+
+import java.sql.SQLException;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.sql.ResultSet;
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.sql.Connection;
+
+import org.apache.derbyTesting.system.nstest.NsTest;
+import org.apache.derbyTesting.system.nstest.utils.DbUtil;
+
+/**
+ * TesterObject - The base tester class for all the testers
+ */
+public class TesterObject {
+
+	private String thread_id;
+
+	protected Connection connex = null;
+
+	protected DbUtil dbutil;
+
+	// *******************************************************************************
+	//
+	// Constructor. Get's the name of the thread running this for use in
+	// messages
+	//
+	// *******************************************************************************
+	public TesterObject(String name) {
+		this.thread_id = name;
+		dbutil = new DbUtil(getThread_id());
+		System.out.println("==========> " + getThread_id()
+				+ " THREAD starting <======");
+	}
+
+	// *******************************************************************************
+	//
+	// Gets the connection to the database. Implemented so that threads that
+	// need to
+	// frequently get a connection can just call this method instead.
+	//
+	// *******************************************************************************
+
+	public Connection getConnection() {
+		Connection conn = null;
+		String jdbcurl = "";
+		try {
+			System.out.println(getThread_id()
+					+ " is getting a connection to the database...");
+
+			if (NsTest.embeddedMode) {
+				jdbcurl = NsTest.embedDbURL + ";" + NsTest.bootPwd;
+			} else {
+				if (NsTest.driver_type.equalsIgnoreCase("DerbyClient"))
+					jdbcurl = NsTest.clientDbURL + ";" + NsTest.bootPwd;
+
+			}
+			System.out.println("-->Thread " + getThread_id()
+					+ " starting with url " + jdbcurl + " <--");
+			conn = DriverManager.getConnection(jdbcurl, NsTest.prop);
+		} catch (Exception e) {
+			e.printStackTrace();
+			System.out.println("FAIL: " + getThread_id()
+					+ " could not get the database connection");
+			printException("Failed getting database connection using "
+					+ jdbcurl, e);
+		}
+		// for statistical purposes, add one to the num of connections makde
+		NsTest.addStats(NsTest.CONNECTIONS_MADE, 1);
+		System.out.println("Connection number: " + NsTest.numConnections);
+		return conn; // null if there was a problem, else a valid connection
+	}
+
+	// *******************************************************************************
+	//
+	// Sets the isolation level to that indicated.
+	//
+	// *******************************************************************************
+	public void setIsolationLevel(int level) {
+		try {
+			connex.setTransactionIsolation(level);
+		} catch (Exception e) {
+			System.out.println("FAIL: " + getThread_id()
+					+ " could not set isolation level");
+			printException("setting transaction isolation", e);
+		}
+	}
+
+	// *******************************************************************************
+	//
+	// Closes the connection to the database. Implemented so that threads that
+	// need to
+	// frequently close their connection can just call this method instead.
+	//
+	// *******************************************************************************
+	public void closeConnection() {
+		try {
+			System.out.println(getThread_id()
+					+ " is closing it's connection to the database...");
+			connex.close();
+		} catch (Exception e) {
+			System.out.println("FAIL: " + getThread_id()
+					+ " could not close the database connection");
+			printException("closing database connection", e);
+		}
+	}
+
+	// ******************************************************************************************
+	//
+	// This method will do a basic Insert/Delete/Update operation. We randomly
+	// decide whether
+	// we want to do either an Insert, a delete or an update
+	//
+	//
+	// *******************************************************************************************
+	public void doIUDOperation() {
+		// decide Insert, Update or Delete
+		int decider = (int) (Math.random() * 100) % 3;
+
+		switch (decider) {
+
+		case 0: // do an Insert
+			try {
+				int numInsert = dbutil.add_one_row(connex, getThread_id());
+				if (numInsert == 1)
+					NsTest.addStats(NsTest.INSERT, 1);
+				else
+					NsTest.addStats(NsTest.FAILED_INSERT, 1);
+			} catch (Exception e) {
+				printException("executing add_one_row()", e);
+			}
+
+			break;
+
+		case 1: // do an update
+
+			try {
+				int numUpdate = dbutil.update_one_row(connex, getThread_id());
+				if (numUpdate == 1)
+					NsTest.addStats(NsTest.UPDATE, 1);
+				else
+					NsTest.addStats(NsTest.FAILED_UPDATE, 1);
+			} catch (Exception e) {
+				printException("executing update_one_row", e);
+			}
+			break;
+
+		case 2: // do a delete
+
+			try {
+				int numDelete = dbutil.delete_one_row(connex, getThread_id());
+				if (numDelete == 1)
+					NsTest.addStats(NsTest.DELETE, 1);
+				else
+					NsTest.addStats(NsTest.FAILED_DELETE, 1);
+			} catch (Exception e) {
+				printException("executing delete_one_row()", e);
+			}
+			break;
+
+		}// end of switch(decider)
+	}// end of method doIUDOperation()
+
+	// ******************************************************************************************
+	//
+	// This method will do a basic Select operation based on the following
+	// criteria
+	// The query should return approximately nstest.MAX_LOW_STRESS number of
+	// rows that we
+	// loop through via a result set and perform operations (getXX calls) in
+	// order to ensure
+	// that data flows properly. The method will
+	// return the total number of rows selected. Note that we do not touch rows
+	// with serialkey
+	// less than nstest.NUM_UNTOUCHED_ROWS, and the selects will be based on the
+	// parameter passed
+	// in, viz numRowsToSelect which is <= nstest.NUM_UNTOUCHED_ROWS
+	//
+	// *******************************************************************************************
+	public int doSelectOperation(int numRowsToSelect) throws SQLException {
+
+		int numRowsSelected = 0;
+		ResultSet rSet = null;
+		Statement s = null;
+
+		System.out.println(getThread_id() + " is selecting " + numRowsToSelect
+				+ " rows");
+		try {
+			// create the statement
+
+			s = connex.createStatement();
+			// Execute the query
+			rSet = s
+			.executeQuery("select 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, serialkey from nstesttab where serialkey <= "
+					+ numRowsToSelect);
+		} catch (Exception e) {
+			System.out
+			.println("FAIL: doSelectOperation() had problems creating/executing query");
+			printException(
+					"FAIL: doSelectOperation() had problems creating/executing query",
+					e);
+			s.close();
+		}
+
+		// Now work over the returned ResultSet and keep track of number of rows
+		// returned
+		// We execute the getXXX methods on each of the selected columns so that
+		// data flow out
+		// from the network server is also tested.
+		try {
+			while (rSet.next()) {
+				// get value of column id
+				int id1 = rSet.getInt(1);
+
+				// get value of column t_char
+				String str1 = rSet.getString(2);
+
+				// get value of column t_date
+				Date dt = rSet.getDate(3);
+
+				// get value of column t_decimal
+				double doub1 = rSet.getDouble(4);
+
+				// get value of column t_decimal_nn
+				double doub2 = rSet.getDouble(5);
+
+				// get value of column t_double
+				double doub3 = rSet.getDouble(6);
+
+				// get value of column t_float
+				float flt1 = rSet.getFloat(7);
+
+				// get value of column t_int
+				int id2 = rSet.getInt(8);
+
+				// get value of column t_longint
+				long lg1 = rSet.getLong(9);
+
+				// get value of column t_numeric_large
+				double doub4 = rSet.getDouble(10);
+
+				// get value of column t_real
+				float flt2 = rSet.getFloat(11);
+
+				// get value of column t_smallint
+				int id3 = rSet.getInt(12);
+
+				// get value of column t_time
+				Time tm = rSet.getTime(13);
+
+				// get value of column t_timestamp
+				Timestamp tstmp = rSet.getTimestamp(14);
+
+				// get value of column t_varchar
+				String str2 = rSet.getString(15);
+
+				// get value of column serialkey
+				long lg2 = rSet.getLong(16);
+
+				numRowsSelected++;
+			}
+			NsTest.addStats(NsTest.SELECT, 1);
+			System.out.println(this.thread_id + " selected " + numRowsSelected
+					+ " rows");
+		} catch (Exception e) {
+			System.out
+			.println("FAIL: doSelectOperation() had problems working over the ResultSet");
+			NsTest.addStats(NsTest.FAILED_SELECT, 1);
+			printException("processing ResultSet during row data retrieval", e);
+			rSet.close();
+			s.close();
+			System.out.println("Closed the select statement");
+		}
+
+		// close the ResultSet and statement and release it's resources.
+		try {
+			if ((rSet != null) && (s != null)) {
+				rSet.close();
+				s.close();
+				System.out.println("Closed the select statement");
+			}
+		} catch (Exception e) {
+			System.out
+			.println("FAIL: doSelectOperation() had problems closing the ResultSet");
+			printException("closing ResultSet of query to get row data", e);
+		}
+
+		return numRowsSelected;
+	}// end of doSelectOperation()
+
+	// *******************************************************************************
+	//
+	// This starts the acutal test operations
+	//
+	// *******************************************************************************
+	public void startTesting() {
+
+		// This method needs to be overridden by the child classes in order for
+		// a Tester to
+		// be able to do work. The specifics such as how often the connection is
+		// opened and
+		// closed and how many transactions are done etc etc which form
+		// individual test cases or
+		// sorts are left to the child class to implement in their overridden
+		// version of this
+		// method
+
+	}// end of startTesting()
+
+	// ** 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 (e instanceof SQLException) {
+			SQLException se = (SQLException) e;
+
+			if (se.getSQLState().equals("40001"))
+				System.out.println("TObj --> deadlocked detected");
+			if (se.getSQLState().equals("40XL1"))
+				System.out.println("TObj --> lock timeout exception");
+			if (se.getSQLState().equals("23500"))
+				System.out.println("TObj --> duplicate key violation");
+			if (se.getNextException() != null) {
+				String m = se.getNextException().getSQLState();
+				System.out.println(se.getNextException().getMessage()
+						+ " SQLSTATE: " + m);
+			}
+		}
+		if (e.getMessage() == null) {
+			System.out.println("TObj -->NULL error message detected");
+			System.out.println("TObj -->Here is the NULL exception - "
+					+ e.toString());
+			System.out.println("TObj -->Stack trace of the NULL exception - ");
+			e.printStackTrace(System.out);
+		}
+		System.out.println("TObj -->At this point - " + where
+				+ ", exception thrown was : " + e.getMessage());
+	}
+
+	public String getTimestamp() {
+		Timestamp ts = new Timestamp(System.currentTimeMillis());
+		return ts.toString();
+	}
+
+	public String getThread_id() {
+		return thread_id;
+	}
+
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/tester/TesterObject.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/DbUtil.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/DbUtil.java?view=auto&rev=514882
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/DbUtil.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/DbUtil.java
Mon Mar  5 14:01:23 2007
@@ -0,0 +1,610 @@
+/*
+ 
+ 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.ResultSet;
+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) values ("
+					+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,cast('00000000000000000000000000000000031'
as clob(1K)),cast(X'000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031'
as blob(10K)))");
+			
+			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);
+			// System.out.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));
+			// System.out.println("set t_Char to " + cs2.substring(0,i2));
+			
+			// System.out.println("now setting date");
+			// Set value of column "t_date"
+			dt.setTime(Math.abs(rand.nextLong() / 150000));
+			ps.setDate(3, dt);
+			// System.out.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()%18));
+			// double t_dec = rand.nextDouble();
+			double t_dec = rand.nextDouble()
+			* Math.pow(10, Math.abs(rand.nextInt() % 6));
+			ps.setDouble(4, t_dec);
+			// System.out.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);
+			// System.out.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);
+			// System.out.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);
+			// System.out.println("set t_float to " + t_flt);
+			
+			// Set value of column "t_int"
+			int t_intval = rand.nextInt();
+			ps.setInt(8, t_intval);
+			// System.out.println("set t_int to " + t_intval);
+			
+			// Set value of column "t_longint"
+			long t_longval = rand.nextLong();
+			ps.setLong(9, t_longval);
+			// System.out.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);
+			// System.out.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);
+			// System.out.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);
+			// System.out.println("set t_smallint to " + t_smlint);
+			
+			// Set value of column "t_time"
+			tt.setTime(Math.abs(rand.nextInt()));
+			ps.setTime(13, tt);
+			// System.out.println("set t_time to " + tt.toString());
+			
+			// Set value of column "t_timestamp"
+			ts.setTime(Math.abs(rand.nextLong() / 50000));
+			ps.setTimestamp(14, ts);
+			// System.out.println("set t_timestamp to " + ts.toString());
+			
+			// Set value of column "t_varchar"
+			ps.setString(15, cs.substring(Math.abs(rand.nextInt() % 100)));
+			// System.out.println("set t_varchar, now executing update stmt");
+			try {
+				rowsAdded = ps.executeUpdate();
+			} catch (SQLException sqe) {
+				if (sqe.getSQLState().equalsIgnoreCase("40XL1")) {
+					System.out
+					.println("LOCK TIMEOUT obatained during insert - add_one_row() "
+							+ sqe.getSQLState());
+					
+				} else {
+					throw sqe;
+				}
+				
+			}
+			if (rowsAdded == 1) {
+				System.out.println(thread_id + " inserted 1 row with id "
+						+ id_ind + NsTest.SUCCESS);
+				
+			} else
+				System.out.println("FAIL: " + thread_id + " insert failed");
+			
+		} catch (Exception e) {
+			e.printStackTrace();
+			System.out
+			.println("Exception when preparing or executing insert prepared stmt");
+			printException("executing/preparing insert stmt in dbUtil", e);
+			e.printStackTrace();
+			// ps.close();
+		}
+		
+		if (ps != null) {
+			try {
+				ps.close();
+				
+			} catch (Exception e) {
+				printException(
+						"closing insert stmt in dbUtil when there was a problem creating it",
+						e);
+			}
+		}
+		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
+			System.out.println(thread_id
+					+ " could not find a row to update or there was an error.");
+			if (ps2 != null)
+				ps2.close();
+			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) {
+			ps2.close();
+			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, rand.nextInt() % 18);
+			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)
+		
+		System.out.println(thread_id + " attempting  to update col " + column
+				+ " to " + ds2);
+		try {
+			rowsUpdated = ps2.executeUpdate();
+		} catch (SQLException sqe) {
+			System.out.println(sqe.getSQLState() + " " + sqe.getErrorCode()
+					+ " " + sqe.getMessage());
+			sqe.printStackTrace();
+		} catch (Exception e) {
+			ps2.close();
+			printException("Error in update_one_row()", e);
+			e.printStackTrace();
+		} finally {
+			conn
+			.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
+		}
+		
+		if (rowsUpdated > 0)
+			System.out.println(thread_id + " updated " + rowsUpdated
+					+ " row with serialkey " + skey + NsTest.SUCCESS);
+		else
+			System.out
+			.println(thread_id + " update failed, no such row exists");
+		
+		if (ps2 != null) {
+			try {
+				ps2.close();
+				
+			} catch (Exception e) {
+				printException("closing update stmt after work is done", e);
+			}
+		}
+		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) {
+			System.out
+			.println("Unexpected error preparing the statement in delete_one()");
+			printException("delete_one_row prepare ", e);
+			if (ps != null)
+				ps.close();
+			return rowsDeleted;
+		}
+		
+		long skey = pick_one(conn, thread_id);
+		System.out.println(thread_id
+				+ " attempting  to delete a row with serialkey = " + skey);
+		if (skey == 0) { // means we did not find a row
+			System.out.println(thread_id
+					+ " could not find a row to delete or there was an error.");
+			ps.close();
+			return rowsDeleted;
+		}
+		
+		try {
+			ps.setLong(1, skey);
+			rowsDeleted = ps.executeUpdate();
+		} catch (Exception e) {
+			System.out
+			.println("Error in delete_one(): either with setLong() or executeUpdate");
+			ps.close();
+			printException("failure to execute delete stmt", e);
+		} finally {
+			conn
+			.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
+			// set it back to read uncommitted
+		}
+		
+		if (rowsDeleted > 0)
+			System.out.println(thread_id + " deleted row with serialkey "
+					+ skey + NsTest.SUCCESS);
+		else
+			System.out.println(thread_id + " delete for serialkey " + skey
+					+ " failed, no such row exists.");
+		
+		if (ps != null) {
+			try {
+				ps.close();
+			} catch (Exception e) {
+				System.out
+				.println("Error in closing prepared statement of delete_one()");
+				printException("failure to close delete stmt after work done",
+						e);
+			}
+		}
+		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 {
+		
+		PreparedStatement ps = null;
+		// ResultSet rs = null;
+		
+		Random rand = new Random();
+		
+		try {
+			
+			ps = conn
+			.prepareStatement("select max(serialkey) from nstesttab where serialkey > ?");
+		} catch (Exception e) {
+			if (ps != null)
+				ps.close();
+			System.out
+			.println("Unexpected error creating the select prepared statement in pick_one()");
+			printException("failure to prepare select stmt in pick_one()", e);
+			return (0);
+		}
+		
+		long minVal = NsTest.NUM_UNTOUCHED_ROWS + 1;
+		// long maxVal = nstest.MAX_INITIAL_ROWS * nstest.INIT_THREADS; //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
+		long rowToReturn = (minVal + 1)
+		+ (Math.abs(rand.nextLong()) % (maxVal - minVal));
+		try {
+			ps.setLong(1, rowToReturn);
+			ResultSet rs = ps.executeQuery();
+			while (rs.next()) {
+				if (rs.getLong(1) > 0) {
+					rowToReturn = rs.getLong(1);
+					System.out
+					.println(getThreadName()
+							+ " dbutil.pick_one() -> Obtained row from the table "
+							+ rowToReturn);
+				} else {
+					System.out
+					.println(getThreadName()
+							+ " dbutil.pick_one() -> Returning random serialkey of "
+							+ rowToReturn);
+				}
+			}
+		} catch (SQLException sqe) {
+			System.out.println(sqe + " while selecting a random row");
+			sqe.printStackTrace();
+		}
+		
+		if (ps != null) {
+			try {
+				ps.close();
+				
+			} catch (Exception e) {
+				System.out
+				.println("Error in closing prepared statement of pick_one()");
+				printException(
+						"failure closing select stmt in pick_one after work is done",
+						e);
+			}
+		}
+		
+		return rowToReturn;
+		
+	}// end 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) {
+		System.out.println(e.toString());
+		if (e instanceof SQLException) {
+			SQLException se = (SQLException) e;
+			
+			if (se.getSQLState().equals("40001"))
+				System.out.println(getThreadName()
+						+ " dbUtil --> deadlocked detected");
+			if (se.getSQLState().equals("40XL1"))
+				System.out.println(getThreadName()
+						+ " dbUtil --> lock timeout exception");
+			if (se.getSQLState().equals("23500"))
+				System.out.println(getThreadName()
+						+ " dbUtil --> duplicate key violation");
+			if (se.getNextException() != null) {
+				String m = se.getNextException().getSQLState();
+				System.out.println(se.getNextException().getMessage()
+						+ " SQLSTATE: " + m);
+				System.out.println(getThreadName()
+						+ " dbUtil ---> Details of exception: " + se.toString()
+						+ " " + se.getErrorCode());
+			}
+		}
+		if (e.getMessage().equals(null)) {
+			System.out.println(getThreadName()
+					+ " dbUtil --> NULL error message detected");
+			System.out
+			.println(getThreadName()
+					+ " dbUtil --> Here is the NULL exection - "
+					+ e.toString());
+			System.out.println(getThreadName()
+					+ " dbUtil --> Stack trace of the NULL exception - ");
+			e.printStackTrace(System.out);
+		}
+		System.out.println(getThreadName() + " dbUtil ----> During " + where
+				+ ", exception thrown was : " + e.toString());
+	}
+	
+	public String getThreadName() {
+		return threadName;
+	}
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/DbUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/MemCheck.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/MemCheck.java?view=auto&rev=514882
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/MemCheck.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/MemCheck.java
Mon Mar  5 14:01:23 2007
@@ -0,0 +1,75 @@
+/*
+ 
+ Derby - Class org.apache.derbyTesting.system.nstest.utils.MemCheck
+ 
+ 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.util.Date;
+
+/**
+ * MemCheck - a background thread that prints current memory usage
+ */
+public class MemCheck extends Thread {
+
+	int delay = 200000;
+
+	public boolean stopNow = false;
+
+	public MemCheck() {
+	}
+
+	public MemCheck(int num) {
+		delay = num;
+	}
+
+	/*
+	 * Implementation of run() method to check memory
+	 * 
+	 */
+	public void run() {
+		while (stopNow == false) {
+			try {
+				showmem();
+				sleep(delay);
+			} catch (java.lang.InterruptedException ie) {
+				System.out.println("memcheck: unexpected error in sleep");
+			}
+		}
+	}
+
+	/*
+	 * Print the current memory status
+	 */
+	public static void showmem() {
+		Runtime rt = null;
+		Date d = null;
+		rt = Runtime.getRuntime();
+		d = new Date();
+		System.out.println("total memory: " + rt.totalMemory() + " free: "
+				+ rt.freeMemory() + " " + d.toString());
+
+	}
+
+	public static void main(String argv[]) {
+		System.out.println("memCheck starting");
+		MemCheck mc = new MemCheck();
+		mc.run();
+	}
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/system/nstest/utils/MemCheck.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message