View | Details | Raw Unified | Return to bug 48817
Collapse All | Expand All

(-)java/org/apache/tomcat/jdbc/pool/PooledConnection.java (-8 / +53 lines)
Lines 17-22 Link Here
17
package org.apache.tomcat.jdbc.pool;
17
package org.apache.tomcat.jdbc.pool;
18
18
19
19
20
import java.lang.reflect.Method;
20
import java.sql.SQLException;
21
import java.sql.SQLException;
21
import java.sql.Statement;
22
import java.sql.Statement;
22
import java.util.HashMap;
23
import java.util.HashMap;
Lines 357-370 public class PooledConnection { Link Here
357
            return true;
358
            return true;
358
        }
359
        }
359
360
360
        String query = (VALIDATE_INIT==validateAction && (poolProperties.getInitSQL()!=null))?poolProperties.getInitSQL():sql;
361
        //Don't bother validating if already have recently enough
361
362
        if (query==null) query = poolProperties.getValidationQuery();
363
364
        if (query == null) {
365
            //no validation possible
366
            return true;
367
        }
368
        long now = System.currentTimeMillis();
362
        long now = System.currentTimeMillis();
369
        if (this.poolProperties.getValidationInterval() > 0 &&
363
        if (this.poolProperties.getValidationInterval() > 0 &&
370
            (validateAction!=VALIDATE_INIT) &&    
364
            (validateAction!=VALIDATE_INIT) &&    
Lines 372-377 public class PooledConnection { Link Here
372
            this.poolProperties.getValidationInterval()) {
366
            this.poolProperties.getValidationInterval()) {
373
            return true;
367
            return true;
374
        }
368
        }
369
370
        String query = (VALIDATE_INIT==validateAction && (poolProperties.getInitSQL()!=null))?poolProperties.getInitSQL():sql;
371
372
        if (query==null) query = poolProperties.getValidationQuery();
373
374
        if (query == null) {
375
          return validateWithoutQuery(10);
376
        }
377
        
375
        Statement stmt = null;
378
        Statement stmt = null;
376
        try {
379
        try {
377
            stmt = connection.createStatement();
380
            stmt = connection.createStatement();
Lines 388-393 public class PooledConnection { Link Here
388
        return false;
391
        return false;
389
    } //validate
392
    } //validate
390
393
394
    private boolean validateWithoutQuery(int isValidTimeoutSecs) {
395
      //check if connection is closed
396
      try {
397
        if (connection.isClosed()) {
398
          return false;
399
        }
400
      } catch (SQLException e) {
401
        log.debug("Problem while checking connection.isClosed().", e);
402
      }
403
      
404
      //look for Connection.isValid() and use it if present
405
      try {
406
        return tryIsValid(isValidTimeoutSecs);
407
      } catch (Exception e) {
408
        //logged within tryIsValid()
409
      }
410
      
411
      //fall back on last resort - getting DatabaseMetaData
412
      try {
413
        connection.getMetaData();
414
        return true;
415
      } catch (Exception e) {
416
        return false;
417
      }
418
    }
419
    
420
    private boolean tryIsValid(int timeout) throws Exception {
421
      Method isValid = connection.getClass().getMethod("isValid", new Class[] { Integer.TYPE });
422
      
423
      try {
424
        Boolean valid = (Boolean)isValid.invoke(connection, timeout);
425
        return valid.booleanValue();
426
      } catch (IllegalAccessException e) {
427
        //Should never happen since isValid() is public
428
        log.warn("Unable to access Connection.isValid()", e);
429
        throw e;
430
      } catch (Exception e) {
431
        log.debug("Problems calling Connection.isValid()", e);
432
        throw e;
433
      }
434
    }
435
391
    /**
436
    /**
392
     * The time limit for how long the object
437
     * The time limit for how long the object
393
     * can remain unused before it is released
438
     * can remain unused before it is released

Return to bug 48817