001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.performance.dbcp;
019
020 import java.sql.Connection;
021 import java.sql.ResultSet;
022 import java.sql.Statement;
023 import java.util.logging.Logger;
024 import javax.sql.DataSource;
025
026 import org.apache.commons.performance.ClientThread;
027 import org.apache.commons.performance.Statistics;
028
029 /**
030 * Client thread that executes requests in a loop using a configured
031 * DataSource, with the number of requests, time between requests and
032 * query strings governed by constructor parameters. See
033 * {@link ClientThread ClientThread javadoc} for a description
034 * of how times between requests are computed.
035 *
036 */
037 public class DBCPClientThread extends ClientThread {
038 /** Initial segment of query string */
039 private String queryString;
040 /** Whether or not the query is on the text column */
041 private boolean textQuery = false;
042 /** DataSource used to connect */
043 private DataSource dataSource = null;
044 /** Database connection */
045 Connection conn = null;
046 /** Current query */
047 String currentQuery = null;
048
049 /**
050 * Create a dbcp client thread.
051 *
052 * @param iterations number of iterations
053 * @param minDelay minimum delay time between client requests
054 * @param maxDelay maximum delay time between client requests
055 * @param sigma standard deviation of delay times between client requests
056 * @param delayType distribution of time between client requests
057 * @param queryType type of query
058 * @param rampPeriod rampup, rampdown period for cyclic load
059 * @param peakPeriod period of sustained peak for cyclic load
060 * @param troughPeriod period of sustained minimum load
061 * @param cycleType type of cycle for mean delay
062 * @param rampType type of ramp (linear or random jumps)
063 * @param logger common logger shared by all clients
064 * @param dataSource DataSource for connections
065 * @param stats Statistics container
066 */
067 public DBCPClientThread(long iterations, long minDelay, long maxDelay,
068 double sigma, String delayType, String queryType, long rampPeriod,
069 long peakPeriod, long troughPeriod, String cycleType,
070 String rampType, Logger logger, DataSource dataSource,
071 Statistics stats) {
072
073 super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
074 peakPeriod, troughPeriod, cycleType, rampType, logger,
075 stats);
076
077 this.dataSource = dataSource;
078
079 if (queryType.equals("integerIndexed")) {
080 queryString = "select * from test_table WHERE indexed=";
081 } else if (queryType.equals("integerScan")) {
082 queryString = "select * from test_table WHERE not_indexed=";
083 } else {
084 queryString = "select * from test_table WHERE text='";
085 textQuery = true;
086 }
087 }
088
089 /** Generate a random query */
090 public void setUp() throws Exception {
091 if (textQuery) {
092 currentQuery = queryString +
093 randomData.nextHexString(20) + "';";
094 } else {
095 currentQuery = queryString +
096 randomData.nextInt(0, 100) + ";";
097 }
098 }
099
100 /** Execute query */
101 public void execute() throws Exception {
102 conn = dataSource.getConnection();
103 Statement stmt = conn.createStatement();
104 stmt.execute(currentQuery);
105 ResultSet rs = stmt.getResultSet();
106 if (!rs.isAfterLast()) {
107 rs.next();
108 }
109 rs.close();
110 stmt.close();
111 }
112
113 /** Close connection */
114 public void cleanUp() throws Exception {
115 if (conn != null) {
116 try {
117 conn.close();
118 } finally {
119 conn = null;
120 }
121 }
122 }
123
124 }