Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class represent a DBConnection pool. Another user can use the
4
 *    object to initial a connection pool, get db connection or return it. 
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: 2003-04-16 22:07:37 -0700 (Wed, 16 Apr 2003) $'
12
 * '$Revision: 1558 $'
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.util.Vector;
33
import java.lang.*;
34
import java.sql.*;
35
import java.util.Stack;
36
import java.util.Hashtable;
37
import java.util.Enumeration;
38

    
39
/** 
40
 * A class represent a DBConnection pool. Another user can use the
41
 * object to initial a connection pool, get db connection or return it.
42
 * This a singleton class, this means only one instance of this class could
43
 * be in the program at one time. 
44
 */
45
public class DBConnectionPool implements Runnable
46
{
47

    
48
  //static attributes
49
  private static DBConnectionPool instance;
50
  private static Vector connectionPool;
51
  private static Thread runner;
52
  
53
  //maximum connection number in the connection pool
54
  final static int MAXIMUMCONNECTIONNUMBER=
55
                  Integer.parseInt(MetaCatUtil.getOption("maximumConnections"));
56
  
57
  //inintila connection number int the connection pool
58
  final static int INITIALCONNECTIONNUMBER=
59
                  Integer.parseInt(MetaCatUtil.getOption("initialConnections"));
60
 
61
  //the number to increase connection pool size
62
  final static int INCREASECONNECTIONNUMBER=
63
                Integer.parseInt(MetaCatUtil.getOption("incrementConnections"));
64
                
65
  //maximum age for a connection (in milli seconds)
66
  final static long MAXIMUMAGE =
67
                  Long.parseLong(MetaCatUtil.getOption("maximumConnectionAge"));
68
  //maximum connection time for a connection ( in milli second)
69
  final static long MAXIMUMCONNECTIONTIME =
70
                 Long.parseLong(MetaCatUtil.getOption("maximumConnectionTime"));
71
  //maximum number for using a connection.
72
  final static int MAXIMUMUSAGENUMBER =
73
                  Integer.parseInt(MetaCatUtil.getOption("maximumUsageNumber"));
74
  //the parameter if run dbconncestionrecyclethread or not
75
  final static String DBCONNECTIONRECYCLETHREAD =
76
                          MetaCatUtil.getOption("runDBConnectionRecycleThread");
77
  //the cycle time of connection recycle action
78
  final static long CYCLETIMEOFDBCONNECTION = 
79
               Long.parseLong(MetaCatUtil.getOption("cycleTimeOfDBConnection"));
80
               
81
  //the number for trying to check out a connection in the pool in 
82
  //getDBConnection method
83
  final static int LIMIT = 2;
84
  
85
  final static int FREE = 0; //status of a connection
86
  final static int BUSY = 1; //statis of a connection
87
  /**
88
   * Returns the single instance, creating one if it's the
89
   * first time this method is called.
90
   */
91
  public static synchronized DBConnectionPool getInstance()
92
                                 throws SQLException 
93
  {
94
    MetaCatUtil.debugMessage
95
                      ("MaximumConnectionNumber: "+MAXIMUMCONNECTIONNUMBER, 49);
96
    MetaCatUtil.debugMessage
97
                     ("Intial connection number: "+INITIALCONNECTIONNUMBER, 49);
98
    MetaCatUtil.debugMessage
99
                 ("Increated connection Number: "+INCREASECONNECTIONNUMBER, 49);
100
    MetaCatUtil.debugMessage
101
                  ("Maximum connection age: "+MAXIMUMAGE, 49);
102
    MetaCatUtil.debugMessage
103
                  ("Maximum connection time: "+MAXIMUMCONNECTIONTIME, 49);
104
    MetaCatUtil.debugMessage
105
                  ("Maximum usage count: "+MAXIMUMUSAGENUMBER, 49);
106
    MetaCatUtil.debugMessage
107
             ("Running recycle thread or not: "+DBCONNECTIONRECYCLETHREAD, 49);
108
    MetaCatUtil.debugMessage
109
             ("Cycle time of recycle: "+CYCLETIMEOFDBCONNECTION, 49); 
110
    if (instance == null) 
111
    {
112
      instance = new DBConnectionPool();
113
    }
114
    return instance;
115
  }//getInstance
116

    
117

    
118
  /**
119
   * This is a private constructor since it is singleton
120
   */
121
   
122
  private DBConnectionPool()  throws SQLException 
123
  {
124
    connectionPool = new Vector();
125
    initialDBConnectionPool();
126
    //running the thread to recycle DBConnection
127
    if (DBCONNECTIONRECYCLETHREAD.equals("on"))
128
    {
129
      runner = new Thread(this);
130
      runner.start();
131
    }
132
  }//DBConnection
133

    
134
  /**
135
   * Method to get the size of DBConnectionPool
136
   */
137
  public int getSizeOfDBConnectionPool()
138
  {
139
    return connectionPool.size();
140
  }
141
  
142
  
143
  /**
144
   * Method to initial a pool of DBConnection objects 
145
   */
146
  private void initialDBConnectionPool() throws SQLException 
147
  {
148

    
149
    DBConnection dbConn = null;
150
    
151
    for ( int i = 0; i < INITIALCONNECTIONNUMBER; i++ ) 
152
    {
153
      //create a new object of DBConnection
154
      //this DBConnection object has a new connection in it
155
      //it automatically generate the createtime and tag
156
      dbConn = new DBConnection();
157
      //put DBConnection into vetor
158
      connectionPool.add(dbConn);
159
    }    
160
    
161
  
162
  }//initialDBConnectionPool
163
  
164
  /**
165
   * Method to get Connection object (Not DBConnection)
166
   */
167
  /*public static Connection getConnection() throws SQLException
168
  {
169
    DBConnection dbConn = null;
170
    //get a DBConnection
171
    dbConn = getDBConnection();
172
    //get connection object in DBConnection object
173
    //The following getConnections method is in DBConnection class
174
    return dbConn.getConnections();
175
  }*/
176
  
177
 
178
  /**
179
   * Method to get a DBConnection in connection pool
180
   * 1) try to get a DBConnection from DBConnection pool
181
   * 2) if 1) failed, then check the size of pool. If the size reach the
182
   *    maximum number of connection, throw a exception: couldn't get one
183
   * 3) If the size is less than the maximum number of connectio, create some
184
   *    new connections and recursive get one
185
   * @param methodName, the name of method which will check connection out
186
   */
187
  public static synchronized DBConnection getDBConnection(String methodName) 
188
                                                throws SQLException
189
  {
190
    DBConnection db = null;
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...", 55);
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++)
206
      {
207
        index =(i+random)%size;
208
        db = (DBConnection) connectionPool.elementAt(index);
209
        MetaCatUtil.debugMessage("Index: "+index, 55);
210
        MetaCatUtil.debugMessage("Tag: "+db.getTag(), 55);
211
        MetaCatUtil.debugMessage("Status: "+db.getStatus(), 55);
212
        //check if the connection is free
213
        if (db.getStatus()==FREE)
214
        {
215
          //If this connection is good, return this DBConnection
216
          if (validateDBConnection(db))
217
          {
218
            
219
            //set this DBConnection status
220
            db.setStatus(BUSY);
221
            //increase checkout serial number
222
            db.increaseCheckOutSerialNumber(1);
223
            //increase one usageCount
224
            db.increaseUsageCount(1);
225
            //set method name to DBConnection
226
            db.setCheckOutMethodName(methodName);
227
            db.setAutoCommit(true);
228
            //debug message
229
            MetaCatUtil.debugMessage("The connection is checked out: "
230
                                       +db.getTag(), 55);
231
            MetaCatUtil.debugMessage("The method for checking is: " 
232
                                              +db.getCheckOutMethodName(), 45);
233
            MetaCatUtil.debugMessage("The age is "+db.getAge(), 55);
234
            MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 55);
235
            MetaCatUtil.debugMessage("The conection time it has: "
236
                                                +db.getConnectionTime(), 55);
237
            //set check out time
238
            db.setCheckOutTime(System.currentTimeMillis());
239
            //check it out
240
            return db;
241
          }//if
242
          else//The DBConnection has some problem
243
          {
244
            //close this DBConnection
245
            db.close();
246
            //remove it form connection pool
247
            connectionPool.remove(index);
248
            //insert a new DBConnection to same palace
249
            db = new DBConnection();
250
            connectionPool.insertElementAt(db, index);
251
          }//else
252
        }//if
253
      }//for
254
    }//for
255
    
256
    //if couldn't get a connection, we should increase DBConnection pool
257
    //if the connection pool size is less than maximum connection number
258
   
259
    if ( size < MAXIMUMCONNECTIONNUMBER )
260
    {
261
       if ((size+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
262
       { 
263
         //if we can create INCREASECONNECTIONNUMBER of new DBConnection
264
         //add to connection pool
265
         for ( int i=0; i<INCREASECONNECTIONNUMBER; i++)
266
         {
267
           DBConnection dbConn = new DBConnection();
268
           connectionPool.add(dbConn);
269
         }//for
270
       }//if
271
       else
272
       {
273
         //There is no enough room to increase INCREASECONNECTIONNUMBER 
274
         //we create new DBCoonection to Maximum connection number
275
         for (int i= size+1; i<= MAXIMUMCONNECTIONNUMBER; i++)
276
         {
277
           DBConnection dbConn = new DBConnection();
278
           connectionPool.add(dbConn);
279
         }//for
280
       }//else
281
   
282
    }//if
283
    else
284
    {
285
      /*throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER + 
286
                            " open db connections is reached." +
287
                            " New db connection to MetaCat" +
288
                            " cannot be established.");*/
289
       MetaCatUtil.debugMessage("The maximum of " +MAXIMUMCONNECTIONNUMBER + 
290
                            " open db connections is reached." +
291
                            " New db connection to MetaCat" +
292
                            " cannot be established.", 1);
293
       return null;
294
       //if couldn't get a connection, sleep 20 seconds and try again.
295
       /*try
296
       {
297
         System.out.println("sleep");
298
         Thread.sleep(2000);
299
       }
300
       catch (Exception e)
301
       {
302
       }*/
303
         
304
      
305
    }//else
306
    
307
    //recursive to get new connection    
308
    return getDBConnection(methodName); 
309
  }//getDBConnection
310

    
311
 
312
  
313
  
314
  /** 
315
   * Method to check if a db connection works fine or not
316
   * Check points include:
317
   * 1. check the usageCount if it is too many
318
   * 2. check the dbconne age if it is too old
319
   * 3. check the connection time if it is too long
320
   * 4. run simple sql query
321
   *
322
   * @param dbConn, the DBConnection object need to check
323
   */
324
  private static boolean validateDBConnection (DBConnection dbConn)
325
  {
326
    
327
    
328
    //Check if the DBConnection usageCount if it is too many
329
    if (dbConn.getUsageCount() >= MAXIMUMUSAGENUMBER )
330
    {
331
      MetaCatUtil.debugMessage("Connection usageCount is too many: "+
332
      dbConn.getUsageCount(), 40);
333
      return false;
334
    }
335
    
336
    //Check if the DBConnection has too much connection time
337
    if (dbConn.getConnectionTime() >= MAXIMUMCONNECTIONTIME)
338
    {
339
      MetaCatUtil.debugMessage("Connection has too much connection time: "+
340
      dbConn.getConnectionTime(), 40);
341
      return false;
342
    }
343
    
344
    //Check if the DBConnection is too old
345
    if (dbConn.getAge() >=MAXIMUMAGE)
346
    {
347
      MetaCatUtil.debugMessage("Connection is too old: "+dbConn.getAge(), 40);
348
      return false;
349
    }
350
    
351
    //Try to run a simple query
352
    try
353
    {
354
      long startTime=System.currentTimeMillis();
355
      DatabaseMetaData metaData = dbConn.getMetaData();
356
      long stopTime=System.currentTimeMillis();
357
      //increase one usagecount
358
      dbConn.increaseUsageCount(1);
359
      //increase connection time
360
      dbConn.setConnectionTime(stopTime-startTime);
361
  
362
    }
363
    catch (Exception e)
364
    {
365
      MetaCatUtil.debugMessage("Error in validateDBConnection: "
366
                                +e.getMessage(), 30);
367
      return false;
368
    }
369
    
370
    return true;
371
    
372
  }//validateDBConnection()
373
  
374
  /**
375
   * Method to return a connection to DBConnection pool.
376
   * @param conn, the Connection object need to check in
377
   */
378
  public static synchronized void returnDBConnection(DBConnection conn, 
379
                                                              int serialNumber)
380
  {
381
    int index = -1;
382
    DBConnection dbConn = null;
383
  
384
    index = getIndexOfPoolForConnection(conn);
385
    if ( index ==-1 )
386
    {
387
      MetaCatUtil.debugMessage("Couldn't find a DBConnection in the pool"
388
                                  +" which have same tag to the returned"
389
                                  +" DBConnetion object", 30);
390
      return;
391
                                  
392
    }//if
393
    else
394
    {
395
      //check the paramter - serialNumber which will be keep in calling method
396
      //if it is as same as the object's checkoutserial number.
397
      //if it is same return it. If it is not same, maybe the connection already
398
      // was returned ealier.
399
      MetaCatUtil.debugMessage("serial number in Connection: "
400
                                      +conn.getCheckOutSerialNumber(), 55);
401
      MetaCatUtil.debugMessage("serial number in local: "+serialNumber, 55);
402
      if (conn.getCheckOutSerialNumber() == serialNumber)
403
      {
404
        dbConn = (DBConnection) connectionPool.elementAt(index);
405
        //set status to free
406
        dbConn.setStatus(FREE);
407
        //count connection time
408
        dbConn.setConnectionTime
409
                          (System.currentTimeMillis()-dbConn.getCheckOutTime());
410
                          
411
        //set check out time to 0
412
        dbConn.setCheckOutTime(0);
413
        
414
        MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+" checked in.",
415
                                                                        55);
416
        MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+"'s status: "
417
                                                    +dbConn.getStatus(), 55);
418
                                                                       
419
      }//if
420
      else
421
      {
422
        MetaCatUtil.debugMessage("This DBConnection couldn't return", 30);
423
      }//else
424
    }//else
425
   
426
 
427
  }//returnConnection
428
  
429
  /**
430
   * Given a returned DBConnection, try to find the index of DBConnection object
431
   * in dbConnection pool by comparing DBConnection' tag and conn.toString.
432
   * If couldn't find , -1 will be returned.
433
   * @param conn, the connection need to be found
434
   */
435
  private static synchronized int getIndexOfPoolForConnection(DBConnection conn)
436
  {
437
    int index = -1;
438
    String info = null;
439
    //if conn is null return -1 too
440
    if (conn==null)
441
    {
442
      return -1;
443
    }
444
    //get tag of this returned DBConnection
445
    info = conn.getTag();
446
    //if the tag is null or empty, -1 will be returned
447
    if (info==null || info.equals(""))
448
    {
449
      return index;
450
    }
451
    //compare this info to the tag of every DBConnection in the pool
452
    for ( int i=0; i< connectionPool.size(); i++)
453
    {
454
      DBConnection dbConn = (DBConnection) connectionPool.elementAt(i);
455
      if (info.equals(dbConn.getTag()))
456
      {
457
        index = i;
458
        break;
459
      }//if
460
    }//for
461
    
462
    return index;
463
  }//getIndexOfPoolForConnection  
464
  
465
  /**
466
   * Method to shut down all connections
467
   */
468
  public static void release()
469
  {
470
    
471
    //shut down the backgroud recycle thread
472
    if (DBCONNECTIONRECYCLETHREAD.equals("on"))
473
    {
474
      runner.interrupt();
475
    }
476
    //cose every dbconnection in the pool
477
    synchronized(connectionPool)
478
    {
479
      for (int i=0;i<connectionPool.size();i++)
480
      {
481
        try
482
        {
483
          DBConnection dbConn= (DBConnection) connectionPool.elementAt(i);
484
          dbConn.close();
485
        }//try
486
        catch (SQLException e)
487
        {
488
          MetaCatUtil.debugMessage("Error in release connection: "
489
                                            +e.getMessage(), 10);
490
        }//catch
491
      }//for
492
    }//synchronized
493
  }//release()
494
  
495
  /**
496
   * periodically to recycle the connection
497
   */
498
  public void run()
499
  {
500
    DBConnection dbConn = null;
501
    //keep the thread running
502
    while (true)
503
    {
504
      //check every dbconnection in the pool
505
      synchronized(connectionPool)
506
      {
507
        for (int i=0; i<connectionPool.size(); i++)
508
        {
509
          dbConn = (DBConnection) connectionPool.elementAt(i);
510
          
511
          //if a DBConnection conncectioning time for one check out is greater 
512
          //than 30000 milliseconds print it out
513
          if (dbConn.getStatus()==BUSY && 
514
            (System.currentTimeMillis()-dbConn.getCheckOutTime())>=30000)
515
          {
516
            MetaCatUtil.debugMessage("This DBConnection is checked out for: "
517
            +(System.currentTimeMillis()-dbConn.getCheckOutTime())/1000
518
            +" secs" , 1);
519
            MetaCatUtil.debugMessage(dbConn.getTag(), 1);
520
            MetaCatUtil.debugMessage("method: "
521
                                          +dbConn.getCheckOutMethodName(), 10);
522
            
523
          }
524
          
525
          //check the validation of free connection in the pool
526
          if (dbConn.getStatus() == FREE)
527
          {
528
            try
529
            {
530
              //try to print out the warning message for every connection
531
              if (dbConn.getWarningMessage()!=null)
532
              {
533
                MetaCatUtil.debugMessage("Warning for connection "
534
                  +dbConn.getTag()+" : "+ dbConn.getWarningMessage(), 10);
535
              }
536
              //check if it is valiate, if not create new one and replace old one
537
              if (!validateDBConnection(dbConn))
538
              {
539
                MetaCatUtil.debugMessage("Recyle it: "+ dbConn.getTag(), 40);
540
                //close this DBConnection
541
                dbConn.close();
542
                //remove it form connection pool
543
                connectionPool.remove(i);
544
                //insert a new DBConnection to same palace
545
                dbConn = new DBConnection();
546
                connectionPool.insertElementAt(dbConn, i);
547
               }//if
548
            }//try
549
            catch (SQLException e)
550
            {
551
              MetaCatUtil.debugMessage("Error in DBConnectionPool.run: "
552
                                              +e.getMessage(), 20);
553
            }//catch
554
          }//if
555
        }//for
556
      }//synchronize   
557
      //Thread sleep 
558
      try
559
      {
560
        Thread.sleep(CYCLETIMEOFDBCONNECTION);
561
      }
562
      catch (Exception e)
563
      {
564
        MetaCatUtil.debugMessage("Error in DBConnectionPool.run: "
565
                                              +e.getMessage(), 20);
566
      }
567
    }//while
568
  }//run
569
  
570
  /**
571
   * Method to get the number of free DBConnection in DBConnection pool
572
   */
573
  public static synchronized int getFreeDBConnectionNumber()
574
  {
575
    int numberOfFreeDBConnetion = 0; //return number
576
    DBConnection db = null; //single DBconnection
577
    int poolSize = 0; //size of connection pool
578
    //get the size of DBConnection pool
579
    poolSize = connectionPool.size();
580
    //Check every DBConnection in the pool
581
    for ( int i=0; i<poolSize; i++)
582
    {
583
      
584
      db = (DBConnection) connectionPool.elementAt(i);
585
      //check the status of db. If it is free, count it
586
      if (db.getStatus() == FREE)
587
      {
588
        numberOfFreeDBConnetion++;
589
      }//if
590
    }//for
591
    //return the count result
592
    return numberOfFreeDBConnetion;
593
  }//getFreeDBConnectionNumber
594
      
595
   /**
596
   * Method to print out the method name which have busy DBconnection
597
   */
598
  public void printMethodNameHavingBusyDBConnection()
599
  {
600
    
601
    DBConnection db = null; //single DBconnection
602
    int poolSize = 0; //size of connection pool
603
    //get the size of DBConnection pool
604
    poolSize = connectionPool.size();
605
    //Check every DBConnection in the pool
606
    for ( int i=0; i<poolSize; i++)
607
    {
608
      
609
      db = (DBConnection) connectionPool.elementAt(i);
610
      //check the status of db. If it is free, count it
611
      if (db.getStatus() == BUSY)
612
      {
613
        MetaCatUtil.debugMessage("This method having a busy DBConnection: "
614
                                    +db.getCheckOutMethodName(), 30);
615
        MetaCatUtil.debugMessage("The busy DBConnection tag is: "
616
                                    +db.getTag(), 30);
617
      }//if
618
    }//for
619
  
620
  }//printMethodNameHavingBusyDBConnection
621
  
622
  /**
623
   * Method to decrease dbconnection pool size when all dbconnections are idle
624
   * If all connections are free and connection pool size greater than 
625
   * initial value, shrink connection pool size to intital value
626
   */
627
  public static synchronized boolean shrinkConnectionPoolSize() 
628
  {
629
     int connectionPoolSize = 0; //store the number of dbconnection pool size
630
     int freeConnectionSize = 0; //store the number of free dbconnection in pool
631
     int difference = 0; // store the difference number between connection size
632
                         // and free connection
633
     boolean hasException = false; //to check if has a exception happend
634
     boolean result = false; //result
635
     DBConnection conn = null; // the dbconnection
636
     connectionPoolSize = connectionPool.size();
637
     freeConnectionSize = getFreeDBConnectionNumber();
638
     MetaCatUtil.debugMessage("Connection pool size: " +connectionPoolSize, 3);
639
     MetaCatUtil.debugMessage("Free Connection number: "+freeConnectionSize, 3);
640
     difference = connectionPoolSize - freeConnectionSize;
641
     
642
     //If all connections are free and connection pool size greater than 
643
     //initial value, shrink connection pool size to intital value
644
     if (difference == 0 && connectionPoolSize > INITIALCONNECTIONNUMBER)
645
     {
646
       //db connection having index from  to connectionpoolsize -1
647
       //intialConnectionnumber should be close and remove from pool
648
       for ( int i=connectionPoolSize-1; i >= INITIALCONNECTIONNUMBER ; i--)
649
       {
650
        
651
         //get the dbconnection from pool
652
         conn = (DBConnection) connectionPool.elementAt(i);
653
         
654
         try
655
         {
656
           //close conn
657
           conn.close();
658
         }//try
659
         catch (SQLException e)
660
         { 
661
           // set hadException ture
662
           hasException = true;
663
           MetaCatUtil.debugMessage("Couldn't close a DBConnection in " +
664
                            "DBConnectionPool.shrinkDBConnectionPoolSize: " +
665
                            e.getMessage(), 30);
666
         }//catch
667
                                        
668
        //remove it from pool
669
        connectionPool.remove(i);
670
        // becuase enter the loop, set result true
671
        result = true;
672
       }//for
673
     }//if
674
     
675
     //if hasException is true ( there at least once exception happend)
676
     // the result should be false
677
     if (hasException)
678
     {
679
       result =false;
680
     }//if
681
     // return result
682
     return result;
683
  }//shrinkDBConnectionPoolSize
684
   
685
    /**
686
   * Method to decrease dbconnection pool size when all dbconnections are idle
687
   * If all connections are free and connection pool size greater than 
688
   * initial value, shrink connection pool size to intital value
689
   */
690
  public static synchronized void shrinkDBConnectionPoolSize() 
691
  {
692
     int connectionPoolSize = 0; //store the number of dbconnection pool size
693
     int freeConnectionSize = 0; //store the number of free dbconnection in pool
694
     int difference = 0; // store the difference number between connection size
695
                         // and free connection
696
    
697
     DBConnection conn = null; // the dbconnection
698
     connectionPoolSize = connectionPool.size();
699
     freeConnectionSize = getFreeDBConnectionNumber();
700
     MetaCatUtil.debugMessage("Connection pool size: " +connectionPoolSize, 35);
701
     MetaCatUtil.debugMessage("Free Connection number: "+freeConnectionSize, 35);
702
     difference = connectionPoolSize - freeConnectionSize;
703
     
704
     //If all connections are free and connection pool size greater than 
705
     //initial value, shrink connection pool size to intital value
706
     if (difference == 0 && connectionPoolSize > INITIALCONNECTIONNUMBER)
707
     {
708
       //db connection having index from  to connectionpoolsize -1
709
       //intialConnectionnumber should be close and remove from pool
710
       for ( int i=connectionPoolSize-1; i >= INITIALCONNECTIONNUMBER ; i--)
711
       {
712
        
713
         //get the dbconnection from pool
714
         conn = (DBConnection) connectionPool.elementAt(i);
715
         //make sure again the DBConnection status is free
716
         if (conn.getStatus()==FREE)
717
         {
718
           try
719
           {
720
             //close conn
721
             conn.close();
722
           }//try
723
           catch (SQLException e)
724
           { 
725
          
726
             MetaCatUtil.debugMessage("Couldn't close a DBConnection in " +
727
                            "DBConnectionPool.shrinkDBConnectionPoolSize: " +
728
                            e.getMessage(), 30);
729
           }//catch
730
        
731
           //remove it from pool
732
           connectionPool.remove(i);
733
         }//if
734
       
735
       }//for
736
     }//if
737
     
738
    
739
  }//shrinkDBConnectionPoolSize
740
   
741
  
742
}//DBConnectionPool
(19-19/57)