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

(-)java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java (+16 lines)
Lines 390-395 public class DataSourceProxy implements PoolConfiguration { Link Here
390
        this.poolProperties.setValidationQuery(validationQuery);
390
        this.poolProperties.setValidationQuery(validationQuery);
391
    }
391
    }
392
392
393
    /**
394
     * {@inheritDoc}
395
     */
396
    @Override
397
    public void setValidator(Validator validator) {
398
        getPoolProperties().setValidator(validator);
399
    }
400
393
    /** 
401
    /** 
394
     * {@inheritDoc}
402
     * {@inheritDoc}
395
     */
403
     */
Lines 818-823 public class DataSourceProxy implements PoolConfiguration { Link Here
818
        return getPoolProperties().getValidationQuery();
826
        return getPoolProperties().getValidationQuery();
819
    }
827
    }
820
828
829
    /**
830
     * {@inheritDoc}
831
     */
832
    @Override
833
    public Validator getValidator() {
834
        return getPoolProperties().getValidator();
835
    }
836
821
    /** 
837
    /** 
822
     * {@inheritDoc}
838
     * {@inheritDoc}
823
     */
839
     */
(-)java/org/apache/tomcat/jdbc/pool/PoolConfiguration.java (+12 lines)
Lines 522-528 public interface PoolConfiguration { Link Here
522
     * @param validationQuery the query used for validation or null if no validation is performed
522
     * @param validationQuery the query used for validation or null if no validation is performed
523
     */
523
     */
524
    public void setValidationQuery(String validationQuery);
524
    public void setValidationQuery(String validationQuery);
525
    
526
    /**
527
     * @return the optional validator object - may be null
528
     */
529
    public Validator getValidator();
525
530
531
    /**
532
     * Set an optional validator object which will be used in place of test queries. If set to
533
     * null, standard validation will be used.
534
     * 
535
     * @param validator the optional validator
536
     */
537
    public void setValidator(Validator validator);
526
    
538
    
527
    /**
539
    /**
528
     * avoid excess validation, only run validation at most at this frequency - time in milliseconds. 
540
     * avoid excess validation, only run validation at most at this frequency - time in milliseconds. 
(-)java/org/apache/tomcat/jdbc/pool/PoolProperties.java (+17 lines)
Lines 51-56 public class PoolProperties implements PoolConfiguration { Link Here
51
    protected int minIdle = initialSize;
51
    protected int minIdle = initialSize;
52
    protected int maxWait = 30000;
52
    protected int maxWait = 30000;
53
    protected String validationQuery;
53
    protected String validationQuery;
54
    protected Validator validator;
54
    protected boolean testOnBorrow = false;
55
    protected boolean testOnBorrow = false;
55
    protected boolean testOnReturn = false;
56
    protected boolean testOnReturn = false;
56
    protected boolean testWhileIdle = false;
57
    protected boolean testWhileIdle = false;
Lines 338-343 public class PoolProperties implements PoolConfiguration { Link Here
338
        return validationQuery;
339
        return validationQuery;
339
    }
340
    }
340
341
342
    /**
343
     * {@inheritDoc}
344
     */
345
    @Override
346
    public Validator getValidator() {
347
        return validator;
348
    }
349
341
    /** 
350
    /** 
342
     * {@inheritDoc}
351
     * {@inheritDoc}
343
     */
352
     */
Lines 631-636 public class PoolProperties implements PoolConfiguration { Link Here
631
        this.validationQuery = validationQuery;
640
        this.validationQuery = validationQuery;
632
    }
641
    }
633
642
643
    /**
644
     * {@inheritDoc}
645
     */
646
    @Override
647
    public void setValidator(Validator validator) {
648
        this.validator = validator;
649
    }
650
634
    /** 
651
    /** 
635
     * {@inheritDoc}
652
     * {@inheritDoc}
636
     */
653
     */
(-)java/org/apache/tomcat/jdbc/pool/PooledConnection.java (-11 / +19 lines)
Lines 362-382 public class PooledConnection { Link Here
362
            return true;
362
            return true;
363
        }
363
        }
364
364
365
        String query = (VALIDATE_INIT==validateAction && (poolProperties.getInitSQL()!=null))?poolProperties.getInitSQL():sql;
365
        //Don't bother validating if already have recently enough
366
367
        if (query==null) query = poolProperties.getValidationQuery();
368
369
        if (query == null) {
370
            //no validation possible
371
            return true;
372
        }
373
        long now = System.currentTimeMillis();
366
        long now = System.currentTimeMillis();
374
        if (this.poolProperties.getValidationInterval() > 0 &&
367
        if (validateAction!=VALIDATE_INIT &&
375
            (validateAction!=VALIDATE_INIT) &&    
368
            poolProperties.getValidationInterval() > 0 &&
376
            (now - this.lastValidated) <
369
            (now - this.lastValidated) <
377
            this.poolProperties.getValidationInterval()) {
370
            poolProperties.getValidationInterval()) {
378
            return true;
371
            return true;
379
        }
372
        }
373
374
        if (poolProperties.getValidator() != null) {
375
            return poolProperties.getValidator().validate(connection, validateAction);
376
        }
377
        
378
        String query = sql;
379
        
380
        if (validateAction == VALIDATE_INIT && poolProperties.getInitSQL() != null) {
381
            query = poolProperties.getInitSQL();
382
        }
383
384
        if (query == null) {
385
            query = poolProperties.getValidationQuery();
386
        }
387
        
380
        Statement stmt = null;
388
        Statement stmt = null;
381
        try {
389
        try {
382
            stmt = connection.createStatement();
390
            stmt = connection.createStatement();
(-)java/org/apache/tomcat/jdbc/pool/Validator.java (+37 lines)
Added Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package org.apache.tomcat.jdbc.pool;
18
19
import java.sql.Connection;
20
21
/**
22
 * Interface to be implemented by custom validator classes.
23
 * 
24
 * @author mpassell
25
 */
26
public interface Validator {
27
    /**
28
     * Validate a connection and return a boolean to indicate if it's valid.
29
     * 
30
     * @param connection the Connection object to test
31
     * @param validateAction the action used. One of {@link PooledConnection#VALIDATE_BORROW},
32
     *   {@link PooledConnection#VALIDATE_IDLE}, {@link PooledConnection#VALIDATE_INIT} or
33
     *   {@link PooledConnection#VALIDATE_RETURN}
34
     * @return true if the connection is valid
35
     */
36
    public boolean validate(Connection connection, int validateAction);
37
}
(-)java/org/apache/tomcat/jdbc/pool/jmx/ConnectionPool.java (-43 / +60 lines)
Lines 29-34 import javax.management.NotificationListener; Link Here
29
import org.apache.juli.logging.Log;
29
import org.apache.juli.logging.Log;
30
import org.apache.juli.logging.LogFactory;
30
import org.apache.juli.logging.LogFactory;
31
import org.apache.tomcat.jdbc.pool.PoolConfiguration;
31
import org.apache.tomcat.jdbc.pool.PoolConfiguration;
32
import org.apache.tomcat.jdbc.pool.Validator;
32
import org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition;
33
import org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition;
33
34
34
public class ConnectionPool extends NotificationBroadcasterSupport implements ConnectionPoolMBean  {
35
public class ConnectionPool extends NotificationBroadcasterSupport implements ConnectionPoolMBean  {
Lines 278-283 public class ConnectionPool extends NotificationBroadcasterSupport implements Co Link Here
278
        return getPoolProperties().getValidationQuery();
279
        return getPoolProperties().getValidationQuery();
279
    }
280
    }
280
281
282
    /**
283
     * {@inheritDoc}
284
     */
285
    @Override
286
    public Validator getValidator() {
287
        return getPoolProperties().getValidator();
288
    }
289
281
    public boolean isAccessToUnderlyingConnectionAllowed() {
290
    public boolean isAccessToUnderlyingConnectionAllowed() {
282
        return getPoolProperties().isAccessToUnderlyingConnectionAllowed();
291
        return getPoolProperties().isAccessToUnderlyingConnectionAllowed();
283
    }
292
    }
Lines 557-604 public class ConnectionPool extends NotificationBroadcasterSupport implements Co Link Here
557
    }
566
    }
558
    
567
    
559
    /**
568
    /**
560
    * {@inheritDoc}
569
     * {@inheritDoc}
561
    */
570
     */
562
   @Override
571
    @Override
563
   public int getSuspectTimeout() {
572
    public void setValidator(Validator validator) {
564
       return getPoolProperties().getSuspectTimeout(); 
573
        getPoolProperties().setValidator(validator);
565
   }
574
    }
566
575
567
   /**
576
    /**
568
    * {@inheritDoc}
577
     * {@inheritDoc}
569
    */
578
     */
570
   @Override
579
    @Override
571
   public void setSuspectTimeout(int seconds) {
580
    public int getSuspectTimeout() {
572
       //no op
581
        return getPoolProperties().getSuspectTimeout(); 
573
   }
582
    }
574
   
583
575
   /** 
584
    /**
576
    * {@inheritDoc}
585
     * {@inheritDoc}
577
    */
586
     */
578
   public void setDataSource(Object ds) {
587
    @Override
579
       getPoolProperties().setDataSource(ds);
588
    public void setSuspectTimeout(int seconds) {
580
   }
589
        //no op
581
   
590
    }
582
   /** 
591
583
    * {@inheritDoc}
592
    /** 
584
    */
593
     * {@inheritDoc}
585
   public Object getDataSource() {
594
     */
586
       return getPoolProperties().getDataSource();
595
    public void setDataSource(Object ds) {
587
   }
596
        getPoolProperties().setDataSource(ds);
588
   
597
    }
589
   
598
590
   /** 
599
    /** 
591
    * {@inheritDoc}
600
     * {@inheritDoc}
592
    */
601
     */
593
   public void setDataSourceJNDI(String jndiDS) {
602
    public Object getDataSource() {
594
       //noop
603
        return getPoolProperties().getDataSource();
595
   }
604
    }
596
   
605
597
   /** 
606
598
    * {@inheritDoc}
607
    /** 
599
    */
608
     * {@inheritDoc}
600
   public String getDataSourceJNDI() {
609
     */
601
       return getPoolProperties().getDataSourceJNDI();
610
    public void setDataSourceJNDI(String jndiDS) {
602
   }
611
        //noop
612
    }
613
614
    /** 
615
     * {@inheritDoc}
616
     */
617
    public String getDataSourceJNDI() {
618
        return getPoolProperties().getDataSourceJNDI();
619
    }
603
620
604
}
621
}

Return to bug 48817