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