You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by mk...@apache.org on 2005/03/16 20:08:34 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer ConnectionFactoryAbstractImpl.java ConnectionFactoryDBCPImpl.java ConnectionFactoryPooledImpl.java

mkalen      2005/03/16 11:08:34

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        ConnectionFactoryAbstractImpl.java
                        ConnectionFactoryDBCPImpl.java
                        ConnectionFactoryPooledImpl.java
  Log:
  Merge with OJB_1_0_RELEASE branch: use PreparedStatement for validation query in PooledImpl. Make sure DBCPImpl calls platform-specific initializeJdbcConnection. Do not cache DBCP statements if using Oracle9i. Some spelling corrections.
  
  Revision  Changes    Path
  1.15      +8 -6      db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionFactoryAbstractImpl.java
  
  Index: ConnectionFactoryAbstractImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionFactoryAbstractImpl.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ConnectionFactoryAbstractImpl.java	7 Jan 2005 23:02:12 -0000	1.14
  +++ ConnectionFactoryAbstractImpl.java	16 Mar 2005 19:08:34 -0000	1.15
  @@ -1,6 +1,6 @@
   package org.apache.ojb.broker.accesslayer;
   
  -/* Copyright 2002-2004 The Apache Software Foundation
  +/* Copyright 2002-2005 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -138,16 +138,17 @@
               {
                   if (conn == null || conn.isClosed())
                   {
  -                    log.error("Connection for JdbcConnectionDiscriptor (" +
  +                    log.error("Connection for JdbcConnectionDescriptor (" +
                               (jcd.getDatasourceName() != null ? "datasource: " + jcd.getDatasourceName() :
                               "db-url: " + getDbURL(jcd) + ", user: " + jcd.getUserName()) +
  -                            ") was not valid, either *closed* or *null*");
  +                            ") is invalid: " +
  +                            (conn != null ? "*closed*" : "*null*"));
                       throw new LookupException("Could not lookup valid connection from pool/DB, connection was "+conn);
                   }
               }
               catch (SQLException e)
               {
  -                log.error("Error during sanity check of new DB Connection, either it was closed", e);
  +                log.error("Error during sanity check of new DB Connection", e);
                   throw new LookupException("Connection check failed", e);
               }
           }
  @@ -179,7 +180,7 @@
       }
   
       /**
  -     * Override this method to do cleanup in your implenetation.
  +     * Override this method to do cleanup in your implementation.
        * Do a <tt>super.releaseAllResources()</tt> in your method implementation
        * to free resources used by this class.
        */
  @@ -309,4 +310,5 @@
           return jcd.isDataSource() ? jcd.getDatasourceName() :
                   jcd.getProtocol() + ":" + jcd.getSubProtocol() + ":" + jcd.getDbAlias();
       }
  +
   }
  
  
  
  1.15      +93 -19    db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionFactoryDBCPImpl.java
  
  Index: ConnectionFactoryDBCPImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionFactoryDBCPImpl.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ConnectionFactoryDBCPImpl.java	18 Dec 2004 13:32:10 -0000	1.14
  +++ ConnectionFactoryDBCPImpl.java	16 Mar 2005 19:08:34 -0000	1.15
  @@ -1,6 +1,6 @@
   package org.apache.ojb.broker.accesslayer;
   
  -/* Copyright 2002-2004 The Apache Software Foundation
  +/* Copyright 2002-2005 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -25,8 +25,10 @@
   import org.apache.commons.dbcp.PoolingDataSource;
   import org.apache.commons.pool.KeyedObjectPoolFactory;
   import org.apache.commons.pool.ObjectPool;
  +import org.apache.commons.pool.KeyedPoolableObjectFactory;
   import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
   import org.apache.commons.pool.impl.GenericObjectPool;
  +import org.apache.commons.pool.impl.GenericKeyedObjectPool;
   import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
   import org.apache.ojb.broker.util.ClassHelper;
   import org.apache.ojb.broker.util.pooling.PoolConfiguration;
  @@ -44,8 +46,9 @@
    */
   public class ConnectionFactoryDBCPImpl extends ConnectionFactoryAbstractImpl
   {
  +
       private Logger log = LoggerFactory.getLogger(ConnectionFactoryDBCPImpl.class);
  -    private Object dummy = new Object();
  +    private Object poolSynch = new Object();
       private DataSource dataSource;
       private ObjectPool connectionPool;
   
  @@ -61,7 +64,7 @@
   
       public Connection getConnectionFromPool() throws LookupException
       {
  -        synchronized (dummy)
  +        synchronized (poolSynch)
           {
               if(dataSource == null)
               {
  @@ -75,7 +78,7 @@
           }
           catch (SQLException e)
           {
  -            throw new LookupException("Can't get connection from DBCP-DataSource", e);
  +            throw new LookupException("Could not get connection from DBCP DataSource", e);
           }
       }
   
  @@ -83,7 +86,7 @@
       {
           try
           {
  -            // we using datasources, thus close return connection to pool
  +            // We are using datasources, thus close returns connection to pool
               con.close();
           }
           catch (SQLException e)
  @@ -95,7 +98,7 @@
       public synchronized void releaseAllResources()
       {
           log.warn("Clear connection pool");
  -        synchronized (dummy)
  +        synchronized (poolSynch)
           {
               try
               {
  @@ -141,11 +144,6 @@
   
           // First, we'll need a ObjectPool that serves as the
           // actual pool of connections.
  -        //
  -        // We'll use a GenericObjectPool instance, although
  -        // any ObjectPool implementation will suffice.
  -        //
  -        // TODO: make objectPool configurable at runtime?
           connectionPool = createObjectPool(conf);
   
           // Next, we'll create a ConnectionFactory that the
  @@ -154,7 +152,7 @@
           //
           org.apache.commons.dbcp.ConnectionFactory connectionFactory = createConnectionFactory(jcd);
   
  -        KeyedObjectPoolFactory statementPoolFactory = createStatementPoolFactory(null);
  +        KeyedObjectPoolFactory statementPoolFactory = createStatementPoolFactory(jcd);
           // set the validation query
           String validationQuery = jcd.getConnectionFactoryDescriptor().getValidationQuery();
           boolean defaultReadOnly = false;
  @@ -163,7 +161,7 @@
                   false : true;
   
           // Abandoned configuration
  -        AbandonedConfig ac = buildAbandonedCongig(jcd);
  +        AbandonedConfig ac = buildAbandonedConfig(jcd);
   
           //
           // Now we'll create the PoolableConnectionFactory, which wraps
  @@ -196,7 +194,7 @@
           return conf;
       }
   
  -    private AbandonedConfig buildAbandonedCongig(JdbcConnectionDescriptor jcd)
  +    private AbandonedConfig buildAbandonedConfig(JdbcConnectionDescriptor jcd)
       {
           PoolConfiguration poolConf = new PoolConfiguration();
           // read all attributes from descriptor and add them to ..Conf class
  @@ -208,17 +206,47 @@
   
       protected ObjectPool createObjectPool(GenericObjectPool.Config config)
       {
  +        // We'll use a GenericObjectPool instance, although
  +        // any ObjectPool implementation will suffice.
           return new GenericObjectPool(null, config);
       }
   
  -    protected org.apache.commons.dbcp.ConnectionFactory createConnectionFactory(JdbcConnectionDescriptor jcd)
  +    protected KeyedObjectPoolFactory createStatementPoolFactory(JdbcConnectionDescriptor jcd)
  +    {
  +        final String platform = jcd.getDbms();
  +        if (platform.equals("Oracle9i"))
  +        {
  +            // mkalen: let the platform set Oracle-specific statement pooling
  +            return null;
  +        }
  +
  +        final KeyedPoolableObjectFactory objectFactory = getStatementCacheObjectFactory();
  +        final GenericKeyedObjectPool.Config factoryConfig = buildStatementCacheFactoryConfig(jcd);
  +        final KeyedObjectPoolFactory stmtPoolFactory;
  +        stmtPoolFactory = new GenericKeyedObjectPoolFactory(objectFactory, factoryConfig);
  +        return stmtPoolFactory;
  +    }
  +
  +    protected KeyedPoolableObjectFactory getStatementCacheObjectFactory()
       {
  -        return new DriverManagerConnectionFactory(getDbURL(jcd), jcd.getUserName(), jcd.getPassWord());
  +        return null;
       }
   
  -    protected KeyedObjectPoolFactory createStatementPoolFactory(Object obj)
  +    protected GenericKeyedObjectPool.Config buildStatementCacheFactoryConfig(JdbcConnectionDescriptor jcd)
       {
  -        return new GenericKeyedObjectPoolFactory(null);
  +        GenericKeyedObjectPool.Config factoryConfig;
  +
  +        // TODO: mkalen: find a way to specify PreparedStatement pooling params
  +        factoryConfig = new GenericKeyedObjectPool.Config();
  +        /*
  +        // We can not use this as that would set Connection pool params on PS-cache...
  +        PoolConfiguration poolConf = new PoolConfiguration();
  +        // read all attributes from descriptor and add them to ..Conf class
  +        poolConf.putAll(jcd.getConnectionFactoryDescriptor().getAttributes());
  +        poolConf.getKeyedObjectPoolConfig();
  +        factoryConfig = poolConf.getKeyedObjectPoolConfig();
  +        */
  +        return factoryConfig;
       }
   
       protected PoolingDataSource createPoolingDataSource(ObjectPool pool)
  @@ -228,4 +256,50 @@
           ds.setAccessToUnderlyingConnectionAllowed(true);
           return ds;
       }
  +
  +    protected org.apache.commons.dbcp.ConnectionFactory createConnectionFactory(JdbcConnectionDescriptor jcd)
  +    {
  +        return new ConPoolFactory(jcd);
  +    }
  +
  +    //**************************************************************************************
  +    // Inner classes
  +    //************************************************************************************
  +
  +    /**
  +     * Inner class used as factory for connection pooling.
  +     * Adhers to OJB platform specification by calling platform-specific init methods
  +     * on newly created connections.
  +     * @see DriverManagerConnectionFactory
  +     */
  +    class ConPoolFactory extends DriverManagerConnectionFactory
  +    {
  +
  +        private final JdbcConnectionDescriptor jcd;
  +
  +        public ConPoolFactory(JdbcConnectionDescriptor jcd)
  +        {
  +            super(getDbURL(jcd), jcd.getUserName(), jcd.getPassWord());
  +            this.jcd = jcd;
  +        }
  +
  +        public Connection createConnection() throws SQLException
  +        {
  +            final Connection conn = super.createConnection();
  +            if (conn != null)
  +            {
  +                try
  +                {
  +                    initializeJdbcConnection(conn, jcd);
  +                }
  +                catch (LookupException e)
  +                {
  +                    log.error("Platform dependent initialization of connection failed", e);
  +                }
  +            }
  +            return conn;
  +        }
  +
  +    }
  +
   }
  
  
  
  1.21      +12 -9     db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionFactoryPooledImpl.java
  
  Index: ConnectionFactoryPooledImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionFactoryPooledImpl.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- ConnectionFactoryPooledImpl.java	17 Feb 2005 13:49:02 -0000	1.20
  +++ ConnectionFactoryPooledImpl.java	16 Mar 2005 19:08:34 -0000	1.21
  @@ -1,6 +1,6 @@
   package org.apache.ojb.broker.accesslayer;
   
  -/* Copyright 2002-2004 The Apache Software Foundation
  +/* Copyright 2002-2005 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -18,7 +18,7 @@
   import java.sql.Connection;
   import java.sql.ResultSet;
   import java.sql.SQLException;
  -import java.sql.Statement;
  +import java.sql.PreparedStatement;
   
   import org.apache.commons.pool.BasePoolableObjectFactory;
   import org.apache.commons.pool.ObjectPool;
  @@ -135,7 +135,7 @@
   
       /**
        * Inner class - {@link org.apache.commons.pool.PoolableObjectFactory}
  -     * used as factory for connection pooling
  +     * used as factory for connection pooling.
        */
       class ConPoolFactory extends BasePoolableObjectFactory
       {
  @@ -159,16 +159,18 @@
                   if (log.isDebugEnabled()) log.debug(e);
                   isValid = false;
               }
  -            if(isValid && getJcd().getConnectionFactoryDescriptor().getValidationQuery() != null)
  +            final String validationQuery;
  +            validationQuery = getJcd().getConnectionFactoryDescriptor().getValidationQuery();
  +            if (isValid && validationQuery != null)
               {
  -                isValid = validateConnection(con , getJcd().getConnectionFactoryDescriptor().getValidationQuery());
  +                isValid = validateConnection(con, validationQuery);
               }
               return isValid;
           }
   
           private boolean validateConnection(Connection conn, String query)
           {
  -            Statement stmt = null;
  +            PreparedStatement stmt = null;
               ResultSet rset = null;
               boolean isValid = false;
               if (failedValidationQuery > 100)
  @@ -179,8 +181,8 @@
               }
               try
               {
  -                stmt = conn.createStatement();
  -                rset = stmt.executeQuery(query);
  +                stmt = conn.prepareStatement(query);
  +                rset = stmt.executeQuery();
                   if (rset.next())
                   {
                       failedValidationQuery = 0;
  @@ -243,4 +245,5 @@
               }
           }
       }
  +
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org