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