Project

General

Profile

« Previous | Next » 

Revision 1095

Added by Jing Tao over 22 years ago

Add a method getDBConnection. In this method, method name of method which checked out the connection can be passed to DBConnection object. In run() method, if a connection is checked out for too long (30 seconds), the method name and connection name will be print out.

View differences:

src/edu/ucsb/nceas/metacat/DBConnectionPool.java
70 70
                 Long.parseLong(MetaCatUtil.getOption("maximumConnectionTime"));
71 71
  //maximum number for using a connection.
72 72
  final static int MAXIMUMUSAGENUMBER =
73
                  Integer.parseInt(MetaCatUtil.getOption("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"));
74 80
               
75 81
  
76 82
  final static int FREE = 0; //status of a connection
......
94 100
                  ("Maximum connection time: "+MAXIMUMCONNECTIONTIME, 49);
95 101
    MetaCatUtil.debugMessage
96 102
                  ("Maximum usage count: "+MAXIMUMUSAGENUMBER, 49);
103
    MetaCatUtil.debugMessage
104
             ("Running recycle thread or not: "+DBCONNECTIONRECYCLETHREAD, 49);
105
    MetaCatUtil.debugMessage
106
             ("Cycle time of recycle: "+CYCLETIMEOFDBCONNECTION, 49); 
97 107
    if (instance == null) 
98 108
    {
99 109
      instance = new DBConnectionPool();
......
111 121
    connectionPool = new Vector();
112 122
    initialDBConnectionPool();
113 123
    //running the thread to recycle DBConnection
114
    runner = new Thread(this);
115
    runner.start();
124
    if (DBCONNECTIONRECYCLETHREAD.equals("on"))
125
    {
126
      runner = new Thread(this);
127
      runner.start();
128
    }
116 129
  }//DBConnection
117 130

  
118 131
  /**
......
159 172
  }*/
160 173
  
161 174
 
175
  /**
176
   * Method to get a DBConnection in connection pool
177
   * 1) try to get a DBConnection from DBConnection pool
178
   * 2) if 1) failed, then check the size of pool. If the size reach the
179
   *    maximum number of connection, throw a exception: couldn't get one
180
   * 3) If the size is less than the maximum number of connectio, create some
181
   *    new connections and recursive get one
182
   * @param methodName, the name of method which will check connection out
183
   */
184
  public static synchronized DBConnection getDBConnection(String methodName) 
185
                                                throws SQLException
186
  {
187
    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++)
192
      {
193
        db = (DBConnection) connectionPool.elementAt(i);
194
        //check if the connection is free
195
        if (db.getStatus()==FREE)
196
        {
197
          //If this connection is good, return this DBConnection
198
          if (validateDBConnection(db))
199
          {
200
            
201
            //set this DBConnection status
202
            db.setStatus(BUSY);
203
            //increase one usageCount
204
            db.increaseUsageCount(1);
205
            //set method name to DBConnection
206
            db.setCheckOutMethodName(methodName);
207
            //debug message
208
            MetaCatUtil.debugMessage("The connection is checked out: "
209
                                       +db.getTag(), 5);
210
            MetaCatUtil.debugMessage("The age is "+db.getAge(), 5);
211
            MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 5);
212
            MetaCatUtil.debugMessage("The conection time it has: "
213
                                                +db.getConnectionTime(), 5);
214
            //set check out time
215
            db.setCheckOutTime(System.currentTimeMillis());
216
            //check it out
217
            return db;
218
          }//if
219
          else//The DBConnection has some problem
220
          {
221
            //close this DBConnection
222
            db.close();
223
            //remove it form connection pool
224
            connectionPool.remove(i);
225
            //insert a new DBConnection to same palace
226
            db = new DBConnection();
227
            connectionPool.insertElementAt(db, i);
228
          }//else
229
        }//if
230
      }//for
231
    
232
    
233
    //if couldn't get a connection, we should increase DBConnection pool
234
    //if the connection pool size is less than maximum connection number
235
    int poolSize = connectionPool.size(); 
236
    if ( poolSize < MAXIMUMCONNECTIONNUMBER )
237
    {
238
       if ((poolSize+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
239
       { 
240
         //if we can create INCREASECONNECTIONNUMBER of new DBConnection
241
         //add to connection pool
242
         for ( int i=0; i<INCREASECONNECTIONNUMBER; i++)
243
         {
244
           DBConnection dbConn = new DBConnection();
245
           connectionPool.add(dbConn);
246
         }//for
247
       }//if
248
       else
249
       {
250
         //There is no enough room to increase INCREASECONNECTIONNUMBER 
251
         //we create new DBCoonection to Maximum connection number
252
         for (int i= poolSize+1; i<= MAXIMUMCONNECTIONNUMBER; i++)
253
         {
254
           DBConnection dbConn = new DBConnection();
255
           connectionPool.add(dbConn);
256
         }//for
257
       }//else
258
   
259
    }//if
260
    else
261
    {
262
      throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER + 
263
                            " open db connections is reached." +
264
                            " New db connection to MetaCat" +
265
                            " cannot be established.");
266
      
267
    }//else
268
    
269
    //recursive to get new connection    
270
    return getDBConnection();
271
  }//getDBConnection
162 272

  
163

  
164 273
  /**
165 274
   * Method to get a DBConnection in connection pool
166 275
   * 1) try to get a DBConnection from DBConnection pool
......
248 357
                            " open db connections is reached." +
249 358
                            " New db connection to MetaCat" +
250 359
                            " cannot be established.");
360
      
251 361
    }//else
252 362
    
253 363
    //recursive to get new connection    
......
340 450
      dbConn = (DBConnection) connectionPool.elementAt(index);
341 451
      //set status to free
342 452
      dbConn.setStatus(FREE);
343
      //count check out time
453
      //count connection time
344 454
      dbConn.setConnectionTime
345 455
                          (System.currentTimeMillis()-dbConn.getCheckOutTime());
456
                          
457
      //set check out time to 0
458
      dbConn.setCheckOutTime(0);
459
      
346 460
      MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+" checked in.",
347 461
                                                                        5);
348 462
    }//else
......
388 502
  {
389 503
    
390 504
    //shut down the backgroud recycle thread
391
    runner.interrupt();
392
    
505
    if (DBCONNECTIONRECYCLETHREAD.equals("on"))
506
    {
507
      runner.interrupt();
508
    }
393 509
    //cose every dbconnection in the pool
394 510
    synchronized(connectionPool)
395 511
    {
......
424 540
        for (int i=0; i<connectionPool.size(); i++)
425 541
        {
426 542
          dbConn = (DBConnection) connectionPool.elementAt(i);
543
          
544
          //if a DBConnection time is greater than 30000 milliseconds 
545
          //print it out
546
          if ((System.currentTimeMillis()-dbConn.getCheckOutTime())>=30000)
547
          {
548
            MetaCatUtil.debugMessage("This DBConnection is checked out for"
549
              +(System.currentTimeMillis()-dbConn.getCheckOutTime())/1000 , 1);
550
            MetaCatUtil.debugMessage(dbConn.getTag(), 1);
551
            MetaCatUtil.debugMessage(dbConn.getCheckOutMethodName(), 1);
552
            
553
          }
554
          
555
          //check the validation of free connection in the pool
427 556
          if (dbConn.getStatus() == FREE)
428 557
          {
429 558
            try
430 559
            {
431 560
              //try to print out the warning message for every connection
432 561
              MetaCatUtil.debugMessage("Warning for connection "+dbConn.getTag()
433
                                    +" : "+ dbConn.getWarningMessage(), 5);
562
                                    +" : "+ dbConn.getWarningMessage(), 1);
434 563
              //check if it is valiate, if not create new one and replace old one
435 564
              if (!validateDBConnection(dbConn))
436 565
              {
......
451 580
          }//if
452 581
        }//for
453 582
      }//synchronize   
454
      //sleep 1200 seconds (20 minutes)
583
      //Thread sleep 
455 584
      try
456 585
      {
457
        Thread.sleep(30000);
586
        Thread.sleep(CYCLETIMEOFDBCONNECTION);
458 587
      }
459 588
      catch (Exception e)
460 589
      {

Also available in: Unified diff