Project

General

Profile

« Previous | Next » 

Revision 1220

Added by Jing Tao almost 22 years ago

Add a new method shrinkConnectionPoolSize .

View differences:

src/edu/ucsb/nceas/metacat/DBConnectionPool.java
623 623
   * If all connections are free and connection pool size greater than 
624 624
   * initial value, shrink connection pool size to intital value
625 625
   */
626
  public static synchronized void shrinkDBConnectionPoolSize()
626
  public static synchronized boolean shrinkConnectionPoolSize() 
627 627
  {
628 628
     int connectionPoolSize = 0; //store the number of dbconnection pool size
629 629
     int freeConnectionSize = 0; //store the number of free dbconnection in pool
630 630
     int difference = 0; // store the difference number between connection size
631 631
                         // and free connection
632
     DBConnection conn = null;
632
     boolean hasException = false; //to check if has a exception happend
633
     boolean result = false; //result
634
     DBConnection conn = null; // the dbconnection
633 635
     connectionPoolSize = connectionPool.size();
634 636
     freeConnectionSize = getFreeDBConnectionNumber();
635
     MetaCatUtil.debugMessage("Connection pool size: " +connectionPoolSize, 20);
636
     MetaCatUtil.debugMessage("Free Connection number: "+freeConnectionSize,20);
637
     MetaCatUtil.debugMessage("Connection pool size: " +connectionPoolSize, 3);
638
     MetaCatUtil.debugMessage("Free Connection number: "+freeConnectionSize, 3);
637 639
     difference = connectionPoolSize - freeConnectionSize;
638 640
     
639 641
     //If all connections are free and connection pool size greater than 
640 642
     //initial value, shrink connection pool size to intital value
641 643
     if (difference == 0 && connectionPoolSize > INITIALCONNECTIONNUMBER)
642 644
     {
643
       //db connection having index from intialConnectionnumber - 1 to
644
       //connectionpoolsize -1 should be close and remove from pool
645
       for ( int i=INITIALCONNECTIONNUMBER -1; i<connectionPoolSize; i++)
645
       //db connection having index from  to connectionpoolsize -1
646
       //intialConnectionnumber should be close and remove from pool
647
       for ( int i=connectionPoolSize-1; i >= INITIALCONNECTIONNUMBER ; i--)
646 648
       {
649
        
647 650
         //get the dbconnection from pool
648 651
         conn = (DBConnection) connectionPool.elementAt(i);
649
         //close conn
650 652
         
651 653
         try
652 654
         {
655
           //close conn
653 656
           conn.close();
654 657
         }//try
655 658
         catch (SQLException e)
656
         {
657
           MetaCatUtil.debugMessage("Couldn't close the DBConnection in" +
658
                         " DBConnectionPool.shrinkDBConnectionPoolSize: " +
659
                         e.getMessage(), 30);
659
         { 
660
           // set hadException ture
661
           hasException = true;
662
           MetaCatUtil.debugMessage("Couldn't close a DBConnection in " +
663
                            "DBConnectionPool.shrinkDBConnectionPoolSize: " +
664
                            e.getMessage(), 30);
660 665
         }//catch
661
         //remove it from pool
662
         connectionPool.remove(i);
666
                                        
667
        //remove it from pool
668
        connectionPool.remove(i);
669
        // becuase enter the loop, set result true
670
        result = true;
663 671
       }//for
664 672
     }//if
673
     
674
     //if hasException is true ( there at least once exception happend)
675
     // the result should be false
676
     if (hasException)
677
     {
678
       result =false;
679
     }//if
680
     // return result
681
     return result;
665 682
  }//shrinkDBConnectionPoolSize
683
   
684
    /**
685
   * Method to decrease dbconnection pool size when all dbconnections are idle
686
   * If all connections are free and connection pool size greater than 
687
   * initial value, shrink connection pool size to intital value
688
   */
689
  public static synchronized void shrinkDBConnectionPoolSize() 
690
  {
691
     int connectionPoolSize = 0; //store the number of dbconnection pool size
692
     int freeConnectionSize = 0; //store the number of free dbconnection in pool
693
     int difference = 0; // store the difference number between connection size
694
                         // and free connection
695
    
696
     DBConnection conn = null; // the dbconnection
697
     connectionPoolSize = connectionPool.size();
698
     freeConnectionSize = getFreeDBConnectionNumber();
699
     MetaCatUtil.debugMessage("Connection pool size: " +connectionPoolSize, 3);
700
     MetaCatUtil.debugMessage("Free Connection number: "+freeConnectionSize, 3);
701
     difference = connectionPoolSize - freeConnectionSize;
666 702
     
703
     //If all connections are free and connection pool size greater than 
704
     //initial value, shrink connection pool size to intital value
705
     if (difference == 0 && connectionPoolSize > INITIALCONNECTIONNUMBER)
706
     {
707
       //db connection having index from  to connectionpoolsize -1
708
       //intialConnectionnumber should be close and remove from pool
709
       for ( int i=connectionPoolSize-1; i >= INITIALCONNECTIONNUMBER ; i--)
710
       {
711
        
712
         //get the dbconnection from pool
713
         conn = (DBConnection) connectionPool.elementAt(i);
714
         //make sure again the DBConnection status is free
715
         if (conn.getStatus()==FREE)
716
         {
717
           try
718
           {
719
             //close conn
720
             conn.close();
721
           }//try
722
           catch (SQLException e)
723
           { 
724
          
725
             MetaCatUtil.debugMessage("Couldn't close a DBConnection in " +
726
                            "DBConnectionPool.shrinkDBConnectionPoolSize: " +
727
                            e.getMessage(), 30);
728
           }//catch
729
        
730
           //remove it from pool
731
           connectionPool.remove(i);
732
         }//if
733
       
734
       }//for
735
     }//if
736
     
737
    
738
  }//shrinkDBConnectionPoolSize
667 739
   
668 740
  
669 741
}//DBConnectionPool

Also available in: Unified diff