Project

General

Profile

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