1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.performance.dbcp;
19
20 import java.sql.Connection;
21 import java.sql.ResultSet;
22 import java.sql.Statement;
23 import java.util.logging.Logger;
24 import javax.sql.DataSource;
25
26 import org.apache.commons.performance.ClientThread;
27 import org.apache.commons.performance.Statistics;
28
29
30
31
32
33
34
35
36
37 public class DBCPClientThread extends ClientThread {
38
39 private String queryString = null;
40
41 private boolean textQuery = false;
42
43 private DataSource dataSource = null;
44
45 Connection conn = null;
46
47 String currentQuery = null;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public DBCPClientThread(long iterations, long minDelay, long maxDelay,
68 double sigma, String delayType, String queryType, long rampPeriod,
69 long peakPeriod, long troughPeriod, String cycleType,
70 String rampType, Logger logger, DataSource dataSource,
71 Statistics stats) {
72
73 super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
74 peakPeriod, troughPeriod, cycleType, rampType, logger,
75 stats);
76
77 this.dataSource = dataSource;
78
79 if (queryType.equals("no-op")) {
80 return;
81 }
82
83 if (queryType.equals("integerIndexed")) {
84 queryString = "select * from test_table WHERE indexed=";
85 } else if (queryType.equals("integerScan")) {
86 queryString = "select * from test_table WHERE not_indexed=";
87 } else {
88 queryString = "select * from test_table WHERE text='";
89 textQuery = true;
90 }
91 }
92
93
94 public void setUp() throws Exception {
95 if (queryString == null) {
96 return;
97 }
98 if (textQuery) {
99 currentQuery = queryString +
100 randomData.nextHexString(20) + "';";
101 } else {
102 currentQuery = queryString +
103 randomData.nextInt(0, 100) + ";";
104 }
105 }
106
107
108 public void execute() throws Exception {
109 conn = dataSource.getConnection();
110 if (queryString == null) {
111 return;
112 }
113 Statement stmt = conn.createStatement();
114 stmt.execute(currentQuery);
115 ResultSet rs = stmt.getResultSet();
116 if (!rs.isAfterLast()) {
117 rs.next();
118 }
119 rs.close();
120 stmt.close();
121 }
122
123
124 public void cleanUp() throws Exception {
125 if (conn != null) {
126 try {
127 conn.close();
128 } finally {
129 conn = null;
130 }
131 }
132 }
133
134 }