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