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-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
11
 * '$Revision: 4080 $'
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
  
60
  private static String  DBDriver;
61
  private static String  DBConnectedJDBC;
62
  private static String  userName;
63
  private static String  passWord;
64
  static {
65
		try {
66
			DBDriver=PropertyService.getProperty("database.driver");
67
			DBConnectedJDBC=PropertyService.getProperty("database.connectionURI");
68
			userName=PropertyService.getProperty("database.user");
69
			passWord=PropertyService.getProperty("database.password");
70
		} catch (PropertyNotFoundException pnfe) {
71
			System.err.println("Could not get property in static block: " 
72
					+ pnfe.getMessage());
73
		}
74
	}
75
  
76
  private static Logger logMetacat = Logger.getLogger(DBConnection.class);
77

    
78
  /**
79
   * Default constructor of the DBConnection class 
80
   * 
81
   */
82
  public DBConnection()  throws SQLException
83
  {
84
    conn = openConnection();
85
    tag = conn.toString();
86
    status = 0;
87
    age = 0;
88
    createTime = System.currentTimeMillis();
89
    connectionTime = 0;
90
    checkOutTime = 0;
91
    usageCount= 0;
92
    checkOutSerialNumber=0;
93
    warningMessage = null;
94
    checkOutMethodName = null;
95
    
96
  }
97
  
98
 
99
  
100
  /**
101
   * get the  connetion from the object
102
   */
103
  public Connection getConnections()
104
  {
105
    return conn;
106
  }
107
  
108
  /**
109
   * Set a connection to this object
110
   * @param myDBConnection, the connection which will be assign to this object
111
   */
112
  public void setConnections( Connection myConnection)
113
  {
114
    this.conn = myConnection;
115
  }
116

    
117
  /**
118
   * get the db connetion tag from the object
119
   */
120
  public String getTag()
121
  {
122
    return tag;
123
  }
124
  
125
  /**
126
   * Set a connection status to this object
127
   * @param myTag, the tag which will be assign to this object
128
   */
129
  public void setTag(String myTag)
130
  {
131
    this.tag = myTag;
132
  }
133
  
134
  /**
135
   * get the db connetion status from the object
136
   */
137
  public int getStatus()
138
  {
139
    return status;
140
  }
141
  
142
  /**
143
   * Set a connection status to this object
144
   * @param myStatus, the status which will be assign to this object
145
   * 0 is free, 1 is using
146
   */
147
  public void setStatus(int myStatus)
148
  {
149
    this.status = myStatus;
150
  }
151
  
152
  /**
153
   * get the db connetion age from the object
154
   */
155
  public long getAge()
156
  {
157
    return (System.currentTimeMillis() - createTime);
158
  }
159
  
160
 
161
  /**
162
   * Set a connection age to this object
163
   * @param myAge, the Age which will be assign to this object
164
   */
165
  public void setAge(long myAge)
166
  {
167
    this.age = myAge;
168
  }  
169
  
170
  /**
171
   * get the db connetion created time from the object
172
   */
173
  public long getCreateTime()
174
  {
175
    return createTime;
176
  }
177
  
178
  /**
179
   * Set a usage number to this object
180
   * @param myCreateTime, the create time which will be assign to this object
181
   */
182
  public void setCreateTime(long myCreateTime)
183
  {
184
    this.createTime = myCreateTime;
185
  }
186
  
187
  /**
188
   * get the how long db connetion used for the object
189
   */
190
  public long getConnectionTime()
191
  {
192
    return connectionTime;
193
  }
194
  
195
 
196
  /**
197
   * Set a connection time to this object
198
   * It is accumulated
199
   * @param myConnectionTime, the connection time which will assign to
200
   * this object
201
   */
202
  public void setConnectionTime(long myConnectionTime)
203
  {
204
    this.connectionTime = this.connectionTime + myConnectionTime;
205
  }
206
  
207
  /**
208
   * get the when a db connetion was checked out
209
   */
210
  public long getCheckOutTime()
211
  {
212
    return checkOutTime;
213
  }
214
  
215
 
216
  /**
217
   * Set check out time to this object
218
  
219
   * @param myCheckOutTime, the check out time which will assign to
220
   * this object
221
   */
222
  public void setCheckOutTime(long myCheckOutTime)
223
  {
224
    this.checkOutTime = myCheckOutTime;
225
  }
226
  
227
  /**
228
   * get the db connetion usage times from the object
229
   */
230
  public int getUsageCount()
231
  {
232
    return usageCount;
233
  }
234
   
235
 
236
  /**
237
   * Set a usage number to this object
238
   * @param myUsageCount, number of usage which will be assign to this object
239
   */
240
  public void setUsageCount(int myUsageCount)
241
  {
242
    this.usageCount = myUsageCount;
243
  }
244
  
245
  /**
246
   * Increase a usage number to this object
247
   * @param myUsageCount, number of usage which will be add to this object
248
   */
249
  public void increaseUsageCount(int myUsageCount)
250
  {
251
    this.usageCount = this.usageCount + myUsageCount;
252
  }  
253
  
254
  /**
255
   * get the check out serial number
256
   */
257
  public int getCheckOutSerialNumber()
258
  {
259
    return checkOutSerialNumber;
260
  }
261
  
262
 
263
  /**
264
   * Set check out serial number to this object
265
  
266
   * @param myCheckOutSerialNumber, the check out serial number which will 
267
   * assign to this object
268
   */
269
  public void setCheckOutSerialNumber(int myCheckOutSerialNumber)
270
  {
271
    this.checkOutSerialNumber = myCheckOutSerialNumber;
272
  }
273
  
274
  /**
275
   * Increase a usage number to this object
276
   * @param myUsageCount, number of usage which will be add to this object
277
   */
278
  public void increaseCheckOutSerialNumber(int myCheckOutSerialNumber)
279
  {
280
    this.checkOutSerialNumber=this.checkOutSerialNumber+myCheckOutSerialNumber;
281
  }  
282
  
283
  
284
  /**
285
   * get the db connetion waring message from the object
286
   */
287
  public SQLWarning getWarningMessage() throws SQLException
288
  {
289
    //should increase 1 UsageCount
290
    increaseUsageCount(1);
291
    return conn.getWarnings();
292
  }
293
  
294
  /**
295
   * Set a warning message to this object
296
   * @param myWarningMessage, the waring which will be assign to this object
297
   */
298
  public void setWarningMessage(SQLWarning myWarningMessage)
299
  {
300
     this.warningMessage = myWarningMessage;
301
  }
302
  
303
  /**
304
   * get the the name of method checked out the connection from the object
305
   */
306
  public String getCheckOutMethodName()
307
  {
308
    return checkOutMethodName;
309
  }
310
  
311
  /**
312
   * Set a method name to the checkOutMethodName 
313
   * @param myCheckOutMethodName, the name of method will assinged to it
314
   */
315
  public void setCheckOutMethodName(String myCheckOutMethodName)
316
  {
317
     this.checkOutMethodName = myCheckOutMethodName;
318
  }
319
  
320
  /**
321
   * Close a DBConnection object
322
   */
323
  public void close() throws SQLException
324
  {
325
    conn.close();
326
    tag = null;
327
    status = 0;
328
    age = 0;
329
    createTime = System.currentTimeMillis();
330
    connectionTime = 0;
331
    checkOutTime = 0;
332
    usageCount= 0;
333
    warningMessage = null;
334
  }
335
  
336
    /** 
337
   * Method to establish DBConnection 
338
   */
339
  public static Connection openConnection()
340
                  throws SQLException 
341
  {
342
    return openConnection(DBDriver, DBConnectedJDBC, userName, passWord);
343
  }//openDBConnection
344

    
345
  /** 
346
   * Method to establish a JDBC database connection 
347
   *
348
   * @param dbDriver the string representing the database driver
349
   * @param connection the string representing the database connectin parameters
350
   * @param user name of the user to use for database connection
351
   * @param password password for the user to use for database connection
352
   */
353
  private static Connection openConnection(String dbDriver, String connection,
354
                String user, String password)
355
                throws SQLException
356
 {
357
     // Load the Oracle JDBC driver
358
     try
359
     {
360
       Class.forName (dbDriver);
361
     }
362
     catch (ClassNotFoundException e)
363
     {
364
       logMetacat.error("Error in DBConnectionPool "+e.getMessage());
365
       return null;
366
     }
367
     // Connect to the database
368
     Connection connLocal = null;
369
     connLocal = DriverManager.getConnection( connection, user, password);
370
     return connLocal;
371
  }//OpenDBConnection
372
  
373
  /** 
374
   * Method to test a JDBC database connection 
375
   *
376
   * @param dbDriver the string representing the database driver
377
   * @param connection the string representing the database connectin parameters
378
   * @param user name of the user to use for database connection
379
   * @param password password for the user to use for database connection
380
   */
381
  public static void testConnection(String dbDriver, String connection,
382
                String user, String password)
383
                throws SQLException
384
  {
385
     // Load the Oracle JDBC driver
386
     try
387
     {
388
       Class.forName (dbDriver);
389
     }
390
     catch (ClassNotFoundException e)
391
     {
392
       throw new SQLException(e.getMessage());
393
     }
394
     // Connect to the database
395
     Connection connLocal = null;
396
     connLocal = DriverManager.getConnection( connection, user, password);
397
     connLocal.close();
398
  }
399
  
400
  /**
401
   * Method to create a PreparedStatement by sending a sql statement
402
   * @Param sql, the sql statement which will be sent to db
403
   */
404
  public PreparedStatement prepareStatement( String sql ) throws SQLException
405
  {
406
    return conn.prepareStatement(sql);
407
  }//prepareStatement
408
  
409
  
410
  /**
411
   * Method to create a Statement
412
   */
413
  public Statement createStatement() throws SQLException
414
  {
415
    return conn.createStatement();
416
  }//prepareStatement
417

    
418
  /**
419
   * Method to make a commit command
420
   */
421
  public void commit() throws SQLException
422
  {
423
    conn.commit();
424
  }//commit
425
  
426
  /**
427
   * Method to set commit mode
428
   * @param autocommit, true of false to auto commit
429
   */
430
  public void setAutoCommit( boolean autoCommit) throws SQLException
431
  {
432
    conn.setAutoCommit(autoCommit);
433
  }//setAutoCommit
434
  
435
  /**
436
   * Method to roll back
437
   */
438
  public void rollback() throws SQLException
439
  {
440
    conn.rollback();
441
  }//rollback
442
  
443
  /**
444
   * Method to get meta data
445
   */
446
  public DatabaseMetaData getMetaData() throws SQLException
447
  {
448
    return conn.getMetaData();
449
  }//getMetaData
450
  
451
}//DBConnection class
(17-17/68)