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