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