You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by le...@apache.org on 2003/07/28 20:22:55 UTC

cvs commit: avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource AbstractJdbcConnection.java Jdbc3Connection.java JdbcConnection.java JdbcConnectionFactory.java JdbcDataSource.java ResourceLimitingJdbcDataSource.java

leif        2003/07/28 11:22:54

  Modified:    datasource/src/java/org/apache/avalon/excalibur/datasource
                        AbstractJdbcConnection.java Jdbc3Connection.java
                        JdbcConnection.java JdbcConnectionFactory.java
                        JdbcDataSource.java
                        ResourceLimitingJdbcDataSource.java
  Log:
  Make it possible to set the maximum connection age before the connection needs
  to be pinged.  This was needed to be able to set it to 0, meaning that connections
  are always pinged before being returned.  Everything still defaults to the old value
  of 5 seconds.
  
  Revision  Changes    Path
  1.32      +24 -4     avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/AbstractJdbcConnection.java
  
  Index: AbstractJdbcConnection.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/AbstractJdbcConnection.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- AbstractJdbcConnection.java	21 Jul 2003 15:54:47 -0000	1.31
  +++ AbstractJdbcConnection.java	28 Jul 2003 18:22:53 -0000	1.32
  @@ -86,6 +86,9 @@
       protected Connection m_connection;
       private Object m_proxy;
       protected Pool m_pool;
  +    
  +    /** The maximum time since a connection was last used before it will be pinged. */
  +    protected int m_testAge;
       protected PreparedStatement m_testStatement;
       protected SQLException m_testException;
       protected long m_lastUsed = System.currentTimeMillis();
  @@ -130,11 +133,28 @@
        */
       public AbstractJdbcConnection( final Connection connection, final String keepAlive )
       {
  +        this( connection, keepAlive, 5000 );
  +    }
  +
  +    /**
  +     * @param connection a driver specific JDBC connection to be wrapped.
  +     * @param keepAlive a query which will be used to check the statis of the connection after it
  +     *                  has been idle.  A null value will cause the keep alive feature to
  +     *                  be disabled.
  +     * @param keepAliveAge the maximum age in milliseconds since a connection was last
  +     *                     used before it must be pinged using the keepAlive query.  Ignored
  +     *                     if keepAlive is null.
  +     */
  +    public AbstractJdbcConnection( final Connection connection,
  +                                   final String keepAlive,
  +                                   final int keepAliveAge )
  +    {
           m_connection = connection;
   
           // subclasses can override initialize()
           this.initialize();
   
  +        m_testAge = keepAliveAge;
           if( null == keepAlive || "".equals( keepAlive.trim() ) )
           {
               m_testStatement = null;
  @@ -214,9 +234,9 @@
   
           long age = System.currentTimeMillis() - m_lastUsed;
           
  -        // If the connection has not been used for 5 seconds, then make
  -        //  sure it is still alive.
  -        if ( ( m_testStatement != null ) && ( age > ( 5 * 1000 ) ) )
  +        // If the connection has not been used for for longer than the keep alive age,
  +        //  then make sure it is still alive.
  +        if ( ( m_testStatement != null ) && ( age > m_testAge ) )
           {
               if( getLogger().isDebugEnabled() )
               {
  
  
  
  1.16      +17 -1     avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/Jdbc3Connection.java
  
  Index: Jdbc3Connection.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/Jdbc3Connection.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- Jdbc3Connection.java	5 Mar 2003 18:59:00 -0000	1.15
  +++ Jdbc3Connection.java	28 Jul 2003 18:22:53 -0000	1.16
  @@ -75,4 +75,20 @@
       {
           super( connection, keepAlive );
       }
  +    
  +    /**
  +     * @param connection a driver specific JDBC connection to be wrapped.
  +     * @param keepAlive a query which will be used to check the statis of the connection after it
  +     *                  has been idle.  A null value will cause the keep alive feature to
  +     *                  be disabled.
  +     * @param keepAliveAge the maximum age in milliseconds since a connection was last
  +     *                     used before it must be pinged using the keepAlive query.  Ignored
  +     *                     if keepAlive is null.
  +     */
  +    public Jdbc3Connection( final Connection connection,
  +                            final String keepAlive,
  +                            final int keepAliveAge )
  +    {
  +        super( connection, keepAlive, keepAliveAge );
  +    }
   }
  
  
  
  1.12      +17 -1     avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/JdbcConnection.java
  
  Index: JdbcConnection.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/JdbcConnection.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- JdbcConnection.java	5 Mar 2003 18:59:01 -0000	1.11
  +++ JdbcConnection.java	28 Jul 2003 18:22:53 -0000	1.12
  @@ -74,5 +74,21 @@
       {
           super( connection, keepAlive );
       }
  +    
  +    /**
  +     * @param connection a driver specific JDBC connection to be wrapped.
  +     * @param keepAlive a query which will be used to check the statis of the connection after it
  +     *                  has been idle.  A null value will cause the keep alive feature to
  +     *                  be disabled.
  +     * @param keepAliveAge the maximum age in milliseconds since a connection was last
  +     *                     used before it must be pinged using the keepAlive query.  Ignored
  +     *                     if keepAlive is null.
  +     */
  +    public JdbcConnection( final Connection connection,
  +                           final String keepAlive,
  +                           final int keepAliveAge )
  +    {
  +        super( connection, keepAlive, keepAliveAge );
  +    }
   }
   
  
  
  
  1.21      +37 -6     avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/JdbcConnectionFactory.java
  
  Index: JdbcConnectionFactory.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/JdbcConnectionFactory.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- JdbcConnectionFactory.java	29 Apr 2003 02:58:39 -0000	1.20
  +++ JdbcConnectionFactory.java	28 Jul 2003 18:22:53 -0000	1.21
  @@ -74,6 +74,7 @@
       private final String m_password;
       private final boolean m_autoCommit;
       private final String m_keepAlive;
  +    private final int m_keepAliveAge;
       private final String m_connectionClass;
       private Class m_class;
       private static final String DEFAULT_KEEPALIVE = "SELECT 1";
  @@ -127,11 +128,39 @@
                                     final String keepAlive,
                                     final String connectionClass )
       {
  +        this( url, username, password, autoCommit, keepAlive, 5000, connectionClass );
  +    }
  +
  +    /**
  +     * Creates and configures a new JdbcConnectionFactory.
  +     *
  +     * @param url full JDBC database url.
  +     * @param username username to use when connecting to the database.
  +     * @param password password to use when connecting to the database.
  +     * @param autoCommit true if connections to the database should operate with auto commit
  +     *                   enabled.
  +     * @param keepAlive a query which will be used to check the statis of a connection after it
  +     *                  has been idle.  A null value will cause the keep alive feature to
  +     *                  be disabled.
  +     * @param keepAliveAge the maximum age in milliseconds since a connection was last
  +     *                     used before it must be pinged using the keepAlive query.  Ignored
  +     *                     if keepAlive is null.
  +     * @param connectionClass class of connections created by the factory.
  +     */
  +    public JdbcConnectionFactory( final String url,
  +                                  final String username,
  +                                  final String password,
  +                                  final boolean autoCommit,
  +                                  final String keepAlive,
  +                                  final int keepAliveAge,
  +                                  final String connectionClass )
  +    {
           this.m_dburl = url;
           this.m_username = username;
           this.m_password = password;
           this.m_autoCommit = autoCommit;
           this.m_keepAlive = keepAlive;
  +        this.m_keepAliveAge = keepAliveAge;
           this.m_connectionClass = connectionClass;
   
           try
  @@ -208,7 +237,7 @@
   
           try
           {
  -            jdbcConnection = getProxy( connection, this.m_keepAlive );
  +            jdbcConnection = getProxy( connection, this.m_keepAlive, this.m_keepAliveAge );
           }
           catch( Exception e )
           {
  @@ -254,22 +283,24 @@
           }
       }
   
  -    private Connection getProxy( Connection conn, String keepAlive )
  +    private Connection getProxy( Connection conn, String keepAlive, int keepAliveAge )
       {
           ProxiedJdbcConnection handler = null;
   
           try
           {
               Constructor builder = m_class.getConstructor( new Class[]{Connection.class,
  -                                                                      String.class} );
  -            handler = (ProxiedJdbcConnection)builder.newInstance( new Object[]{conn, keepAlive} );
  +                                                                      String.class,
  +                                                                      Integer.TYPE } );
  +            handler = (ProxiedJdbcConnection)builder.newInstance(
  +                new Object[]{conn, keepAlive, new Integer( keepAliveAge ) } );
           }
           catch( Exception e )
           {
               final String msg = "Could not create the proper invocation handler, "
                       + "defaulting to AbstractJdbcConnection";
               getLogger().error( msg, e );
  -            handler = new AbstractJdbcConnection( conn, keepAlive );
  +            handler = new AbstractJdbcConnection( conn, keepAlive, keepAliveAge );
           }
   
           final Connection connection = (Connection)Proxy.newProxyInstance(
  
  
  
  1.25      +7 -5      avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/JdbcDataSource.java
  
  Index: JdbcDataSource.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/JdbcDataSource.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- JdbcDataSource.java	20 May 2003 20:44:43 -0000	1.24
  +++ JdbcDataSource.java	28 Jul 2003 18:22:54 -0000	1.25
  @@ -67,7 +67,7 @@
    * <pre>
    *   &lt;jdbc&gt;
    *     &lt;pool-controller min="<i>5</i>" max="<i>10</i>" connection-class="<i>my.overrided.ConnectionClass</i>"&gt;
  - *       &lt;keep-alive disable="false"&gt;select 1&lt;/keep-alive&gt;
  + *       &lt;keep-alive disable="false" age="5000"&gt;select 1&lt;/keep-alive&gt;
    *     &lt;/pool-controller&gt;
    *     &lt;auto-commit&gt;<i>true</i>&lt;/auto-commit&gt;
    *     &lt;driver&gt;<i>com.database.jdbc.JdbcDriver</i>&lt;/driver&gt;
  @@ -97,7 +97,8 @@
    *  of connections.  If a connection has not been used for 5 seconds then before returning the
    *  connection from a call to getConnection(), the connection is first used to ping the database
    *  to make sure that it is still alive.  Setting the <code>disable</code> attribute to true will
  - *  disable this feature.  (Defaults to a query of "SELECT 1" and being enabled)</li>
  + *  disable this feature.  Setting the <code>age</code> allows the 5 second age to be overridden.
  + *  (Defaults to a query of "SELECT 1" and being enabled)</li>
    *
    * <li>The <code>auto-commit</code> element is used to override the default (<code>true</code>)
    *  value of the auto-commit policy.  It ensures that the database connection that is returned
  @@ -149,6 +150,7 @@
               final String user = configuration.getChild( "user" ).getValue( null );
               final String passwd = configuration.getChild( "password" ).getValue( null );
               final Configuration controller = configuration.getChild( "pool-controller" );
  +            final int keepAliveAge = controller.getChild( "keep-alive" ).getAttributeAsInteger( "age", 5000 );
               String keepAlive = controller.getChild( "keep-alive" ).getValue( "SELECT 1" );
               final boolean disableKeepAlive = controller.getChild( "keep-alive" ).getAttributeAsBoolean( "disable", false );
   
  @@ -246,8 +248,8 @@
                   }
               }
   
  -            final JdbcConnectionFactory factory =
  -                new JdbcConnectionFactory( dburl, user, passwd, autoCommit, keepAlive, connectionClass );
  +            final JdbcConnectionFactory factory = new JdbcConnectionFactory(
  +                dburl, user, passwd, autoCommit, keepAlive, keepAliveAge, connectionClass );
               final DefaultPoolController poolController = new DefaultPoolController( l_max / 4 );
   
               factory.enableLogging( getLogger() );
  
  
  
  1.9       +6 -5      avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/ResourceLimitingJdbcDataSource.java
  
  Index: ResourceLimitingJdbcDataSource.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/ResourceLimitingJdbcDataSource.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ResourceLimitingJdbcDataSource.java	20 May 2003 20:44:43 -0000	1.8
  +++ ResourceLimitingJdbcDataSource.java	28 Jul 2003 18:22:54 -0000	1.9
  @@ -84,7 +84,7 @@
    *       blocking="<i>true</i>" timeout="<i>-1</i>"
    *       trim-interval="<i>60000</i>" auto-commit="true"
    *       connection-class="<i>my.overrided.ConnectionClass</i>"&gt;
  - *       &lt;keep-alive disable="false"&gt;select 1&lt;/keep-alive&gt;
  + *       &lt;keep-alive disable="false" age="5000"&gt;select 1&lt;/keep-alive&gt;
    *     &lt;/pool-controller&gt;
    *     &lt;driver&gt;<i>com.database.jdbc.JdbcDriver</i>&lt;/driver&gt;
    *     &lt;dburl&gt;<i>jdbc:driver://host/mydb</i>&lt;/dburl&gt;
  @@ -156,8 +156,8 @@
    * connection from a call to getConnection(), the connection is first
    * used to ping the database to make sure that it is still alive.
    * Setting the <code>disable</code> attribute to true will disable
  - * this feature.  (Defaults to a query of "SELECT 1" and being
  - * enabled)</li>
  + * this feature.  Setting the <code>age</code> allows the 5 second age to
  + * be overridden.  (Defaults to a query of "SELECT 1" and being enabled)</li>
    *
    * <li>The <code>driver</code> element is used to specify the driver
    * to use when connecting to the database.  The specified class must
  @@ -267,6 +267,7 @@
           final String passwd = configuration.getChild( "password" ).getValue( null );
   
           final Configuration controller = configuration.getChild( "pool-controller" );
  +        final int keepAliveAge = controller.getChild( "keep-alive" ).getAttributeAsInteger( "age", 5000 );
           String keepAlive = controller.getChild( "keep-alive" ).getValue( "SELECT 1" );
           final boolean disableKeepAlive =
               controller.getChild( "keep-alive" ).getAttributeAsBoolean( "disable", false );
  @@ -342,7 +343,7 @@
           }
   
           final JdbcConnectionFactory factory = new JdbcConnectionFactory
  -            ( dburl, user, passwd, autoCommit, keepAlive, connectionClass );
  +            ( dburl, user, passwd, autoCommit, keepAlive, keepAliveAge, connectionClass );
   
           factory.enableLogging( getLogger() );
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org