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