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