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-05-16 17:43:38 -0700 (Thu, 16 May 2002) $'
|
12
|
* '$Revision: 1088 $'
|
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;
|
49
|
private int usageCount;// how many time the connection was used
|
50
|
private SQLWarning warningMessage;
|
51
|
|
52
|
|
53
|
private String DBDriver=MetaCatUtil.getOption("dbDriver");
|
54
|
private String DBConnectedJDBC=MetaCatUtil.getOption("defaultDB");
|
55
|
private String userName=MetaCatUtil.getOption("user");
|
56
|
private String passWord=MetaCatUtil.getOption("password");
|
57
|
|
58
|
/**
|
59
|
* Default constructor of the DBConnection class
|
60
|
* It will create a new DBConnection with a new Connection
|
61
|
*
|
62
|
*/
|
63
|
public DBConnection() throws SQLException
|
64
|
{
|
65
|
conn = openConnection();
|
66
|
tag = conn.toString();
|
67
|
status = 0;
|
68
|
age = 0;
|
69
|
createTime = System.currentTimeMillis();
|
70
|
connectionTime = 0;
|
71
|
checkOutTime = 0;
|
72
|
usageCount= 0;
|
73
|
warningMessage = null;
|
74
|
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* construct an instance of the DBQConnection class
|
79
|
*
|
80
|
* @param conn, the JDBC connection itself.
|
81
|
* @param status, the status of this connection, free or using
|
82
|
* @param age,the age of the connection
|
83
|
* @param usageCount of the connection
|
84
|
*/
|
85
|
public DBConnection (Connection conn, int status, long age,
|
86
|
long createTime, long connectionTime, int usageCount)
|
87
|
throws SQLException
|
88
|
{
|
89
|
this.conn = conn;
|
90
|
this.tag = conn.toString();
|
91
|
this.status = status;
|
92
|
this.age = age;
|
93
|
this.createTime = createTime;
|
94
|
this.connectionTime = connectionTime;
|
95
|
this.usageCount = usageCount;
|
96
|
try
|
97
|
{
|
98
|
this.warningMessage = conn.getWarnings();
|
99
|
}
|
100
|
catch (SQLException e)
|
101
|
{
|
102
|
throw e;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* get the connetion from the object
|
108
|
*/
|
109
|
public Connection getConnection()
|
110
|
{
|
111
|
return conn;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Set a connection to this object
|
116
|
* @param myDBConnection, the connection which will be assign to this object
|
117
|
*/
|
118
|
public void setConnection( Connection myConnection)
|
119
|
{
|
120
|
this.conn = myConnection;
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* get the db connetion tag from the object
|
125
|
*/
|
126
|
public String getTag()
|
127
|
{
|
128
|
return tag;
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Set a connection status to this object
|
133
|
* @param myTag, the tag which will be assign to this object
|
134
|
*/
|
135
|
public void setTag(String myTag)
|
136
|
{
|
137
|
this.tag = myTag;
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* get the db connetion status from the object
|
142
|
*/
|
143
|
public int getStatus()
|
144
|
{
|
145
|
return status;
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Set a connection status to this object
|
150
|
* @param myStatus, the status which will be assign to this object
|
151
|
* 0 is free, 1 is using
|
152
|
*/
|
153
|
public void setStatus(int myStatus)
|
154
|
{
|
155
|
this.status = myStatus;
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* get the db connetion age from the object
|
160
|
*/
|
161
|
public long getAge()
|
162
|
{
|
163
|
return (System.currentTimeMillis() - createTime);
|
164
|
}
|
165
|
|
166
|
|
167
|
/**
|
168
|
* Set a connection age to this object
|
169
|
* @param myAge, the Age which will be assign to this object
|
170
|
*/
|
171
|
public void setAge(long myAge)
|
172
|
{
|
173
|
this.age = myAge;
|
174
|
}
|
175
|
|
176
|
/**
|
177
|
* get the db connetion created time from the object
|
178
|
*/
|
179
|
public long getCreateTime()
|
180
|
{
|
181
|
return createTime;
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* Set a usage number to this object
|
186
|
* @param myCreateTime, the create time which will be assign to this object
|
187
|
*/
|
188
|
public void setCreateTime(long myCreateTime)
|
189
|
{
|
190
|
this.createTime = myCreateTime;
|
191
|
}
|
192
|
|
193
|
/**
|
194
|
* get the how long db connetion usedfrom the object
|
195
|
*/
|
196
|
public long getConnectionTime()
|
197
|
{
|
198
|
return connectionTime;
|
199
|
}
|
200
|
|
201
|
|
202
|
/**
|
203
|
* Set a connection time to this object
|
204
|
* It is accumulated
|
205
|
* @param myConnectionTime, the connection time which will assign to
|
206
|
* this object
|
207
|
*/
|
208
|
public void setConnectionTime(long myConnectionTime)
|
209
|
{
|
210
|
this.connectionTime = this.connectionTime + myConnectionTime;
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* get the how long db connetion usedfrom the object
|
215
|
*/
|
216
|
public long getCheckOutTime()
|
217
|
{
|
218
|
return checkOutTime;
|
219
|
}
|
220
|
|
221
|
|
222
|
/**
|
223
|
* Set check out time to this object
|
224
|
|
225
|
* @param myCheckOutTime, the check out time which will assign to
|
226
|
* this object
|
227
|
*/
|
228
|
public void setCheckOutTime(long myCheckOutTime)
|
229
|
{
|
230
|
this.checkOutTime = myCheckOutTime;
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* get the db connetion usage times from the object
|
235
|
*/
|
236
|
public int getUsageCount()
|
237
|
{
|
238
|
return usageCount;
|
239
|
}
|
240
|
|
241
|
|
242
|
/**
|
243
|
* Set a usage number to this object
|
244
|
* @param myUsageCount, number of usage which will be assign to this object
|
245
|
*/
|
246
|
public void setUsageCount(int myUsageCount)
|
247
|
{
|
248
|
this.usageCount = myUsageCount;
|
249
|
}
|
250
|
|
251
|
/**
|
252
|
* Increase a usage number to this object
|
253
|
* @param myUsageCount, number of usage which will be add to this object
|
254
|
*/
|
255
|
public void increaseUsageCount(int myUsageCount)
|
256
|
{
|
257
|
this.usageCount = this.usageCount + myUsageCount;
|
258
|
}
|
259
|
|
260
|
|
261
|
/**
|
262
|
* get the db connetion waring message from the object
|
263
|
*/
|
264
|
public SQLWarning getWarningMessage()
|
265
|
{
|
266
|
return warningMessage;
|
267
|
}
|
268
|
|
269
|
/**
|
270
|
* Set a warning message to this object
|
271
|
* @param myWarningMessage, the waring which will be assign to this object
|
272
|
*/
|
273
|
public void setWarningMessage(SQLWarning myWarningMessage)
|
274
|
{
|
275
|
this.warningMessage = myWarningMessage;
|
276
|
}
|
277
|
|
278
|
/**
|
279
|
* Close a DBConnection object
|
280
|
*/
|
281
|
public void close() throws SQLException
|
282
|
{
|
283
|
conn.close();
|
284
|
conn=null;
|
285
|
tag = null;
|
286
|
status = 0;
|
287
|
age = 0;
|
288
|
createTime = System.currentTimeMillis();
|
289
|
connectionTime = 0;
|
290
|
checkOutTime = 0;
|
291
|
usageCount= 0;
|
292
|
warningMessage = null;
|
293
|
}
|
294
|
|
295
|
/**
|
296
|
* Method to establish a JDBC database connection
|
297
|
*/
|
298
|
private Connection openConnection()
|
299
|
throws SQLException
|
300
|
{
|
301
|
return openConnection(DBDriver, DBConnectedJDBC, userName, passWord);
|
302
|
}//openDBConnection
|
303
|
|
304
|
/**
|
305
|
* Method to establish a JDBC database connection
|
306
|
*
|
307
|
* @param dbDriver the string representing the database driver
|
308
|
* @param connection the string representing the database connectin parameters
|
309
|
* @param user name of the user to use for database connection
|
310
|
* @param password password for the user to use for database connection
|
311
|
*/
|
312
|
private Connection openConnection(String dbDriver, String connection,
|
313
|
String user, String password)
|
314
|
throws SQLException
|
315
|
{
|
316
|
|
317
|
// Load the Oracle JDBC driver
|
318
|
try
|
319
|
{
|
320
|
Class.forName (dbDriver);
|
321
|
}
|
322
|
catch (ClassNotFoundException e)
|
323
|
{
|
324
|
MetaCatUtil.debugMessage("Error in DBConnectionPool "+e.getMessage(),30);
|
325
|
return null;
|
326
|
}
|
327
|
// Connect to the database
|
328
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
329
|
return conn;
|
330
|
}//OpenDBConnection
|
331
|
|
332
|
|
333
|
}//DBConnection class
|