Project

General

Profile

« Previous | Next » 

Revision 1089

Added by Jing Tao over 22 years ago

Add some new methods in it. Remove openDBconnection part to DBConnection class.

View differences:

src/edu/ucsb/nceas/metacat/DBConnectionPool.java
30 30

  
31 31
import java.io.*;
32 32
import java.util.Vector;
33
import java.util.zip.*;
34
import java.net.URL;
35
import java.net.MalformedURLException;
33
import java.lang.*;
36 34
import java.sql.*;
37 35
import java.util.Stack;
38 36
import java.util.Hashtable;
......
46 44
 */
47 45
public class DBConnectionPool
48 46
{
49
  private String DBDriver;
50
  private String DBConnectedJDBC;
51
  private String userName;
52
  private String passWord;
53
  private int currentConnectionNumber;
54
  
47

  
55 48
  //static attributes
56 49
  private static DBConnectionPool instance;
57 50
  private static Vector connectionPool;
58 51
  
59
  
60
  final static int maximumConnectionNumber=
52
  //maximum connection number in the connection pool
53
  final static int MAXIMUMCONNECTIONNUMBER=
61 54
                  Integer.parseInt(MetaCatUtil.getOption("maximumConnections"));
62
  final static int initialConnectionNumber=
55
  //inintila connection number int the connection pool
56
  final static int INITIALCONNECTIONNUMBER=
63 57
                  Integer.parseInt(MetaCatUtil.getOption("initialConnections"));
64
 
58
  //the number to increase connection pool size
59
  final static int INCREASECONNECTIONNUMBER=
60
                Integer.parseInt(MetaCatUtil.getOption("incrementConnections"));
61
  //maximum age for a connection (in milli seconds)
62
  final static long MAXIMUMAGE =
63
                  Long.parseLong(MetaCatUtil.getOption("maximumConnectionAge"));
64
  //maximum connection time for a connection ( in milli second)
65
  final static long MAXIMUMCONNECTIONTIME =
66
                 Long.parseLong(MetaCatUtil.getOption("maximumConnectionTime"));
67
  //maximum number for using a connection.
68
  final static int MAXIMUMUSAGENUMBER =
69
                  Integer.parseInt(MetaCatUtil.getOption("maximumUsageNumber"));  
70
               
71
  
72
  final static int FREE = 0; //status of a connection
73
  final static int BUSY = 1; //statis of a connection
65 74
  /**
66 75
   * Returns the single instance, creating one if it's the
67 76
   * first time this method is called.
68 77
   */
69 78
  static synchronized public DBConnectionPool getInstance()
70
                                 throws SQLException, ClassNotFoundException 
79
                                 throws SQLException 
71 80
  {
72 81
    if (instance == null) 
73 82
    {
......
75 84
    }
76 85
    return instance;
77 86
  }//getInstance
87

  
88
  /**
89
   * Method to get the size of DBConnectionPool
90
   */
91
  public int getSizeOfDBConnectionPool()
92
  {
93
    return connectionPool.size();
94
  }
78 95
  
79 96
  /**
80 97
   * This is a private constructor since it is singleton
81 98
   */
82 99
   
83
  private DBConnectionPool()  throws SQLException, ClassNotFoundException 
100
  private DBConnectionPool()  throws SQLException 
84 101
  {
85
    DBDriver=MetaCatUtil.getOption("dbDriver");
86
    DBConnectedJDBC=MetaCatUtil.getOption("defaultDB");
87
    userName=MetaCatUtil.getOption("user");
88
    passWord=MetaCatUtil.getOption("password");
102

  
103
    initialDBConnectionPool();
89 104
  }//DBConnection
90 105
  
91
  /** 
92
   * Method to establish a JDBC database connection 
93
   */
94
  private Connection openConnection()
95
                  throws SQLException, ClassNotFoundException 
96
  {
97
    return openConnection(DBDriver, DBConnectedJDBC, userName, passWord);
98
  }//openDBConnection
99

  
100
  /** 
101
   * Method to establish a JDBC database connection 
102
   *
103
   * @param dbDriver the string representing the database driver
104
   * @param connection the string representing the database connectin parameters
105
   * @param user name of the user to use for database connection
106
   * @param password password for the user to use for database connection
107
   */
108
  private Connection openConnection(String dbDriver, String connection,
109
                String user, String password)
110
                throws SQLException, ClassNotFoundException 
111
 {
112

  
113
     // Load the Oracle JDBC driver
114
     Class.forName (dbDriver);
115
     // Connect to the database
116
     Connection conn = DriverManager.getConnection( connection, user, password);
117
     return conn;
118
  }//OpenDBConnection
119

  
120 106
  /**
121 107
   * Method to initial a pool of DBConnection objects 
122 108
   */
123
  public Vector initialDBConnectionPool()
124
                throws SQLException, ClassNotFoundException 
109
  private Vector initialDBConnectionPool() throws SQLException 
125 110
 {
126 111

  
127
    Connection conn = null;
128 112
    DBConnection dbConn = null;
129 113
    
130
    for ( int i = 0; i < initialConnectionNumber; i++ ) 
114
    for ( int i = 0; i < INITIALCONNECTIONNUMBER; i++ ) 
131 115
    {
132
      //open a connection to db
133
      conn = openConnection();
134 116
      //create a new object of DBConnection
117
      //this DBConnection object has a new connection in it
118
      //it automatically generate the createtime and tag
135 119
      dbConn = new DBConnection();
136
      //set connetion to DBConnection
137
      dbConn.setDBConnection(conn);
138 120
      //put DBConnection into vetor
139 121
      connectionPool.add(dbConn);
140 122
    }    
......
142 124
    return connectionPool;
143 125
  }
144 126
  
127
 
128

  
129

  
130
  /**
131
   * Method to get a DBConnection in connection pool
132
   */
133
  public static DBConnection getDBConnection() throws SQLException
134
  {
135
    DBConnection db = null;    
136
    //try every DBConnection in the pool
137
    synchronized(connectionPool)
138
    {
139
      for (int i=0; i<connectionPool.size(); i++)
140
      {
141
        db = (DBConnection) connectionPool.elementAt(i);
142
        //check if the connection is free
143
        if (db.getStatus()==FREE)
144
        {
145
          //If this connection is good, return this DBConnection
146
          if (validateDBConnection(db))
147
          {
148
            //set this DBConnection status
149
            db.setStatus(BUSY);
150
            //set check out time
151
            db.setCheckOutTime(System.currentTimeMillis());
152
            //check it out
153
            return db;
154
          }//if
155
          else//The DBConnection has some problem
156
          {
157
            //close this DBConnection
158
            db.close();
159
            //remove it form connection pool
160
            connectionPool.remove(i);
161
            //insert a new DBConnection to same palace
162
            db = new DBConnection();
163
            connectionPool.insertElementAt(db, i);
164
          }//else
165
        }//if
166
      }//for
167
    }//sychronize
168
    
169
    //if couldn't get a connection, we should increase DBConnection pool
170
    //if the connection pool size is less than maximum connection number
171
    int poolSize = connectionPool.size(); 
172
    if ( poolSize < MAXIMUMCONNECTIONNUMBER )
173
    {
174
       if ((poolSize+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
175
       { 
176
         //if we can create INCREASECONNECTIONNUMBER of new DBConnection
177
         //add to connection pool
178
         for ( int i=0; i<INCREASECONNECTIONNUMBER; i++)
179
         {
180
           DBConnection dbConn = new DBConnection();
181
           connectionPool.add(dbConn);
182
         }//for
183
       }//if
184
       else
185
       {
186
         //There is no enough room to increase INCREASECONNECTIONNUMBER 
187
         //we create new DBCoonection to Maximum connection number
188
         for (int i= poolSize+1; i<= MAXIMUMCONNECTIONNUMBER; i++)
189
         {
190
           DBConnection dbConn = new DBConnection();
191
           connectionPool.add(dbConn);
192
         }//for
193
       }//else
194
   
195
    }//if
196
    else
197
    {
198
      throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER + 
199
                            " open db connections is reached." +
200
                            " New db connection to MetaCat" +
201
                            " cannot be established.");
202
    }//else
203
    
204
    //recursive to get new connection    
205
    return getDBConnection();
206
  }//getDBConnection
145 207
  
146 208
  
147 209
  
210
  /** 
211
   * Method to check if a db connection works fine or not
212
   * Check points include:
213
   * 1. check the usageCount if it is too many
214
   * 2. check the dbconne age if it is too old
215
   * 3. check the connection time if it is too long
216
   * 4. run simple sql query
217
   *
218
   * @param dbConn, the DBConnection object need to check
219
   */
220
  private static boolean validateDBConnection (DBConnection dbConn)
221
  {
222
    Connection conn = null;
223
    
224
    //Check if the DBConnection usageCount if it is too many
225
    if (dbConn.getUsageCount() >= MAXIMUMUSAGENUMBER )
226
    {
227
      MetaCatUtil.debugMessage("Connection usageCount is too many: "+
228
      dbConn.getUsageCount(), 50);
229
      return false;
230
    }
231
    
232
    //Check if the DBConnection has too much connection time
233
    if (dbConn.getConnectionTime() >= MAXIMUMCONNECTIONTIME)
234
    {
235
      MetaCatUtil.debugMessage("Connection has too much connection time: "+
236
      dbConn.getConnectionTime(), 50);
237
      return false;
238
    }
239
    
240
    //Check if the DBConnection is too old
241
    if (dbConn.getAge() >=MAXIMUMAGE)
242
    {
243
      MetaCatUtil.debugMessage("Connection is too old: "+dbConn.getAge(), 50);
244
      return false;
245
    }
246
    
247
    //Try to run a simple query
248
    conn = dbConn.getConnection();
249
    try
250
    {
251
      long startTime=System.currentTimeMillis();
252
      conn.getMetaData();
253
      long stopTime=System.currentTimeMillis();
254
      //increase one usagecount
255
      dbConn.increaseUsageCount(1);
256
      //increase connection time
257
      dbConn.setConnectionTime(stopTime-startTime);
148 258
  
259
    }
260
    catch (Exception e)
261
    {
262
      MetaCatUtil.debugMessage("Error in validateDBConnection: "
263
                                +e.getMessage(), 30);
264
      return false;
265
    }
266
    
267
    return true;
268
    
269
  }//validateDBConnection()
270
  
149 271
}//DBConnectionPool

Also available in: Unified diff