Project

General

Profile

« Previous | Next » 

Revision 1217

Added by Jing Tao almost 22 years ago

Merge DBConnection branch to head.

View differences:

MetaCatUtil.java
50 50
  private static String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
51 51
  private static boolean debug = false;
52 52

  
53
  private Hashtable connectionPool = new Hashtable();
53
  //private Hashtable connectionPool = new Hashtable();
54 54

  
55 55
  /** 
56 56
   * Determine our db adapter class and create an instance of that class
......
63 63
    }
64 64
  }
65 65

  
66
// CONSTRUCTORS NOT NEEDED
67
//  /**
68
//   * Construct an instance of the utility class
69
//   */
70
//  public MetaCatUtil() {
71
//
72
//    options = (PropertyResourceBundle)
73
//          PropertyResourceBundle.getBundle(propertiesFile);
74
//  }
75
//  
76
//  /**
77
//   * This constructor allows the usage of a different properties file
78
//   * @param propFile the properties file that you wish to use.
79
//   */
80
//  public MetaCatUtil(String propFile)  {
81
//
82
//    propertiesFile = propFile;
83
//  }
84 66

  
67

  
85 68
  /**
86 69
   * Instantiate a class using the name of the class at runtime
87 70
   *
......
132 115
    return value;
133 116
  }
134 117

  
135
  /** 
136
   * Utility method to establish a JDBC database connection using connection
137
   * info from the properties file
138
   */
139
  public Connection openDBConnection()
140
                throws SQLException, ClassNotFoundException {
141
    return openDBConnection(getOption("dbDriver"), getOption("defaultDB"),
142
                     getOption("user"), getOption("password"));
143
  }
144

  
145
  /** 
146
   * Utility method to establish a JDBC database connection 
147
   *
148
   * @param dbDriver the string representing the database driver
149
   * @param connection the string representing the database connectin parameters
150
   * @param user name of the user to use for database connection
151
   * @param password password for the user to use for database connection
152
   */
153
  public static Connection openDBConnection(String dbDriver, String connection,
154
                String user, String password)
155
                throws SQLException, ClassNotFoundException {
156

  
157
     // Load the Oracle JDBC driver
158
     Class.forName (dbDriver);
159

  
160
     debugMessage("Opening connection: " + connection);
161
     // Connect to the database
162
     Connection conn = DriverManager.getConnection( connection, user, password);
163
     return conn;
164
  }
165

  
166
  /* Utility method to create and return a pool of Connection objects */
167
  public Hashtable getConnectionPool()
168
                throws SQLException, ClassNotFoundException {
169

  
170
    int initConn = (new Integer(getOption("initialConnections"))).intValue();
171
    
172
    for ( int i = 0; i < initConn; i++ ) {
173
        connectionPool.put( openDBConnection(), Boolean.FALSE );
174
    }    
175
    
176
    return connectionPool;
177
  }
118
 
178 119
  
179
  /* Utility method to get a unused Connection object from the pool */
180
  public Connection getConnection()
181
                throws SQLException, ClassNotFoundException, Exception 
182
  {
183

  
184
    Connection conn = null;
185
    Enumeration connections = connectionPool.keys();
186
    
187
    synchronized (connectionPool) {
188
      while (connections.hasMoreElements()) {
189

  
190
        conn = (Connection)connections.nextElement();
191
        Boolean b = (Boolean)connectionPool.get(conn);
192

  
193
        if (conn == null || conn.isClosed()) {
194
            // closed connection; replace it
195
            conn = openDBConnection();
196
            // update the pool to show this one taken
197
            connectionPool.put(conn, Boolean.TRUE);  
198
            
199
            return conn;
200
        } else if (b == Boolean.FALSE) {
201
            // unused connection; test if it works and get it
202
            try {
203
                conn.setAutoCommit(true);
204
            } catch (SQLException e) {
205
                // problem with the connection, replace it
206
                conn = openDBConnection();
207
            }    
208
            // update the pool to show this one taken
209
            connectionPool.put(conn, Boolean.TRUE);    
210

  
211
            return conn;
212
        }    
213
      }  
214
    }   
215
    
216
    // If we get here, there no free connections;
217
    // we need to make more
218
    int incConn = (new Integer(getOption("incrementConnections"))).intValue();
219
    int maxConn = (new Integer(getOption("maximumConnections"))).intValue();
220
    int k = connectionPool.size();
221
    if ( k >= maxConn ) {
222
        throw new Exception("The maximum of " + maxConn + 
223
                            " open db connections is reached." +
224
                            " New db connection to MetaCat" +
225
                            " cannot be established.");
226
    }    
227

  
228
    // decide how many new connections to open; no more than the maximum
229
    if ( incConn > maxConn - k ) { incConn = maxConn - k; }
230
    
231
    // get them
232
    for ( int i = 0; i < incConn; i++ ) {
233
        connectionPool.put( openDBConnection(), Boolean.FALSE );
234
    }
235

  
236
    // Recurse to get one of the new connections
237
    return getConnection();
238
  }
239 120
  
240
  /* Utility method to give the connection back to the pool */
241
  public void returnConnection (Connection returned) {
242
    
243
    Connection conn;
244
    Enumeration connections = connectionPool.keys();
245
    
246
    try {
247
      if ( (returned != null) && !(returned.isClosed()) ) {
248
        while (connections.hasMoreElements()) {
249
          conn = (Connection)connections.nextElement();
250
          if ( conn == returned ) {
251
            connectionPool.put( conn, Boolean.FALSE );
252
            break;
253
          }    
254
        }    
255
      }
256
    } catch (SQLException e) {}    
257
  }
258

  
259
  /* Return the size of the pool */
260
  public int getPoolSize() {
261
    return connectionPool.size();
262
  }
263
  
264
  /* Utility method to close all the connection from the pool */
265
  public void closeConnections() {
266
    
267
    Connection conn;
268
    Enumeration connections = connectionPool.keys();
269
    
270
    while (connections.hasMoreElements()) {
271
        conn = (Connection)connections.nextElement();
272
        try {
273
            if ( (conn != null) && !(conn.isClosed()) ) {
274
              conn.close();
275
            }    
276
        } catch (SQLException e) {}    
277
    }
278
  }
279
  
280
  
281 121
  /** Utility method to convert a file handle into a URL */
282 122
  public static URL fileToURL(File file)
283 123
  {

Also available in: Unified diff