Project

General

Profile

« Previous | Next » 

Revision 1217

Added by Jing Tao almost 22 years ago

Merge DBConnection branch to head.

View differences:

DBConnectionPool.java
78 78
  final static long CYCLETIMEOFDBCONNECTION = 
79 79
               Long.parseLong(MetaCatUtil.getOption("cycleTimeOfDBConnection"));
80 80
               
81
  //the number for trying to check out a connection in the pool in 
82
  //getDBConnection method
83
  final static int LIMIT = 2;
81 84
  
82 85
  final static int FREE = 0; //status of a connection
83 86
  final static int BUSY = 1; //statis of a connection
......
185 188
                                                throws SQLException
186 189
  {
187 190
    DBConnection db = null;
188
    MetaCatUtil.debugMessage("Try to checking out connection...", 10);    
189
    //try every DBConnection in the pool
190
  
191
      for (int i=0; i<connectionPool.size(); i++)
191
    int random = 0; //random number
192
    int index = 0; //index
193
    int size = 0; //size of connection pool
194
    MetaCatUtil.debugMessage("Try to checking out connection...", 45);
195
    size = connectionPool.size();
196
    MetaCatUtil.debugMessage("size of connection pool: "+size, 45);
197
    
198
     //try every DBConnection in the pool
199
    //every DBConnection will be try LIMITE times
200
    for (int j=0 ; j<LIMIT; j++)
201
    {
202
       //create a random number as the started index for connection pool
203
      //So that the connection ofindex of 0 wouldn't be a the heaviest user
204
      random = (new Double (Math.random()*100)).intValue();
205
      for (int i=0; i<size; i++)
192 206
      {
193
        db = (DBConnection) connectionPool.elementAt(i);
207
        index =(i+random)%size;
208
        db = (DBConnection) connectionPool.elementAt(index);
209
        MetaCatUtil.debugMessage("Index: "+index, 45);
210
        MetaCatUtil.debugMessage("Tag: "+db.getTag(), 45);
211
        MetaCatUtil.debugMessage("Status: "+db.getStatus(), 45);
194 212
        //check if the connection is free
195 213
        if (db.getStatus()==FREE)
196 214
        {
......
208 226
            db.setCheckOutMethodName(methodName);
209 227
            //debug message
210 228
            MetaCatUtil.debugMessage("The connection is checked out: "
211
                                       +db.getTag(), 20);
212
            MetaCatUtil.debugMessage("The age is "+db.getAge(), 25);
213
            MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 25);
229
                                       +db.getTag(), 35);
230
            MetaCatUtil.debugMessage("The method for checking is: " 
231
                                              +db.getCheckOutMethodName(), 35);
232
            MetaCatUtil.debugMessage("The age is "+db.getAge(), 40);
233
            MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 40);
214 234
            MetaCatUtil.debugMessage("The conection time it has: "
215
                                                +db.getConnectionTime(), 25);
235
                                                +db.getConnectionTime(), 40);
216 236
            //set check out time
217 237
            db.setCheckOutTime(System.currentTimeMillis());
218 238
            //check it out
......
223 243
            //close this DBConnection
224 244
            db.close();
225 245
            //remove it form connection pool
226
            connectionPool.remove(i);
246
            connectionPool.remove(index);
227 247
            //insert a new DBConnection to same palace
228 248
            db = new DBConnection();
229
            connectionPool.insertElementAt(db, i);
249
            connectionPool.insertElementAt(db, index);
230 250
          }//else
231 251
        }//if
232 252
      }//for
253
    }//for
233 254
    
234
    
235 255
    //if couldn't get a connection, we should increase DBConnection pool
236 256
    //if the connection pool size is less than maximum connection number
237
    int poolSize = connectionPool.size(); 
238
    if ( poolSize < MAXIMUMCONNECTIONNUMBER )
257
   
258
    if ( size < MAXIMUMCONNECTIONNUMBER )
239 259
    {
240
       if ((poolSize+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
260
       if ((size+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
241 261
       { 
242 262
         //if we can create INCREASECONNECTIONNUMBER of new DBConnection
243 263
         //add to connection pool
......
251 271
       {
252 272
         //There is no enough room to increase INCREASECONNECTIONNUMBER 
253 273
         //we create new DBCoonection to Maximum connection number
254
         for (int i= poolSize+1; i<= MAXIMUMCONNECTIONNUMBER; i++)
274
         for (int i= size+1; i<= MAXIMUMCONNECTIONNUMBER; i++)
255 275
         {
256 276
           DBConnection dbConn = new DBConnection();
257 277
           connectionPool.add(dbConn);
......
261 281
    }//if
262 282
    else
263 283
    {
264
      throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER + 
284
      /*throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER + 
265 285
                            " open db connections is reached." +
266 286
                            " New db connection to MetaCat" +
267
                            " cannot be established.");
268
      
269
    }//else
270
    
271
    //recursive to get new connection    
272
    return getDBConnection();
273
  }//getDBConnection
274

  
275
  /**
276
   * Method to get a DBConnection in connection pool
277
   * 1) try to get a DBConnection from DBConnection pool
278
   * 2) if 1) failed, then check the size of pool. If the size reach the
279
   *    maximum number of connection, throw a exception: couldn't get one
280
   * 3) If the size is less than the maximum number of connectio, create some
281
   *    new connections and recursive get one
282
   */
283
  public static synchronized DBConnection getDBConnection() throws SQLException
284
  {
285
    DBConnection db = null;
286
    MetaCatUtil.debugMessage("Try to checking out connection...", 10);    
287
    //try every DBConnection in the pool
288
  
289
      for (int i=0; i<connectionPool.size(); i++)
290
      {
291
        db = (DBConnection) connectionPool.elementAt(i);
292
        //check if the connection is free
293
        if (db.getStatus()==FREE)
294
        {
295
          //If this connection is good, return this DBConnection
296
          if (validateDBConnection(db))
297
          {
298
            
299
            //set this DBConnection status
300
            db.setStatus(BUSY);
301
            //increase checkout serial number
302
            db.increaseCheckOutSerialNumber(1);
303
            //increase one usageCount
304
            db.increaseUsageCount(1);
305
            //debug message
306
            MetaCatUtil.debugMessage("The connection is checked out: "
307
                                       +db.getTag(), 25);
308
            MetaCatUtil.debugMessage("The age is "+db.getAge(), 25);
309
            MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 25);
310
            MetaCatUtil.debugMessage("The conection time it has: "
311
                                                +db.getConnectionTime(), 25);
312
            //set check out time
313
            db.setCheckOutTime(System.currentTimeMillis());
314
            //check it out
315
            return db;
316
          }//if
317
          else//The DBConnection has some problem
318
          {
319
            //close this DBConnection
320
            db.close();
321
            //remove it form connection pool
322
            connectionPool.remove(i);
323
            //insert a new DBConnection to same palace
324
            db = new DBConnection();
325
            connectionPool.insertElementAt(db, i);
326
          }//else
327
        }//if
328
      }//for
329
    
330
    
331
    //if couldn't get a connection, we should increase DBConnection pool
332
    //if the connection pool size is less than maximum connection number
333
    int poolSize = connectionPool.size(); 
334
    if ( poolSize < MAXIMUMCONNECTIONNUMBER )
335
    {
336
       if ((poolSize+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
337
       { 
338
         //if we can create INCREASECONNECTIONNUMBER of new DBConnection
339
         //add to connection pool
340
         for ( int i=0; i<INCREASECONNECTIONNUMBER; i++)
341
         {
342
           DBConnection dbConn = new DBConnection();
343
           connectionPool.add(dbConn);
344
         }//for
345
       }//if
346
       else
347
       {
348
         //There is no enough room to increase INCREASECONNECTIONNUMBER 
349
         //we create new DBCoonection to Maximum connection number
350
         for (int i= poolSize+1; i<= MAXIMUMCONNECTIONNUMBER; i++)
351
         {
352
           DBConnection dbConn = new DBConnection();
353
           connectionPool.add(dbConn);
354
         }//for
355
       }//else
356
   
357
    }//if
358
    else
359
    {
360
      throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER + 
287
                            " cannot be established.");*/
288
       MetaCatUtil.debugMessage("The maximum of " +MAXIMUMCONNECTIONNUMBER + 
361 289
                            " open db connections is reached." +
362 290
                            " New db connection to MetaCat" +
363
                            " cannot be established.");
291
                            " cannot be established.", 1);
292
       return null;
293
       //if couldn't get a connection, sleep 20 seconds and try again.
294
       /*try
295
       {
296
         System.out.println("sleep");
297
         Thread.sleep(2000);
298
       }
299
       catch (Exception e)
300
       {
301
       }*/
302
         
364 303
      
365 304
    }//else
366 305
    
367 306
    //recursive to get new connection    
368
    return getDBConnection();
307
    return getDBConnection(methodName); 
369 308
  }//getDBConnection
309

  
310
 
370 311
  
371 312
  
372
  
373 313
  /** 
374 314
   * Method to check if a db connection works fine or not
375 315
   * Check points include:
......
382 322
   */
383 323
  private static boolean validateDBConnection (DBConnection dbConn)
384 324
  {
385
    Connection conn = null;
386 325
    
326
    
387 327
    //Check if the DBConnection usageCount if it is too many
388 328
    if (dbConn.getUsageCount() >= MAXIMUMUSAGENUMBER )
389 329
    {
......
408 348
    }
409 349
    
410 350
    //Try to run a simple query
411
    conn = dbConn.getConnections();
412 351
    try
413 352
    {
414 353
      long startTime=System.currentTimeMillis();
415
      conn.getMetaData();
354
      DatabaseMetaData metaData = dbConn.getMetaData();
416 355
      long stopTime=System.currentTimeMillis();
417 356
      //increase one usagecount
418 357
      dbConn.increaseUsageCount(1);
......
446 385
    {
447 386
      MetaCatUtil.debugMessage("Couldn't find a DBConnection in the pool"
448 387
                                  +" which have same tag to the returned"
449
                                  +" DBConnetion object");
388
                                  +" DBConnetion object", 30);
450 389
      return;
451 390
                                  
452 391
    }//if
......
470 409
                          
471 410
        //set check out time to 0
472 411
        dbConn.setCheckOutTime(0);
473
      
412
        
474 413
        MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+" checked in.",
475
                                                                        20);
414
                                                                        35);
415
         MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+"'s status: "
416
                                                    +dbConn.getStatus(), 35);
417
                                                                       
476 418
      }//if
419
      else
420
      {
421
        MetaCatUtil.debugMessage("This DBConnection couldn't return", 30);
422
      }//else
477 423
    }//else
478 424
   
479 425
 
......
489 435
  {
490 436
    int index = -1;
491 437
    String info = null;
438
    //if conn is null return -1 too
439
    if (conn==null)
440
    {
441
      return -1;
442
    }
492 443
    //get tag of this returned DBConnection
493 444
    info = conn.getTag();
494 445
    //if the tag is null or empty, -1 will be returned
......
615 566
    }//while
616 567
  }//run
617 568
  
569
  /**
570
   * Method to get the number of free DBConnection in DBConnection pool
571
   */
572
  public int getFreeDBConnectionNumber()
573
  {
574
    int numberOfFreeDBConnetion = 0; //return number
575
    DBConnection db = null; //single DBconnection
576
    int poolSize = 0; //size of connection pool
577
    //get the size of DBConnection pool
578
    poolSize = connectionPool.size();
579
    //Check every DBConnection in the pool
580
    for ( int i=0; i<poolSize; i++)
581
    {
582
      
583
      db = (DBConnection) connectionPool.elementAt(i);
584
      //check the status of db. If it is free, count it
585
      if (db.getStatus() == FREE)
586
      {
587
        numberOfFreeDBConnetion++;
588
      }//if
589
    }//for
590
    //return the count result
591
    return numberOfFreeDBConnetion;
592
  }//getFreeDBConnectionNumber
593
      
594
   /**
595
   * Method to print out the method name which have busy DBconnection
596
   */
597
  public void printMethodNameHavingBusyDBConnection()
598
  {
599
    
600
    DBConnection db = null; //single DBconnection
601
    int poolSize = 0; //size of connection pool
602
    //get the size of DBConnection pool
603
    poolSize = connectionPool.size();
604
    //Check every DBConnection in the pool
605
    for ( int i=0; i<poolSize; i++)
606
    {
607
      
608
      db = (DBConnection) connectionPool.elementAt(i);
609
      //check the status of db. If it is free, count it
610
      if (db.getStatus() == BUSY)
611
      {
612
        MetaCatUtil.debugMessage("This method having a busy DBConnection: "
613
                                    +db.getCheckOutMethodName(), 30);
614
        MetaCatUtil.debugMessage("The busy DBConnection tag is: "
615
                                    +db.getTag(), 30);
616
      }//if
617
    }//for
618
  
619
  }//printMethodNameHavingBusyDBConnection
620
  
618 621
}//DBConnectionPool

Also available in: Unified diff