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-17 17:19:20 -0700 (Fri, 17 May 2002) $'
12
 * '$Revision: 1091 $'
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
  
52
  
53
  private static String  DBDriver=MetaCatUtil.getOption("dbDriver");
54
  private static String  DBConnectedJDBC=MetaCatUtil.getOption("defaultDB");
55
  private static String  userName=MetaCatUtil.getOption("user");
56
  private static String  passWord=MetaCatUtil.getOption("password");
57
  
58
  /**
59
   * Default constructor of the DBConnection class 
60
   * 
61
   */
62
  public DBConnection()  throws SQLException
63
  {
64
    conn = openConnection();
65
    tag = conn.toString();
66
    status = 0;
67
    age = 0;
68
    createTime = System.currentTimeMillis();
69
    connectionTime = 0;
70
    checkOutTime = 0;
71
    usageCount= 0;
72
    warningMessage = null;
73
    
74
  }
75
  
76
 
77
  
78
  /**
79
   * get the  connetion from the object
80
   */
81
  public Connection getConnections()
82
  {
83
    return conn;
84
  }
85
  
86
  /**
87
   * Set a connection to this object
88
   * @param myDBConnection, the connection which will be assign to this object
89
   */
90
  public void setConnections( Connection myConnection)
91
  {
92
    this.conn = myConnection;
93
  }
94

    
95
  /**
96
   * get the db connetion tag from the object
97
   */
98
  public String getTag()
99
  {
100
    return tag;
101
  }
102
  
103
  /**
104
   * Set a connection status to this object
105
   * @param myTag, the tag which will be assign to this object
106
   */
107
  public void setTag(String myTag)
108
  {
109
    this.tag = myTag;
110
  }
111
  
112
  /**
113
   * get the db connetion status from the object
114
   */
115
  public int getStatus()
116
  {
117
    return status;
118
  }
119
  
120
  /**
121
   * Set a connection status to this object
122
   * @param myStatus, the status which will be assign to this object
123
   * 0 is free, 1 is using
124
   */
125
  public void setStatus(int myStatus)
126
  {
127
    this.status = myStatus;
128
  }
129
  
130
  /**
131
   * get the db connetion age from the object
132
   */
133
  public long getAge()
134
  {
135
    return (System.currentTimeMillis() - createTime);
136
  }
137
  
138
 
139
  /**
140
   * Set a connection age to this object
141
   * @param myAge, the Age which will be assign to this object
142
   */
143
  public void setAge(long myAge)
144
  {
145
    this.age = myAge;
146
  }  
147
  
148
  /**
149
   * get the db connetion created time from the object
150
   */
151
  public long getCreateTime()
152
  {
153
    return createTime;
154
  }
155
  
156
  /**
157
   * Set a usage number to this object
158
   * @param myCreateTime, the create time which will be assign to this object
159
   */
160
  public void setCreateTime(long myCreateTime)
161
  {
162
    this.createTime = myCreateTime;
163
  }
164
  
165
  /**
166
   * get the how long db connetion usedfrom the object
167
   */
168
  public long getConnectionTime()
169
  {
170
    return connectionTime;
171
  }
172
  
173
 
174
  /**
175
   * Set a connection time to this object
176
   * It is accumulated
177
   * @param myConnectionTime, the connection time which will assign to
178
   * this object
179
   */
180
  public void setConnectionTime(long myConnectionTime)
181
  {
182
    this.connectionTime = this.connectionTime + myConnectionTime;
183
  }
184
  
185
    /**
186
   * get the how long db connetion usedfrom the object
187
   */
188
  public long getCheckOutTime()
189
  {
190
    return checkOutTime;
191
  }
192
  
193
 
194
  /**
195
   * Set check out time to this object
196
  
197
   * @param myCheckOutTime, the check out time which will assign to
198
   * this object
199
   */
200
  public void setCheckOutTime(long myCheckOutTime)
201
  {
202
    this.checkOutTime = myCheckOutTime;
203
  }
204
  
205
  /**
206
   * get the db connetion usage times from the object
207
   */
208
  public int getUsageCount()
209
  {
210
    return usageCount;
211
  }
212
   
213
 
214
  /**
215
   * Set a usage number to this object
216
   * @param myUsageCount, number of usage which will be assign to this object
217
   */
218
  public void setUsageCount(int myUsageCount)
219
  {
220
    this.usageCount = myUsageCount;
221
  }
222
  
223
  /**
224
   * Increase a usage number to this object
225
   * @param myUsageCount, number of usage which will be add to this object
226
   */
227
  public void increaseUsageCount(int myUsageCount)
228
  {
229
    this.usageCount = this.usageCount + myUsageCount;
230
  }  
231
  
232
  
233
  /**
234
   * get the db connetion waring message from the object
235
   */
236
  public SQLWarning getWarningMessage() throws SQLException
237
  {
238
    return conn.getWarnings();
239
  }
240
  
241
  /**
242
   * Set a warning message to this object
243
   * @param myWarningMessage, the waring which will be assign to this object
244
   */
245
  public void setWarningMessage(SQLWarning myWarningMessage)
246
  {
247
     this.warningMessage = myWarningMessage;
248
  }
249
  
250
  /**
251
   * Close a DBConnection object
252
   */
253
  public void close() throws SQLException
254
  {
255
    conn.close();
256
    tag = null;
257
    status = 0;
258
    age = 0;
259
    createTime = System.currentTimeMillis();
260
    connectionTime = 0;
261
    checkOutTime = 0;
262
    usageCount= 0;
263
    warningMessage = null;
264
  }
265
  
266
    /** 
267
   * Method to establish DBConnection 
268
   */
269
  public static Connection openConnection()
270
                  throws SQLException 
271
  {
272
    return openConnection(DBDriver, DBConnectedJDBC, userName, passWord);
273
  }//openDBConnection
274

    
275
  /** 
276
   * Method to establish a JDBC database connection 
277
   *
278
   * @param dbDriver the string representing the database driver
279
   * @param connection the string representing the database connectin parameters
280
   * @param user name of the user to use for database connection
281
   * @param password password for the user to use for database connection
282
   */
283
  private static Connection openConnection(String dbDriver, String connection,
284
                String user, String password)
285
                throws SQLException
286
 {
287
     // Load the Oracle JDBC driver
288
     try
289
     {
290
       Class.forName (dbDriver);
291
     }
292
     catch (ClassNotFoundException e)
293
     {
294
       MetaCatUtil.debugMessage("Error in DBConnectionPool "+e.getMessage(),30);
295
       return null;
296
     }
297
     // Connect to the database
298
     Connection connLocal = null;
299
     connLocal = DriverManager.getConnection( connection, user, password);
300
     return connLocal;
301
  }//OpenDBConnection
302
  
303
  /**
304
   * Method to create a PreparedStatement by sending a sql statement
305
   * @Param sql, the sql statement which will be sent to db
306
   */
307
  public PreparedStatement prepareStatement( String sql ) throws SQLException
308
  {
309
    return conn.prepareStatement(sql);
310
  }//prepareStatement
311
  
312

    
313
 
314
  
315
}//DBConnection class
(13-13/43)