1 |
1218
|
tao
|
/**
|
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$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
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 |
5027
|
daigle
|
import edu.ucsb.nceas.metacat.database.*;
|
35 |
1218
|
tao
|
|
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'%jing%'";
|
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.execute();
|
152 |
|
|
rs = pStmt.getResultSet();
|
153 |
|
|
if (rs.next())
|
154 |
|
|
{
|
155 |
|
|
String str = rs.getString(1);
|
156 |
|
|
System.out.println("Select docid: "+str);
|
157 |
|
|
}//if
|
158 |
|
|
}//try
|
159 |
|
|
finally
|
160 |
|
|
{
|
161 |
|
|
try
|
162 |
|
|
{
|
163 |
|
|
pStmt.close();
|
164 |
|
|
}//try
|
165 |
|
|
finally
|
166 |
|
|
{
|
167 |
|
|
DBConnectionPool.returnDBConnection(conn,serialNumber);
|
168 |
|
|
System.out.println("return select conn!");
|
169 |
|
|
System.out.println("the return insert connection's status: "
|
170 |
|
|
+conn.getStatus());
|
171 |
|
|
}//finally
|
172 |
|
|
|
173 |
|
|
}//fianlly
|
174 |
|
|
|
175 |
|
|
}//select
|
176 |
|
|
|
177 |
|
|
public void run()
|
178 |
|
|
{
|
179 |
|
|
long index = (new Double (Math.random()*100000)).longValue();
|
180 |
|
|
try
|
181 |
|
|
{
|
182 |
|
|
insert(index);
|
183 |
|
|
select();
|
184 |
|
|
}//try
|
185 |
|
|
catch (SQLException e)
|
186 |
|
|
{
|
187 |
|
|
System.out.println("error is run: "+e.getMessage());
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
}//DBConnectionPoolTester
|