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: tao $'
|
11
|
* '$Date: 2002-06-13 11:54:51 -0700 (Thu, 13 Jun 2002) $'
|
12
|
* '$Revision: 1217 $'
|
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
|
* index, status, age, createtime, connection time, usageCount, warning message
|
37
|
*/
|
38
|
|
39
|
public class DBConnection
|
40
|
{
|
41
|
private Connection conn;
|
42
|
private String tag;//to idenify this object
|
43
|
private int status;// free or using
|
44
|
private long age;
|
45
|
private long createTime;
|
46
|
private long connectionTime; //how long it use for connections,
|
47
|
//it is accumulated
|
48
|
private long checkOutTime; //the time when check it out
|
49
|
private int usageCount;// how many time the connection was used
|
50
|
private int checkOutSerialNumber; // a number to identify same check out.
|
51
|
//for a connection
|
52
|
private SQLWarning warningMessage;
|
53
|
private String checkOutMethodName;
|
54
|
|
55
|
|
56
|
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
|
|
61
|
/**
|
62
|
* Default constructor of the DBConnection class
|
63
|
*
|
64
|
*/
|
65
|
public DBConnection() throws SQLException
|
66
|
{
|
67
|
conn = openConnection();
|
68
|
tag = conn.toString();
|
69
|
status = 0;
|
70
|
age = 0;
|
71
|
createTime = System.currentTimeMillis();
|
72
|
connectionTime = 0;
|
73
|
checkOutTime = 0;
|
74
|
usageCount= 0;
|
75
|
checkOutSerialNumber=0;
|
76
|
warningMessage = null;
|
77
|
checkOutMethodName = null;
|
78
|
|
79
|
}
|
80
|
|
81
|
|
82
|
|
83
|
/**
|
84
|
* get the connetion from the object
|
85
|
*/
|
86
|
public Connection getConnections()
|
87
|
{
|
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
|
public void setConnections( Connection myConnection)
|
96
|
{
|
97
|
this.conn = myConnection;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* 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
|
* 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
|
return (System.currentTimeMillis() - createTime);
|
141
|
}
|
142
|
|
143
|
|
144
|
/**
|
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
|
/**
|
154
|
* get the db connetion created time from the object
|
155
|
*/
|
156
|
public long getCreateTime()
|
157
|
{
|
158
|
return createTime;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* 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
|
* get the how long db connetion used for the object
|
172
|
*/
|
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
|
/**
|
191
|
* get the when a db connetion was checked out
|
192
|
*/
|
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
|
* get the db connetion usage times from the object
|
212
|
*/
|
213
|
public int getUsageCount()
|
214
|
{
|
215
|
return usageCount;
|
216
|
}
|
217
|
|
218
|
|
219
|
/**
|
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
|
* 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
|
/**
|
238
|
* get the check out serial number
|
239
|
*/
|
240
|
public int getCheckOutSerialNumber()
|
241
|
{
|
242
|
return checkOutSerialNumber;
|
243
|
}
|
244
|
|
245
|
|
246
|
/**
|
247
|
* 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
|
* get the db connetion waring message from the object
|
269
|
*/
|
270
|
public SQLWarning getWarningMessage() throws SQLException
|
271
|
{
|
272
|
//should increase 1 UsageCount
|
273
|
increaseUsageCount(1);
|
274
|
return conn.getWarnings();
|
275
|
}
|
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
|
/**
|
287
|
* 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
|
* 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
|
|
319
|
/**
|
320
|
* Method to establish DBConnection
|
321
|
*/
|
322
|
public static Connection openConnection()
|
323
|
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
|
private static Connection openConnection(String dbDriver, String connection,
|
337
|
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
|
Connection connLocal = null;
|
352
|
connLocal = DriverManager.getConnection( connection, user, password);
|
353
|
return connLocal;
|
354
|
}//OpenDBConnection
|
355
|
|
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
|
|
366
|
/**
|
367
|
* Method to create a Statement
|
368
|
*/
|
369
|
public Statement createStatement() throws SQLException
|
370
|
{
|
371
|
return conn.createStatement();
|
372
|
}//prepareStatement
|
373
|
|
374
|
/**
|
375
|
* Method to make a commit command
|
376
|
*/
|
377
|
public void commit() throws SQLException
|
378
|
{
|
379
|
conn.commit();
|
380
|
}//commit
|
381
|
|
382
|
/**
|
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
|
|
391
|
/**
|
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
|
}//DBConnection class
|