1 |
1086
|
tao
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A class represent a connection object, it includes connction
|
4 |
|
|
* itself, index, status, age and usageCount.
|
5 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
|
|
* Authors: Jing Tao
|
8 |
|
|
*
|
9 |
|
|
* '$Author$'
|
10 |
|
|
* '$Date$'
|
11 |
|
|
* '$Revision$'
|
12 |
|
|
*
|
13 |
|
|
* This program is free software; you can redistribute it and/or modify
|
14 |
|
|
* it under the terms of the GNU General Public License as published by
|
15 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
16 |
|
|
* (at your option) any later version.
|
17 |
|
|
*
|
18 |
|
|
* This program is distributed in the hope that it will be useful,
|
19 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
* GNU General Public License for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with this program; if not, write to the Free Software
|
25 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26 |
|
|
*/
|
27 |
|
|
|
28 |
|
|
package edu.ucsb.nceas.metacat;
|
29 |
|
|
|
30 |
|
|
import java.io.*;
|
31 |
|
|
import java.sql.*;
|
32 |
|
|
|
33 |
2663
|
sgarg
|
import org.apache.log4j.Logger;
|
34 |
|
|
|
35 |
4080
|
daigle
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
36 |
|
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
37 |
|
|
|
38 |
1086
|
tao
|
/**
|
39 |
|
|
* A class represent a connection object, it includes connection itself,
|
40 |
1088
|
tao
|
* index, status, age, createtime, connection time, usageCount, warning message
|
41 |
1086
|
tao
|
*/
|
42 |
|
|
|
43 |
1091
|
tao
|
public class DBConnection
|
44 |
1086
|
tao
|
{
|
45 |
|
|
private Connection conn;
|
46 |
1088
|
tao
|
private String tag;//to idenify this object
|
47 |
|
|
private int status;// free or using
|
48 |
1086
|
tao
|
private long age;
|
49 |
1088
|
tao
|
private long createTime;
|
50 |
|
|
private long connectionTime; //how long it use for connections,
|
51 |
|
|
//it is accumulated
|
52 |
1121
|
tao
|
private long checkOutTime; //the time when check it out
|
53 |
1088
|
tao
|
private int usageCount;// how many time the connection was used
|
54 |
1121
|
tao
|
private int checkOutSerialNumber; // a number to identify same check out.
|
55 |
|
|
//for a connection
|
56 |
1086
|
tao
|
private SQLWarning warningMessage;
|
57 |
1094
|
tao
|
private String checkOutMethodName;
|
58 |
1086
|
tao
|
|
59 |
1088
|
tao
|
|
60 |
4080
|
daigle
|
private static String DBDriver;
|
61 |
|
|
private static String DBConnectedJDBC;
|
62 |
|
|
private static String userName;
|
63 |
|
|
private static String passWord;
|
64 |
|
|
static {
|
65 |
|
|
try {
|
66 |
|
|
DBDriver=PropertyService.getProperty("database.driver");
|
67 |
|
|
DBConnectedJDBC=PropertyService.getProperty("database.connectionURI");
|
68 |
|
|
userName=PropertyService.getProperty("database.user");
|
69 |
|
|
passWord=PropertyService.getProperty("database.password");
|
70 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
71 |
|
|
System.err.println("Could not get property in static block: "
|
72 |
|
|
+ pnfe.getMessage());
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
2663
|
sgarg
|
private static Logger logMetacat = Logger.getLogger(DBConnection.class);
|
77 |
|
|
|
78 |
1086
|
tao
|
/**
|
79 |
1088
|
tao
|
* Default constructor of the DBConnection class
|
80 |
|
|
*
|
81 |
1086
|
tao
|
*/
|
82 |
1091
|
tao
|
public DBConnection() throws SQLException
|
83 |
1086
|
tao
|
{
|
84 |
1088
|
tao
|
conn = openConnection();
|
85 |
|
|
tag = conn.toString();
|
86 |
1086
|
tao
|
status = 0;
|
87 |
|
|
age = 0;
|
88 |
1088
|
tao
|
createTime = System.currentTimeMillis();
|
89 |
|
|
connectionTime = 0;
|
90 |
|
|
checkOutTime = 0;
|
91 |
1086
|
tao
|
usageCount= 0;
|
92 |
1121
|
tao
|
checkOutSerialNumber=0;
|
93 |
1086
|
tao
|
warningMessage = null;
|
94 |
1094
|
tao
|
checkOutMethodName = null;
|
95 |
1086
|
tao
|
|
96 |
|
|
}
|
97 |
|
|
|
98 |
1091
|
tao
|
|
99 |
1086
|
tao
|
|
100 |
|
|
/**
|
101 |
1088
|
tao
|
* get the connetion from the object
|
102 |
1086
|
tao
|
*/
|
103 |
1091
|
tao
|
public Connection getConnections()
|
104 |
1086
|
tao
|
{
|
105 |
|
|
return conn;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
/**
|
109 |
|
|
* Set a connection to this object
|
110 |
|
|
* @param myDBConnection, the connection which will be assign to this object
|
111 |
|
|
*/
|
112 |
1091
|
tao
|
public void setConnections( Connection myConnection)
|
113 |
1086
|
tao
|
{
|
114 |
1088
|
tao
|
this.conn = myConnection;
|
115 |
1086
|
tao
|
}
|
116 |
1088
|
tao
|
|
117 |
1086
|
tao
|
/**
|
118 |
1088
|
tao
|
* get the db connetion tag from the object
|
119 |
|
|
*/
|
120 |
|
|
public String getTag()
|
121 |
|
|
{
|
122 |
|
|
return tag;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* Set a connection status to this object
|
127 |
|
|
* @param myTag, the tag which will be assign to this object
|
128 |
|
|
*/
|
129 |
|
|
public void setTag(String myTag)
|
130 |
|
|
{
|
131 |
|
|
this.tag = myTag;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
1086
|
tao
|
* get the db connetion status from the object
|
136 |
|
|
*/
|
137 |
|
|
public int getStatus()
|
138 |
|
|
{
|
139 |
|
|
return status;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Set a connection status to this object
|
144 |
|
|
* @param myStatus, the status which will be assign to this object
|
145 |
|
|
* 0 is free, 1 is using
|
146 |
|
|
*/
|
147 |
|
|
public void setStatus(int myStatus)
|
148 |
|
|
{
|
149 |
|
|
this.status = myStatus;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
/**
|
153 |
|
|
* get the db connetion age from the object
|
154 |
|
|
*/
|
155 |
|
|
public long getAge()
|
156 |
|
|
{
|
157 |
1088
|
tao
|
return (System.currentTimeMillis() - createTime);
|
158 |
1086
|
tao
|
}
|
159 |
|
|
|
160 |
1088
|
tao
|
|
161 |
1086
|
tao
|
/**
|
162 |
|
|
* Set a connection age to this object
|
163 |
|
|
* @param myAge, the Age which will be assign to this object
|
164 |
|
|
*/
|
165 |
|
|
public void setAge(long myAge)
|
166 |
|
|
{
|
167 |
|
|
this.age = myAge;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
1088
|
tao
|
/**
|
171 |
|
|
* get the db connetion created time from the object
|
172 |
|
|
*/
|
173 |
|
|
public long getCreateTime()
|
174 |
|
|
{
|
175 |
|
|
return createTime;
|
176 |
|
|
}
|
177 |
1086
|
tao
|
|
178 |
|
|
/**
|
179 |
1088
|
tao
|
* Set a usage number to this object
|
180 |
|
|
* @param myCreateTime, the create time which will be assign to this object
|
181 |
|
|
*/
|
182 |
|
|
public void setCreateTime(long myCreateTime)
|
183 |
|
|
{
|
184 |
|
|
this.createTime = myCreateTime;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
/**
|
188 |
1121
|
tao
|
* get the how long db connetion used for the object
|
189 |
1088
|
tao
|
*/
|
190 |
|
|
public long getConnectionTime()
|
191 |
|
|
{
|
192 |
|
|
return connectionTime;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
/**
|
197 |
|
|
* Set a connection time to this object
|
198 |
|
|
* It is accumulated
|
199 |
|
|
* @param myConnectionTime, the connection time which will assign to
|
200 |
|
|
* this object
|
201 |
|
|
*/
|
202 |
|
|
public void setConnectionTime(long myConnectionTime)
|
203 |
|
|
{
|
204 |
|
|
this.connectionTime = this.connectionTime + myConnectionTime;
|
205 |
|
|
}
|
206 |
|
|
|
207 |
1121
|
tao
|
/**
|
208 |
|
|
* get the when a db connetion was checked out
|
209 |
1088
|
tao
|
*/
|
210 |
|
|
public long getCheckOutTime()
|
211 |
|
|
{
|
212 |
|
|
return checkOutTime;
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
/**
|
217 |
|
|
* Set check out time to this object
|
218 |
|
|
|
219 |
|
|
* @param myCheckOutTime, the check out time which will assign to
|
220 |
|
|
* this object
|
221 |
|
|
*/
|
222 |
|
|
public void setCheckOutTime(long myCheckOutTime)
|
223 |
|
|
{
|
224 |
|
|
this.checkOutTime = myCheckOutTime;
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
/**
|
228 |
1086
|
tao
|
* get the db connetion usage times from the object
|
229 |
|
|
*/
|
230 |
|
|
public int getUsageCount()
|
231 |
|
|
{
|
232 |
|
|
return usageCount;
|
233 |
|
|
}
|
234 |
1088
|
tao
|
|
235 |
|
|
|
236 |
1086
|
tao
|
/**
|
237 |
|
|
* Set a usage number to this object
|
238 |
|
|
* @param myUsageCount, number of usage which will be assign to this object
|
239 |
|
|
*/
|
240 |
|
|
public void setUsageCount(int myUsageCount)
|
241 |
|
|
{
|
242 |
|
|
this.usageCount = myUsageCount;
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
/**
|
246 |
1088
|
tao
|
* Increase a usage number to this object
|
247 |
|
|
* @param myUsageCount, number of usage which will be add to this object
|
248 |
|
|
*/
|
249 |
|
|
public void increaseUsageCount(int myUsageCount)
|
250 |
|
|
{
|
251 |
|
|
this.usageCount = this.usageCount + myUsageCount;
|
252 |
|
|
}
|
253 |
|
|
|
254 |
1121
|
tao
|
/**
|
255 |
|
|
* get the check out serial number
|
256 |
|
|
*/
|
257 |
|
|
public int getCheckOutSerialNumber()
|
258 |
|
|
{
|
259 |
|
|
return checkOutSerialNumber;
|
260 |
|
|
}
|
261 |
1088
|
tao
|
|
262 |
1121
|
tao
|
|
263 |
1088
|
tao
|
/**
|
264 |
1121
|
tao
|
* Set check out serial number to this object
|
265 |
|
|
|
266 |
|
|
* @param myCheckOutSerialNumber, the check out serial number which will
|
267 |
|
|
* assign to this object
|
268 |
|
|
*/
|
269 |
|
|
public void setCheckOutSerialNumber(int myCheckOutSerialNumber)
|
270 |
|
|
{
|
271 |
|
|
this.checkOutSerialNumber = myCheckOutSerialNumber;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
/**
|
275 |
|
|
* Increase a usage number to this object
|
276 |
|
|
* @param myUsageCount, number of usage which will be add to this object
|
277 |
|
|
*/
|
278 |
|
|
public void increaseCheckOutSerialNumber(int myCheckOutSerialNumber)
|
279 |
|
|
{
|
280 |
|
|
this.checkOutSerialNumber=this.checkOutSerialNumber+myCheckOutSerialNumber;
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
/**
|
285 |
1086
|
tao
|
* get the db connetion waring message from the object
|
286 |
|
|
*/
|
287 |
1091
|
tao
|
public SQLWarning getWarningMessage() throws SQLException
|
288 |
1086
|
tao
|
{
|
289 |
1121
|
tao
|
//should increase 1 UsageCount
|
290 |
|
|
increaseUsageCount(1);
|
291 |
1091
|
tao
|
return conn.getWarnings();
|
292 |
1086
|
tao
|
}
|
293 |
|
|
|
294 |
|
|
/**
|
295 |
|
|
* Set a warning message to this object
|
296 |
|
|
* @param myWarningMessage, the waring which will be assign to this object
|
297 |
|
|
*/
|
298 |
|
|
public void setWarningMessage(SQLWarning myWarningMessage)
|
299 |
|
|
{
|
300 |
|
|
this.warningMessage = myWarningMessage;
|
301 |
|
|
}
|
302 |
|
|
|
303 |
1088
|
tao
|
/**
|
304 |
1094
|
tao
|
* get the the name of method checked out the connection from the object
|
305 |
|
|
*/
|
306 |
|
|
public String getCheckOutMethodName()
|
307 |
|
|
{
|
308 |
|
|
return checkOutMethodName;
|
309 |
|
|
}
|
310 |
|
|
|
311 |
|
|
/**
|
312 |
|
|
* Set a method name to the checkOutMethodName
|
313 |
|
|
* @param myCheckOutMethodName, the name of method will assinged to it
|
314 |
|
|
*/
|
315 |
|
|
public void setCheckOutMethodName(String myCheckOutMethodName)
|
316 |
|
|
{
|
317 |
|
|
this.checkOutMethodName = myCheckOutMethodName;
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
/**
|
321 |
1088
|
tao
|
* Close a DBConnection object
|
322 |
|
|
*/
|
323 |
|
|
public void close() throws SQLException
|
324 |
|
|
{
|
325 |
|
|
conn.close();
|
326 |
|
|
tag = null;
|
327 |
|
|
status = 0;
|
328 |
|
|
age = 0;
|
329 |
|
|
createTime = System.currentTimeMillis();
|
330 |
|
|
connectionTime = 0;
|
331 |
|
|
checkOutTime = 0;
|
332 |
|
|
usageCount= 0;
|
333 |
|
|
warningMessage = null;
|
334 |
|
|
}
|
335 |
1086
|
tao
|
|
336 |
1088
|
tao
|
/**
|
337 |
1091
|
tao
|
* Method to establish DBConnection
|
338 |
1088
|
tao
|
*/
|
339 |
1091
|
tao
|
public static Connection openConnection()
|
340 |
1088
|
tao
|
throws SQLException
|
341 |
|
|
{
|
342 |
|
|
return openConnection(DBDriver, DBConnectedJDBC, userName, passWord);
|
343 |
|
|
}//openDBConnection
|
344 |
|
|
|
345 |
|
|
/**
|
346 |
|
|
* Method to establish a JDBC database connection
|
347 |
|
|
*
|
348 |
|
|
* @param dbDriver the string representing the database driver
|
349 |
|
|
* @param connection the string representing the database connectin parameters
|
350 |
|
|
* @param user name of the user to use for database connection
|
351 |
|
|
* @param password password for the user to use for database connection
|
352 |
|
|
*/
|
353 |
1091
|
tao
|
private static Connection openConnection(String dbDriver, String connection,
|
354 |
1088
|
tao
|
String user, String password)
|
355 |
|
|
throws SQLException
|
356 |
|
|
{
|
357 |
|
|
// Load the Oracle JDBC driver
|
358 |
|
|
try
|
359 |
|
|
{
|
360 |
|
|
Class.forName (dbDriver);
|
361 |
|
|
}
|
362 |
|
|
catch (ClassNotFoundException e)
|
363 |
|
|
{
|
364 |
2663
|
sgarg
|
logMetacat.error("Error in DBConnectionPool "+e.getMessage());
|
365 |
1088
|
tao
|
return null;
|
366 |
|
|
}
|
367 |
|
|
// Connect to the database
|
368 |
1091
|
tao
|
Connection connLocal = null;
|
369 |
|
|
connLocal = DriverManager.getConnection( connection, user, password);
|
370 |
|
|
return connLocal;
|
371 |
1088
|
tao
|
}//OpenDBConnection
|
372 |
1091
|
tao
|
|
373 |
4080
|
daigle
|
/**
|
374 |
|
|
* Method to test a JDBC database connection
|
375 |
|
|
*
|
376 |
|
|
* @param dbDriver the string representing the database driver
|
377 |
|
|
* @param connection the string representing the database connectin parameters
|
378 |
|
|
* @param user name of the user to use for database connection
|
379 |
|
|
* @param password password for the user to use for database connection
|
380 |
|
|
*/
|
381 |
|
|
public static void testConnection(String dbDriver, String connection,
|
382 |
|
|
String user, String password)
|
383 |
|
|
throws SQLException
|
384 |
|
|
{
|
385 |
|
|
// Load the Oracle JDBC driver
|
386 |
|
|
try
|
387 |
|
|
{
|
388 |
|
|
Class.forName (dbDriver);
|
389 |
|
|
}
|
390 |
|
|
catch (ClassNotFoundException e)
|
391 |
|
|
{
|
392 |
|
|
throw new SQLException(e.getMessage());
|
393 |
|
|
}
|
394 |
|
|
// Connect to the database
|
395 |
|
|
Connection connLocal = null;
|
396 |
|
|
connLocal = DriverManager.getConnection( connection, user, password);
|
397 |
|
|
connLocal.close();
|
398 |
|
|
}
|
399 |
|
|
|
400 |
1091
|
tao
|
/**
|
401 |
|
|
* Method to create a PreparedStatement by sending a sql statement
|
402 |
|
|
* @Param sql, the sql statement which will be sent to db
|
403 |
|
|
*/
|
404 |
|
|
public PreparedStatement prepareStatement( String sql ) throws SQLException
|
405 |
|
|
{
|
406 |
|
|
return conn.prepareStatement(sql);
|
407 |
|
|
}//prepareStatement
|
408 |
|
|
|
409 |
1132
|
tao
|
|
410 |
|
|
/**
|
411 |
|
|
* Method to create a Statement
|
412 |
|
|
*/
|
413 |
|
|
public Statement createStatement() throws SQLException
|
414 |
|
|
{
|
415 |
|
|
return conn.createStatement();
|
416 |
|
|
}//prepareStatement
|
417 |
1088
|
tao
|
|
418 |
1138
|
tao
|
/**
|
419 |
|
|
* Method to make a commit command
|
420 |
|
|
*/
|
421 |
|
|
public void commit() throws SQLException
|
422 |
|
|
{
|
423 |
|
|
conn.commit();
|
424 |
|
|
}//commit
|
425 |
1088
|
tao
|
|
426 |
1217
|
tao
|
/**
|
427 |
|
|
* Method to set commit mode
|
428 |
|
|
* @param autocommit, true of false to auto commit
|
429 |
|
|
*/
|
430 |
|
|
public void setAutoCommit( boolean autoCommit) throws SQLException
|
431 |
|
|
{
|
432 |
|
|
conn.setAutoCommit(autoCommit);
|
433 |
|
|
}//setAutoCommit
|
434 |
1138
|
tao
|
|
435 |
1217
|
tao
|
/**
|
436 |
|
|
* Method to roll back
|
437 |
|
|
*/
|
438 |
|
|
public void rollback() throws SQLException
|
439 |
|
|
{
|
440 |
|
|
conn.rollback();
|
441 |
|
|
}//rollback
|
442 |
|
|
|
443 |
|
|
/**
|
444 |
|
|
* Method to get meta data
|
445 |
|
|
*/
|
446 |
|
|
public DatabaseMetaData getMetaData() throws SQLException
|
447 |
|
|
{
|
448 |
|
|
return conn.getMetaData();
|
449 |
|
|
}//getMetaData
|
450 |
|
|
|
451 |
1086
|
tao
|
}//DBConnection class
|