File: NsSampleClientThread.java

package info (click to toggle)
derby 10.10.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80,884 kB
  • ctags: 78,150
  • sloc: java: 650,864; sql: 46,413; xml: 21,894; sh: 3,378; sed: 96; makefile: 44
file content (488 lines) | stat: -rw-r--r-- 14,179 bytes parent folder | download
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
/*

   Derby - Class nserverdemo.NsSampleClientThread

   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 nserverdemo;

import java.util.Properties;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.io.IOException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.io.PrintWriter;
import java.util.Properties;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.util.Random;
import java.lang.Math;
/**
 * NsSampleClientThread thread to perform the NsSampleWork
 */
public class NsSampleClientThread extends Thread {

	protected int thread_id;
	protected Properties properties;
	protected PrintWriter pw;
	protected String dbUrl;

  NsSampleClientThread(int id,String dbUrl, Properties properties,PrintWriter pw) {
		this.thread_id=id;
		this.dbUrl = dbUrl;
		this.properties = properties;
		this.pw = pw;
	}

   public void run() {
	 System.out.println("[NsSampleClientThread] Thread id - "+this.thread_id + "; started.");
	 NsSampleWork w = new NsSampleWork(this.thread_id,dbUrl,properties,pw);
	 w.doWork();  // do all the NsSampleWork
	 pw.println("[NsSampleClientThread] Thread id - "+this.thread_id+"; finished all tasks.");
   }
}



/**
 * NsSampleWork class represents all the work done in the sample demo program.
 * It includes
 * getting a connection to the database, creating and loading of schema,
 * preparing and execution of SQL statements (insert, select, update, delete )
 */
class NsSampleWork {

	protected int thread_id;
	protected String dbUrl;
	protected Properties properties;
	PrintWriter pw;
	PreparedStatement select = null;
	PreparedStatement insert = null;
	PreparedStatement delete = null;
	PreparedStatement update = null;
	PreparedStatement getMaxKey = null;



	public static int counter=0;
	static Integer lock = new Integer(0);
	 /**
	  * dbUrl is the database url to connect to
	  */
	 NsSampleWork(int id, String dbURL,Properties properties,PrintWriter pw) {
		this.thread_id = id;
		this.dbUrl = dbURL;
		this.pw = pw;
		this.properties = properties;
	 }


	 /**
	  *	gets a database connection
	  * If the dbUrl is trying to connect to the Derby NetNsSampleWork server using JCC
	  * then the jcc driver must be already loaded before calling this method,
	  * else there will be an error
	  * return jcc connection if no error, else null
	  */
	 public Connection getConnection(String dbUrl, Properties properties) {
		 Connection conn = null;
		 try {
			pw.println("[NsSampleWork] Thread id - "+thread_id + "; requests database connection, dbUrl ="+dbUrl);
			conn = DriverManager.getConnection(dbUrl, properties);
		  } catch (Exception e) {
			 System.out.println("[NsSampleWork] Thread id - "+ thread_id + "; failed to get database connection. Exception thrown:");
			 e.printStackTrace();
		    }
		  return conn;
	 }


	 /**
	  * set the connection to this isolation level
	  */
	 public void setIsolationLevel(Connection conn, int level) {
		 try {
			conn.setTransactionIsolation(level);
		 } catch (Exception e) {
			 pw.println("[NsSampleWork] Thread id - "+ thread_id +"; setIsolationLevel failed. Exception thrown: ");
			 e.printStackTrace();
		   }
	 }


	 /**
	  * close connection
	  */
	 public void closeConnection(Connection conn) {
		 try {
			if(conn != null)
			 	conn.close();
			pw.println("[NsSampleWork] Thread id - "+thread_id + "; closed connection to the database.");
		 } catch (Exception e) {
			 pw.println("[NsSampleWork] Thread id - "+thread_id + "; error when closing connection;"+ e);
			 e.printStackTrace();
		   }
	 }


	 /**
	  * prepare required sql statements
	  */
	 public void prepareStmts(Connection conn) {
		 try {
		 	select = conn.prepareStatement("select t_int,  t_char, t_float,t_key from SAMPLETBL where t_key = ?");
		 	insert = conn.prepareStatement("insert into SAMPLETBL (t_int, t_char,t_float,t_key) values (?,?,?,?)");
		 	update = conn.prepareStatement(" update SAMPLETBL set t_int = ? where t_key = ?");
		 	delete = conn.prepareStatement("delete from SAMPLETBL where t_key = ?");
		 	getMaxKey = conn.prepareStatement("select max(t_key) from SAMPLETBL");
	 	} catch (Exception e) {
			e.printStackTrace();
		  }
	 }


	 /**
	  * executing a select and retrieving the results
	  * select the row with t_key value as 'selectWhat'
	  */
	 public int doSelectOperation(long selectWhat) {
		 int numRowsSelected = 0;
		 ResultSet rs = null;

		 try {
			 select.setLong(1,selectWhat);
			 rs = select.executeQuery();

			 while (rs.next()) {
				 numRowsSelected++;

				 int intVal = rs.getInt(1);
				 String strVal = rs.getString(2);
				 float floatVal = rs.getFloat(3);
				 long longVal = rs.getLong(4); 	//t_key column

				 pw.println("[NsSampleWork] Thread id - "+ thread_id +" selected "+numRowsSelected +" row ["+ intVal + ","
				 			+ strVal +","+ floatVal +","+ longVal +"]");
			 }
		 } catch (Exception e) {
			e.printStackTrace();
		   } finally {
			   try {
			 	if(rs != null)
					rs.close();
			   } catch (Exception e) {
				   e.printStackTrace();
			     }
		     }
		 return numRowsSelected;
	 }


	 /**
	  *  Opens a connection and executes DML (insert, select, update, delete) operations
	  */
	 public void doWork() {

	   Connection conn = null;
	   ResultSet rs = null;
	   try {
		 conn = getConnection(dbUrl,properties);

		 if(conn == null)
		 	throw new Exception("Failed to obtain connection!");

		 conn.setAutoCommit(true);

		 // Setting isolation level to read uncommitted, since this is a sample application.
		 // Please set the isolation level depending on the requirements of your application
		 setIsolationLevel(conn,Connection.TRANSACTION_READ_UNCOMMITTED);

		 prepareStmts(conn);

		 // Perform the DML operations
		 for (int i=0; i<NsSample.ITERATIONS; i++) {
			 // Choose between either a select or any one of (insert or update or delete ) operation
			 int choice = (int) (Math.random() * 100) % 2;
			 switch (choice) {
				 case 0: { //select a row
					rs = getMaxKey.executeQuery(); //gets max t_key value
					long selectWhere = 0;
					if(rs.next()) {
						selectWhere = rs.getLong(1);
					}
					int numSelected = doSelectOperation(selectWhere);
					break;
				 }

				 case 1: { //do an insert, update or delete
					doIUDOperation();
					break;
				 }
			 } //end of switch()
		 }//enf of for()

	   } catch(Exception e) {
		   pw.println("[NsSampleWork] Thread id - "+ thread_id + "; error when performing dml operations; ");
		   e.printStackTrace();
	     } finally {
		   	try {
				if(rs != null)
					rs.close();

				closeConnection(conn);
				cleanup();
		    } catch(Exception ee) {
			   pw.println("[NsSampleWork] Thread id - " + thread_id+"; error when cleaning up connection, resultset; exception is ");
			   ee.printStackTrace();
		      }
	       }
	 }//end of method doNsSampleWork()


	/**
	 * close resources
	 */
	public void cleanup() {
	  try{
		if(select != null)
			select.close();
		if(insert != null)
			insert.close();
		if(delete != null)
			delete.close();
		if(update != null)
			update.close();
		if(getMaxKey != null)
			getMaxKey.close();
  	  } catch (Exception e) {
		  e.printStackTrace();
	    }
	}


	 /**
	  * Perform an insert or an update or delete operation
	  */
	 public void doIUDOperation() {
		 int decide = (int) (Math.random() * 100) % 3;
		 ResultSet rs = null;

		 try {
			switch (decide) {
				 case 0: { //insert
						int numInsert = insertRow(insert);
						pw.println("[NsSampleWork] Thread id - "+thread_id+"; inserted "+numInsert+" row.");
						break;
				 }

				 case 1: { //update
						rs = getMaxKey.executeQuery();
						long updateRow=0;
						if(rs.next())
							updateRow = rs.getLong(1);
						int numUpdate = updateRow(update,updateRow);
						System.out.println("[NsSampleWork] Thread id - "+thread_id+"; updated "+numUpdate+" row with t_key = " + updateRow);
						break;
				 }

				 case 2: { //delete
						rs = getMaxKey.executeQuery();
						long deleteRow =0;
						if(rs.next())
							deleteRow = rs.getLong(1);
						int numDelete = deleteRow(delete,deleteRow);
						System.out.println("[NsSampleWork] Thread id - "+thread_id+"; deleted "+numDelete+" row with t_key = " + deleteRow);
						break;
				 }
		 	}//end of switch()
	 	} catch (Exception e) {
			e.printStackTrace();
		  } finally {
			  try {
			  	if(rs != null)
				  rs.close();
			  } catch (Exception e) {
				  e.printStackTrace();
			    }
		    }
	 }//end of method doIUDOperation()


	 /**
	  * Create necessary schema if schema not already created
	  */
	 public static void checkAndCreateSchema(Connection conn,PrintWriter pw) {
		Statement stmt = null;
		ResultSet rs = null;

		try	{
			conn.setAutoCommit(true);
		} catch (SQLException  se) {
			pw.println("[NsSampleWork] Error when setting autocommit on connection; exception thrown: ");
			se.printStackTrace();
		  }

		// Check for existence of schema by quering the catalog systables
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select tablename from sys.systables " +
								" where tablename = 'SAMPLETBL'");
			if (rs.next()) {
				pw.println("[NsSampleWork] Table 'SAMPLETBL' already exists; no need to create schema again.");
				return;
			}
		} catch (SQLException  se) {
			pw.println("[NsSampleWork] Unable to query the metadata for existence of table SAMPLETBL; exception is "+se);
			pw.println("[NsSampleWork] Exiting the application.");
			se.printStackTrace();
			System.exit(1);
		  }

		// Create the necessary table and indexes
		try {
			pw.println("[NsSampleWork] Begin creating table - SAMPLETBL and necessary indexes. ");
			stmt.execute("create table SAMPLETBL (" +
						  "t_int int," +
						  "t_char char(15),"+
						  "t_float float," +
						  "t_key bigint )");
			stmt.execute("create index t_char_idx on SAMPLETBL ( t_char)");
			stmt.execute("create index t_float_idx on SAMPLETBL ( t_float)");
			stmt.execute("create index t_key_idx on SAMPLETBL ( t_key )" );
		} catch (Exception  e) {
			pw.println("[NsSampleWork] Error when creating schema; exception is " + e.toString());
			pw.println("[NsSampleWork] Exiting the application.");
			e.printStackTrace();
			System.exit(1);
		  } finally {
			  try {
			  	if(rs != null)
				  rs.close();
			  	if(stmt != null)
				  stmt.close();
			  } catch (Exception e) {
				  e.printStackTrace();
			    }
			}
	 }//end of method checkAndCreateSchema()


	 /**
	  * Loads schema , inserts 'rowsToInsert' number of rows into the table
	  */
	 public static void loadSchema(Connection conn,int rowsToInsert,PrintWriter pw)	 {
		 int insertsRemaining = rowsToInsert;
		 PreparedStatement ps=null;

		 try {
			ps = conn.prepareStatement("insert into SAMPLETBL (t_int, t_char,t_float,t_key) values (?,?,?,?)");
			// Insert one row at a time
			while (insertsRemaining-- >= 0)	{
				 int numInserts = insertRow(ps);
				 if (numInserts != 1)
					pw.println("[NsSampleWork] Failed to insert row.");
			 }
		 } catch (Exception e) {
			 pw.println("[NsSampleWork] Error when loading schema; exception is "+ e);
			 e.printStackTrace();
		   } finally {
			  try {
			   	if(ps != null)
				  ps.close();
			  } catch (Exception e) {
				  e.printStackTrace();
			    }
		     }
	 }//end of method loadSchema()



	/**
	 *	Generates random values and performs the inserts into the database
	 */
	public static int insertRow(PreparedStatement ps) {

		int rowsAdded = 0;
		try	{
			// Generate random values for the datatypes in the sample table
			Random rand = new Random();
			int intVal = Math.abs(rand.nextInt()%1000);

			String charVal = "Derby";

			synchronized(lock) {
				charVal += counter;
				counter++;
			}

			// Set parameter values
			ps.setInt(1, intVal);
			ps.setString(2,charVal);
			ps.setFloat(3, rand.nextFloat()*(float)Math.pow(10,Math.abs(rand.nextInt()%30)));
			ps.setLong(4,rand.nextLong()%10000);
			rowsAdded = ps.executeUpdate();
			return rowsAdded;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		  }
	}


	/**
	 * update a row in the table
	 * updateWhere	is the value of the t_key row which needs to be updated
	 * return  number of rows updated
	 */
	public static int updateRow (PreparedStatement ps,long updateWhere) {
		try	{
		  int val=0;
		  synchronized(lock) {
			val = counter++;
		  }
		  ps.setInt(1,val);
		  ps.setLong(2,updateWhere);
		  return(ps.executeUpdate());
		} catch (SQLException se) {
			se.printStackTrace();
			return 0;
		  }
	}


	/**
	 * Delete row from table
	 * deleteRow is the value of the t_key of the row to be deleted
	 * return number of rows deleted
	 */
	public static int deleteRow(PreparedStatement ps,long deleteRow) {
		int rowsDeleted = 0;
		try	{
			ps.setLong(1, deleteRow);
			rowsDeleted = ps.executeUpdate();
			return rowsDeleted;
		} catch(Exception e) {
			e.printStackTrace();
			return 0;
		  }
	}

}//end of class NsSampleWork