Project

General

Profile

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
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2008-12-19 09:44:12 -0800 (Fri, 19 Dec 2008) $'
11
 * '$Revision: 4686 $'
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
import org.apache.log4j.Logger;
34

    
35
import edu.ucsb.nceas.metacat.service.PropertyService;
36
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
37

    
38
/**
39
 * A class represent a connection object, it includes connection itself, 
40
 * index, status, age, createtime, connection time, usageCount, warning message
41
 */
42
 
43
public class DBConnection 
44
{
45
  private Connection conn;
46
  private String tag;//to idenify this object
47
  private int status;// free or using
48
  private long age;
49
  private long createTime;
50
  private long connectionTime; //how long it use for connections, 
51
                               //it is accumulated
52
  private long checkOutTime; //the time when check it out
53
  private int usageCount;// how many time the connection was used
54
  private int checkOutSerialNumber; // a number to identify same check out.
55
                                     //for a connection
56
  private SQLWarning warningMessage;
57
  private String checkOutMethodName;
58
  
59
  private static String  DBDriver;
60
  private static String  DBConnectedJDBC;
61
  private static String  userName;
62
  private static String  passWord;
63
  
64
  private static Logger logMetacat = Logger.getLogger(DBConnection.class);
65

    
66
  /**
67
   * Default constructor of the DBConnection class 
68
   * 
69
   */
70
  public DBConnection()  throws SQLException
71
  {
72
	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
    conn = openConnection();
83
    tag = conn.toString();
84
    status = 0;
85
    age = 0;
86
    createTime = System.currentTimeMillis();
87
    connectionTime = 0;
88
    checkOutTime = 0;
89
    usageCount= 0;
90
    checkOutSerialNumber=0;
91
    warningMessage = null;
92
    checkOutMethodName = null;
93
    
94
  }
95
  
96
 
97
  
98
  /**
99
   * get the  connetion from the object
100
   */
101
  public Connection getConnections()
102
  {
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
  public void setConnections( Connection myConnection)
111
  {
112
    this.conn = myConnection;
113
  }
114

    
115
  /**
116
   * 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
   * 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
    return (System.currentTimeMillis() - createTime);
156
  }
157
  
158
 
159
  /**
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
  /**
169
   * get the db connetion created time from the object
170
   */
171
  public long getCreateTime()
172
  {
173
    return createTime;
174
  }
175
  
176
  /**
177
   * 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
   * get the how long db connetion used for the object
187
   */
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
  /**
206
   * get the when a db connetion was checked out
207
   */
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
   * get the db connetion usage times from the object
227
   */
228
  public int getUsageCount()
229
  {
230
    return usageCount;
231
  }
232
   
233
 
234
  /**
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
   * 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
  /**
253
   * get the check out serial number
254
   */
255
  public int getCheckOutSerialNumber()
256
  {
257
    return checkOutSerialNumber;
258
  }
259
  
260
 
261
  /**
262
   * 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
   * get the db connetion waring message from the object
284
   */
285
  public SQLWarning getWarningMessage() throws SQLException
286
  {
287
    //should increase 1 UsageCount
288
    increaseUsageCount(1);
289
    return conn.getWarnings();
290
  }
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
  /**
302
   * 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
   * 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
  
334
    /** 
335
   * Method to establish DBConnection 
336
   */
337
  public static Connection openConnection()
338
                  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
  private static Connection openConnection(String dbDriver, String connection,
352
                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
       logMetacat.error("Error in DBConnectionPool "+e.getMessage());
363
       return null;
364
     }
365
     // Connect to the database
366
     Connection connLocal = null;
367
     connLocal = DriverManager.getConnection( connection, user, password);
368
     return connLocal;
369
  }//OpenDBConnection
370
  
371
  /** 
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
  /**
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
  
408
  /**
409
   * Method to create a Statement
410
   */
411
  public Statement createStatement() throws SQLException
412
  {
413
    return conn.createStatement();
414
  }//prepareStatement
415

    
416
  /**
417
   * Method to make a commit command
418
   */
419
  public void commit() throws SQLException
420
  {
421
    conn.commit();
422
  }//commit
423
  
424
  /**
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
  
433
  /**
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
}//DBConnection class
(17-17/69)