1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class to test how many sql command one connection can execute
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Jing Tao
|
7
|
*
|
8
|
* '$Author: leinfelder $'
|
9
|
* '$Date: 2011-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
|
10
|
* '$Revision: 6595 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
|
28
|
|
29
|
import java.io.*;
|
30
|
import java.util.Vector;
|
31
|
import java.lang.*;
|
32
|
import java.sql.*;
|
33
|
import edu.ucsb.nceas.metacat.*;
|
34
|
import edu.ucsb.nceas.metacat.database.*;
|
35
|
|
36
|
|
37
|
/**
|
38
|
* A class to test when mamimum dbconnection number reached
|
39
|
*/
|
40
|
|
41
|
public class MaxmumDBConnection implements Runnable
|
42
|
{
|
43
|
|
44
|
/**
|
45
|
* Usage: java -cp metacat.jar < -t times>
|
46
|
*
|
47
|
*/
|
48
|
public static void main(String [] args) throws Exception
|
49
|
{
|
50
|
// defaul loop value is 10
|
51
|
int loop = 10;
|
52
|
Thread runner = null;
|
53
|
|
54
|
DBConnectionPool pool = DBConnectionPool.getInstance();
|
55
|
|
56
|
|
57
|
// get loop number from argument
|
58
|
for ( int i=0 ; i < args.length; ++i )
|
59
|
{
|
60
|
|
61
|
if ( args[i].equals( "-t" ) )
|
62
|
{
|
63
|
loop = Integer.parseInt(args[++i]);
|
64
|
}//if
|
65
|
else
|
66
|
{
|
67
|
System.err.println(" args[" +i+ "] '" +args[i]+ "' ignored." );
|
68
|
}//else
|
69
|
}//for
|
70
|
|
71
|
System.out.println("loop: "+loop);
|
72
|
|
73
|
//ran the number of thread.
|
74
|
for (int i= 0; i<loop; i++)
|
75
|
{
|
76
|
runner = new Thread( new MaxmumDBConnection());
|
77
|
runner.start();
|
78
|
|
79
|
}//for
|
80
|
System.out.println("End");
|
81
|
|
82
|
|
83
|
|
84
|
}//main
|
85
|
|
86
|
|
87
|
|
88
|
|
89
|
/**
|
90
|
* Method to run a sal insert command
|
91
|
* @param conn, the connection will be used
|
92
|
* @param i, part of docid
|
93
|
*/
|
94
|
private static void insert(long i) throws SQLException
|
95
|
{
|
96
|
|
97
|
int serialNumber = 0;
|
98
|
DBConnection conn = null;
|
99
|
PreparedStatement pStmt = null;
|
100
|
String sql = "insert into xml_documents (docid) values (?)";
|
101
|
String docid = "jing."+i;
|
102
|
|
103
|
try
|
104
|
{
|
105
|
conn = DBConnectionPool.getDBConnection("insert");
|
106
|
|
107
|
serialNumber = conn.getCheckOutSerialNumber();
|
108
|
System.out.println("serialNumber: "+serialNumber);
|
109
|
pStmt = conn.prepareStatement(sql);
|
110
|
pStmt.setString(1, docid);
|
111
|
pStmt.execute();
|
112
|
System.out.println("Inserted successfully: "+i);
|
113
|
}
|
114
|
finally
|
115
|
{
|
116
|
try
|
117
|
{
|
118
|
pStmt.close();
|
119
|
}
|
120
|
finally
|
121
|
{
|
122
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
123
|
System.out.println("return inert conn!");
|
124
|
System.out.println("the return insert connection's status: "
|
125
|
+conn.getStatus());
|
126
|
}
|
127
|
}
|
128
|
|
129
|
}//insert
|
130
|
|
131
|
|
132
|
/**
|
133
|
* Method to run a sql select commnad
|
134
|
* @param conn, connection will be used
|
135
|
*/
|
136
|
private static void select() throws SQLException
|
137
|
{
|
138
|
|
139
|
int serialNumber = 0;
|
140
|
DBConnection conn = null;
|
141
|
PreparedStatement pStmt = null;
|
142
|
ResultSet rs = null;
|
143
|
String sql = "select docid from xml_documents where docid like ?";
|
144
|
|
145
|
try
|
146
|
{
|
147
|
conn = DBConnectionPool.getDBConnection("select");
|
148
|
serialNumber = conn.getCheckOutSerialNumber();
|
149
|
System.out.println("serialNumber: "+serialNumber);
|
150
|
pStmt = conn.prepareStatement(sql);
|
151
|
pStmt.setString(1, "%jing%");
|
152
|
pStmt.execute();
|
153
|
rs = pStmt.getResultSet();
|
154
|
if (rs.next())
|
155
|
{
|
156
|
String str = rs.getString(1);
|
157
|
System.out.println("Select docid: "+str);
|
158
|
}//if
|
159
|
}//try
|
160
|
finally
|
161
|
{
|
162
|
try
|
163
|
{
|
164
|
pStmt.close();
|
165
|
}//try
|
166
|
finally
|
167
|
{
|
168
|
DBConnectionPool.returnDBConnection(conn,serialNumber);
|
169
|
System.out.println("return select conn!");
|
170
|
System.out.println("the return insert connection's status: "
|
171
|
+conn.getStatus());
|
172
|
}//finally
|
173
|
|
174
|
}//fianlly
|
175
|
|
176
|
}//select
|
177
|
|
178
|
public void run()
|
179
|
{
|
180
|
long index = (new Double (Math.random()*100000)).longValue();
|
181
|
try
|
182
|
{
|
183
|
insert(index);
|
184
|
select();
|
185
|
}//try
|
186
|
catch (SQLException e)
|
187
|
{
|
188
|
System.out.println("error is run: "+e.getMessage());
|
189
|
}
|
190
|
|
191
|
}
|
192
|
|
193
|
|
194
|
}//DBConnectionPoolTester
|