Revision 1088
Added by Jing Tao over 22 years ago
src/edu/ucsb/nceas/metacat/DBConnection.java | ||
---|---|---|
33 | 33 |
|
34 | 34 |
/** |
35 | 35 |
* A class represent a connection object, it includes connection itself, |
36 |
* index, status, age, usageCount, warning message |
|
36 |
* index, status, age, createtime, connection time, usageCount, warning message
|
|
37 | 37 |
*/ |
38 | 38 |
|
39 | 39 |
public class DBConnection |
40 | 40 |
{ |
41 | 41 |
private Connection conn; |
42 |
private int status; |
|
42 |
private String tag;//to idenify this object |
|
43 |
private int status;// free or using |
|
43 | 44 |
private long age; |
44 |
private int usageCount; |
|
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 |
|
45 | 50 |
private SQLWarning warningMessage; |
46 | 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 |
|
|
47 | 58 |
/** |
48 |
* default constructor of the DBConnection class |
|
49 |
* |
|
59 |
* Default constructor of the DBConnection class |
|
60 |
* It will create a new DBConnection with a new Connection |
|
61 |
* |
|
50 | 62 |
*/ |
51 |
public DBConnection() |
|
63 |
public DBConnection() throws SQLException
|
|
52 | 64 |
{ |
53 |
conn = null; |
|
65 |
conn = openConnection(); |
|
66 |
tag = conn.toString(); |
|
54 | 67 |
status = 0; |
55 | 68 |
age = 0; |
69 |
createTime = System.currentTimeMillis(); |
|
70 |
connectionTime = 0; |
|
71 |
checkOutTime = 0; |
|
56 | 72 |
usageCount= 0; |
57 | 73 |
warningMessage = null; |
58 | 74 |
|
... | ... | |
66 | 82 |
* @param age,the age of the connection |
67 | 83 |
* @param usageCount of the connection |
68 | 84 |
*/ |
69 |
public DBConnection (Connection conn, int status, int myAge,
|
|
70 |
int usageCount)
|
|
85 |
public DBConnection (Connection conn, int status, long age,
|
|
86 |
long createTime, long connectionTime, int usageCount)
|
|
71 | 87 |
throws SQLException |
72 | 88 |
{ |
73 | 89 |
this.conn = conn; |
90 |
this.tag = conn.toString(); |
|
74 | 91 |
this.status = status; |
75 | 92 |
this.age = age; |
93 |
this.createTime = createTime; |
|
94 |
this.connectionTime = connectionTime; |
|
76 | 95 |
this.usageCount = usageCount; |
77 | 96 |
try |
78 | 97 |
{ |
... | ... | |
85 | 104 |
} |
86 | 105 |
|
87 | 106 |
/** |
88 |
* get the db connetion from the object
|
|
107 |
* get the connetion from the object |
|
89 | 108 |
*/ |
90 |
public Connection getDBConnection()
|
|
109 |
public Connection getConnection() |
|
91 | 110 |
{ |
92 | 111 |
return conn; |
93 | 112 |
} |
... | ... | |
96 | 115 |
* Set a connection to this object |
97 | 116 |
* @param myDBConnection, the connection which will be assign to this object |
98 | 117 |
*/ |
99 |
public void setDBConnection( Connection myDBConnection)
|
|
118 |
public void setConnection( Connection myConnection)
|
|
100 | 119 |
{ |
101 |
this.conn = myDBConnection;
|
|
120 |
this.conn = myConnection; |
|
102 | 121 |
} |
103 |
|
|
122 |
|
|
104 | 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 |
/** |
|
105 | 141 |
* get the db connetion status from the object |
106 | 142 |
*/ |
107 | 143 |
public int getStatus() |
... | ... | |
124 | 160 |
*/ |
125 | 161 |
public long getAge() |
126 | 162 |
{ |
127 |
return age;
|
|
163 |
return (System.currentTimeMillis() - createTime);
|
|
128 | 164 |
} |
129 | 165 |
|
166 |
|
|
130 | 167 |
/** |
131 | 168 |
* Set a connection age to this object |
132 | 169 |
* @param myAge, the Age which will be assign to this object |
... | ... | |
136 | 173 |
this.age = myAge; |
137 | 174 |
} |
138 | 175 |
|
176 |
/** |
|
177 |
* get the db connetion created time from the object |
|
178 |
*/ |
|
179 |
public long getCreateTime() |
|
180 |
{ |
|
181 |
return createTime; |
|
182 |
} |
|
139 | 183 |
|
140 | 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 |
/** |
|
141 | 234 |
* get the db connetion usage times from the object |
142 | 235 |
*/ |
143 | 236 |
public int getUsageCount() |
144 | 237 |
{ |
145 | 238 |
return usageCount; |
146 | 239 |
} |
147 |
|
|
240 |
|
|
241 |
|
|
148 | 242 |
/** |
149 | 243 |
* Set a usage number to this object |
150 | 244 |
* @param myUsageCount, number of usage which will be assign to this object |
... | ... | |
155 | 249 |
} |
156 | 250 |
|
157 | 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 |
/** |
|
158 | 262 |
* get the db connetion waring message from the object |
159 | 263 |
*/ |
160 | 264 |
public SQLWarning getWarningMessage() |
... | ... | |
171 | 275 |
this.warningMessage = myWarningMessage; |
172 | 276 |
} |
173 | 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 |
} |
|
174 | 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 |
|
|
175 | 333 |
}//DBConnection class |
Also available in: Unified diff
Add some new attributes and methods in it.