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