db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/util/pooling PoolConfiguration.java
Date Thu, 17 Feb 2005 13:47:18 GMT
arminw      2005/02/17 05:47:18

  Modified:    src/java/org/apache/ojb/broker/accesslayer Tag:
                        OJB_1_0_RELEASE ConnectionFactoryPooledImpl.java
               src/java/org/apache/ojb/broker/util/pooling Tag:
                        OJB_1_0_RELEASE PoolConfiguration.java
  Log:
  add patch by Ilkka Priha
  </snip>
  We noticed that under heavy load (lack of memory) OJB returns closed Connections into the
pool. This happens when the finalizer activates the finalize method of PersistenceBroker instances.
At least MySQL Connections are already closed at this point (their finalize called as well?).
Closed Connections cause a lot of trouble later on when borrowed back to use from the pool.
  <snip>
  ConnectionFactoryPooledImpl now always check for closed connections when connection was
returned to pool and validation of connection was improved.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.15.2.1  +29 -20    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.15
  retrieving revision 1.15.2.1
  diff -u -r1.15 -r1.15.2.1
  --- ConnectionFactoryPooledImpl.java	11 Jun 2004 18:49:09 -0000	1.15
  +++ ConnectionFactoryPooledImpl.java	17 Feb 2005 13:47:18 -0000	1.15.2.1
  @@ -56,7 +56,17 @@
       {
           try
           {
  -            ((ObjectPool) this.connectionPools.get(jcd.getPBKey())).returnObject(con);
  +            /**
  +             * Patch by Ilkka Priha
  +             * To avoid problems with closed connections we only return unclosed
  +             * connections to pool. We do this test independent from the from the
  +             * commons-pool settings, which also supports validation on return of
  +             * a connecion.
  +             */
  +            if(!con.isClosed())
  +            {
  +                ((ObjectPool) this.connectionPools.get(jcd.getPBKey())).returnObject(con);
  +            }
           }
           catch (Exception e)
           {
  @@ -149,24 +159,22 @@
           public boolean validateObject(Object obj)
           {
               Connection con = (Connection) obj;
  -            String query = jcd.getConnectionPoolDescriptor().getValidationQuery();
  -            if (query == null || query.trim().equals(""))
  +            boolean isValid = false;
  +            try
               {
  -                try
  -                {
  -                    return !con.isClosed();
  -                }
  -                catch (SQLException e)
  -                {
  -                    log.warn("Connection validation failed: " + e.getMessage());
  -                    if (log.isDebugEnabled()) log.debug(e);
  -                    return false;
  -                }
  +                isValid = !con.isClosed();
               }
  -            else
  +            catch (SQLException e)
               {
  -                return validateConnection(con, query);
  +                log.warn("Connection validation failed: " + e.getMessage());
  +                if (log.isDebugEnabled()) log.debug(e);
  +                isValid = false;
               }
  +            if(isValid && jcd.getConnectionPoolDescriptor().getValidationQuery()
!= null)
  +            {
  +                isValid = validateConnection(con , jcd.getConnectionPoolDescriptor().getValidationQuery());
  +            }
  +            return isValid;
           }
   
           private boolean validateConnection(Connection conn, String query)
  @@ -176,8 +184,9 @@
               boolean isValid = false;
               if(failedValidationQuery > 100)
               {
  -                throw new OJBRuntimeException("Validation of connection "+conn+" using
validation query "+
  -                        query + " failed more than 100 times.");
  +                --failedValidationQuery;
  +                throw new OJBRuntimeException("Validation of connection "+conn+" using
validation query '"+
  +                        query + "' failed more than 100 times.");
               }
               try
               {
  @@ -199,8 +208,8 @@
               catch (SQLException e)
               {
                   ++failedValidationQuery;
  -                log.warn("Validation query for connection failed, discard connection. Query
was " +
  -                        query + ", Message was " + e.getMessage());
  +                log.warn("Validation query for connection failed, discard connection. Query
was '" +
  +                        query + "', Message was " + e.getMessage());
                   if (log.isDebugEnabled()) log.debug(e);
               }
               finally
  
  
  
  No                   revision
  No                   revision
  1.11.2.1  +5 -3      db-ojb/src/java/org/apache/ojb/broker/util/pooling/PoolConfiguration.java
  
  Index: PoolConfiguration.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/pooling/PoolConfiguration.java,v
  retrieving revision 1.11
  retrieving revision 1.11.2.1
  diff -u -r1.11 -r1.11.2.1
  --- PoolConfiguration.java	23 Jun 2004 12:34:12 -0000	1.11
  +++ PoolConfiguration.java	17 Feb 2005 13:47:18 -0000	1.11.2.1
  @@ -21,6 +21,7 @@
   import org.apache.commons.dbcp.AbandonedConfig;
   import org.apache.commons.pool.impl.GenericKeyedObjectPool;
   import org.apache.commons.pool.impl.GenericObjectPool;
  +import org.apache.commons.lang.StringUtils;
   
   
   /**
  @@ -178,12 +179,13 @@
   
       public String getValidationQuery()
       {
  -        return getProperty(VALIDATION_QUERY);
  +        String result = getProperty(VALIDATION_QUERY);
  +        return StringUtils.isEmpty(result) ? null : result;
       }
   
       public void setValidationQuery(String validationQuery)
       {
  -        if(validationQuery != null) setProperty(VALIDATION_QUERY, validationQuery);
  +        setProperty(VALIDATION_QUERY, validationQuery);
       }
   
       public int getMaxActive()
  
  
  

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


Mime
View raw message