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
 *    Release: @release@
9
 *
10
 *   '$Author: tao $'
11
 *     '$Date: 2002-05-20 13:08:38 -0700 (Mon, 20 May 2002) $'
12
 * '$Revision: 1094 $'
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
 * index, status, age, createtime, connection time, usageCount, warning message
37
 */
38
 
39
public class DBConnection 
40
{
41
  private Connection conn;
42
  private String tag;//to idenify this object
43
  private int status;// free or using
44
  private long age;
45
  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
  private SQLWarning warningMessage;
51
  private String checkOutMethodName;
52
  
53
  
54
  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
  
59
  /**
60
   * Default constructor of the DBConnection class 
61
   * 
62
   */
63
  public DBConnection()  throws SQLException
64
  {
65
    conn = openConnection();
66
    tag = conn.toString();
67
    status = 0;
68
    age = 0;
69
    createTime = System.currentTimeMillis();
70
    connectionTime = 0;
71
    checkOutTime = 0;
72
    usageCount= 0;
73
    warningMessage = null;
74
    checkOutMethodName = null;
75
    
76
  }
77
  
78
 
79
  
80
  /**
81
   * get the  connetion from the object
82
   */
83
  public Connection getConnections()
84
  {
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
  public void setConnections( Connection myConnection)
93
  {
94
    this.conn = myConnection;
95
  }
96

    
97
  /**
98
   * 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
   * 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
    return (System.currentTimeMillis() - createTime);
138
  }
139
  
140
 
141
  /**
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
  /**
151
   * get the db connetion created time from the object
152
   */
153
  public long getCreateTime()
154
  {
155
    return createTime;
156
  }
157
  
158
  /**
159
   * 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
   * get the db connetion usage times from the object
209
   */
210
  public int getUsageCount()
211
  {
212
    return usageCount;
213
  }
214
   
215
 
216
  /**
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
   * 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
   * get the db connetion waring message from the object
237
   */
238
  public SQLWarning getWarningMessage() throws SQLException
239
  {
240
    return conn.getWarnings();
241
  }
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
  /**
253
   * 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
   * 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
  
285
    /** 
286
   * Method to establish DBConnection 
287
   */
288
  public static Connection openConnection()
289
                  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
  private static Connection openConnection(String dbDriver, String connection,
303
                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
     Connection connLocal = null;
318
     connLocal = DriverManager.getConnection( connection, user, password);
319
     return connLocal;
320
  }//OpenDBConnection
321
  
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

    
332
 
333
  
334
}//DBConnection class
(13-13/43)