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 ar...@apache.org on 2002/11/28 23:21:05 UTC

cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer StatementManager.java

arminw      2002/11/28 14:21:05

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        StatementManager.java
  Log:
  change exception handling
  
  Revision  Changes    Path
  1.14      +196 -183  jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/StatementManager.java
  
  Index: StatementManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/StatementManager.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- StatementManager.java	8 Nov 2002 16:44:21 -0000	1.13
  +++ StatementManager.java	28 Nov 2002 22:21:05 -0000	1.14
  @@ -54,12 +54,6 @@
    * <http://www.apache.org/>.
    */
   
  -import java.sql.*;
  -import java.util.Collection;
  -import java.util.Enumeration;
  -import java.util.HashMap;
  -import java.util.Iterator;
  -
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
  @@ -70,10 +64,29 @@
   import org.apache.ojb.broker.platforms.Platform;
   import org.apache.ojb.broker.platforms.PlatformException;
   import org.apache.ojb.broker.platforms.PlatformFactory;
  -import org.apache.ojb.broker.query.*;
  +import org.apache.ojb.broker.query.BetweenCriteria;
  +import org.apache.ojb.broker.query.ColumnCriteria;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.ExistsCriteria;
  +import org.apache.ojb.broker.query.FieldCriteria;
  +import org.apache.ojb.broker.query.InCriteria;
  +import org.apache.ojb.broker.query.NullCriteria;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.SelectionCriteria;
  +import org.apache.ojb.broker.query.SqlCriteria;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  +import java.sql.PreparedStatement;
  +import java.sql.ResultSet;
  +import java.sql.SQLException;
  +import java.sql.Statement;
  +import java.sql.Types;
  +import java.util.Collection;
  +import java.util.Enumeration;
  +import java.util.HashMap;
  +import java.util.Iterator;
  +
   /**
    * manages JDBC Connection and Statement ressources.
    *
  @@ -115,44 +128,45 @@
               throws PersistenceBrokerException
       {
           StatementsForClassIF sfc = (StatementsForClassIF) statementTable.get(cds);
  -		if (sfc == null)
  -		{
  -			synchronized (statementTable)
  +        if (sfc == null)
  +        {
  +            synchronized (statementTable)
               {
                   sfc = (StatementsForClassIF) new StatementsForClassImpl(broker, cds);
                   statementTable.put(cds, sfc);
               }
  -		}
  -		return sfc;
  +        }
  +        return sfc;
       }
   
       public void closeResources(Statement stmt, ResultSet rs)
       {
  -        if(log.isDebugEnabled()) log.debug("closeResources was called");
  +        if (log.isDebugEnabled()) log.debug("closeResources was called");
           try
  -		{
  +        {
               platform.beforeStatementClose(stmt, rs);
               //close statement on wrapped statement class, or real statement
  -            if(stmt != null)
  +            if (stmt != null)
               {
                   //log.info("## close: "+stmt);
                   stmt.close();
                   if (m_eagerRelease)
                   {
  -               		broker.getConnectionManager().releaseConnection();
  +                    broker.getConnectionManager().releaseConnection();
                   }
   
               }
  -            platform.afterStatementClose(stmt,rs);
  -		}
  -		catch (SQLException ignored)
  -		{
  -			log.error("Statement closing failed", ignored);
  -		}
  -        catch(PlatformException e)
  +            platform.afterStatementClose(stmt, rs);
  +        }
  +        catch (PlatformException e)
           {
               log.error("Platform dependent operation failed", e);
           }
  +        catch (SQLException ignored)
  +        {
  +            if (log.isDebugEnabled()) log.debug("Statement closing failed", ignored);
  +        }
  +
       }
   
       /**
  @@ -172,30 +186,30 @@
       /**
        * binds the objects primary key and locking values to the statement, BRJ
        */
  -	public void bindDelete(PreparedStatement stmt, ClassDescriptor cld, Object obj) throws java.sql.SQLException
  -	{
  -		Platform pf = PlatformFactory.getPlatformFor(cld.getConnectionDescriptor());
  -		int index = 1;
  -		Object[] values, currentLockingValues;
  -
  -		currentLockingValues = cld.getCurrentLockingValues(obj);
  -
  -		// parameters for WHERE-clause pk
  -		values = cld.getKeyValues(obj);
  -		for (int i = 0; i < values.length; i++)
  -		{
  -			pf.setObjectForStatement(stmt, index, values[i], JdbcAccess.getSqlTypePk(cld, i));
  -			index++;
  -		}
  -
  -		// parameters for WHERE-clause locking
  -		values = currentLockingValues;
  -		for (int i = 0; i < values.length; i++)
  -		{
  -			pf.setObjectForStatement(stmt, index, values[i], JdbcAccess.getSqlTypeLocking(cld, i));
  -			index++;
  -		}
  -	}
  +    public void bindDelete(PreparedStatement stmt, ClassDescriptor cld, Object obj) throws java.sql.SQLException
  +    {
  +        Platform pf = PlatformFactory.getPlatformFor(cld.getConnectionDescriptor());
  +        int index = 1;
  +        Object[] values, currentLockingValues;
  +
  +        currentLockingValues = cld.getCurrentLockingValues(obj);
  +
  +        // parameters for WHERE-clause pk
  +        values = cld.getKeyValues(obj);
  +        for (int i = 0; i < values.length; i++)
  +        {
  +            pf.setObjectForStatement(stmt, index, values[i], JdbcAccess.getSqlTypePk(cld, i));
  +            index++;
  +        }
  +
  +        // parameters for WHERE-clause locking
  +        values = currentLockingValues;
  +        for (int i = 0; i < values.length; i++)
  +        {
  +            pf.setObjectForStatement(stmt, index, values[i], JdbcAccess.getSqlTypeLocking(cld, i));
  +            index++;
  +        }
  +    }
   
       /**
        * bind a value
  @@ -206,30 +220,30 @@
        * @param cld the ClassDescriptor
        * @return next index for PreparedStatement
        */
  -	private int bindStatementValue(
  -		PreparedStatement stmt,
  -		int index,
  -		Object value,
  -		ClassDescriptor cld,
  -		FieldDescriptor fld)
  -		throws SQLException
  -	{
  -		// if value is a subQuery bind it
  -		if (value instanceof Query)
  -		{
  -			Query subQuery = (Query) value;
  -			if (subQuery.getCriteria() != null && !subQuery.getCriteria().isEmpty())
  -			{
  -				return bindStatement(
  -					stmt,
  -					subQuery.getCriteria(),
  -					cld.getRepository().getDescriptorFor(subQuery.getSearchClass()),
  -					index);
  -			}
  -			return index;
  -		}
  +    private int bindStatementValue(
  +            PreparedStatement stmt,
  +            int index,
  +            Object value,
  +            ClassDescriptor cld,
  +            FieldDescriptor fld)
  +            throws SQLException
  +    {
  +        // if value is a subQuery bind it
  +        if (value instanceof Query)
  +        {
  +            Query subQuery = (Query) value;
  +            if (subQuery.getCriteria() != null && !subQuery.getCriteria().isEmpty())
  +            {
  +                return bindStatement(
  +                        stmt,
  +                        subQuery.getCriteria(),
  +                        cld.getRepository().getDescriptorFor(subQuery.getSearchClass()),
  +                        index);
  +            }
  +            return index;
  +        }
   
  -		// if query has criteria, bind them
  +        // if query has criteria, bind them
           if (fld != null)
           {
               // BRJ: use field conversions and platform
  @@ -237,10 +251,10 @@
               if (value != null)
               {
                   pf.setObjectForStatement(
  -                    stmt,
  -                    index,
  -                    fld.getFieldConversion().javaToSql(value),
  -                    fld.getColumnJdbcType());
  +                        stmt,
  +                        index,
  +                        fld.getFieldConversion().javaToSql(value),
  +                        fld.getColumnJdbcType());
               }
               else
               {
  @@ -256,12 +270,12 @@
               }
               else
               {
  -                stmt.setNull(index,Types.NULL);
  +                stmt.setNull(index, Types.NULL);
               }
           }
   
  -		return ++index; // increment before return
  -	}
  +        return ++index; // increment before return
  +    }
   
       /**
        * bind SelectionCriteria
  @@ -272,7 +286,7 @@
        * @return next index for PreparedStatement
        */
       private int bindStatement(PreparedStatement stmt, int index, SelectionCriteria crit, ClassDescriptor cld)
  -        throws SQLException
  +            throws SQLException
       {
           FieldDescriptor fld = cld.getFieldDescriptorForPath(crit.getAttribute());
   
  @@ -299,7 +313,7 @@
        * @return next index for PreparedStatement
        */
       private int bindStatement(PreparedStatement stmt, int index, FieldCriteria crit)
  -        throws SQLException
  +            throws SQLException
       {
           return index;
       }
  @@ -312,7 +326,7 @@
        * @return next index for PreparedStatement
        */
       private int bindStatement(PreparedStatement stmt, int index, ColumnCriteria crit)
  -        throws SQLException
  +            throws SQLException
       {
           return index;
       }
  @@ -338,11 +352,11 @@
        * @return next index for PreparedStatement
        */
       private int bindStatement(PreparedStatement stmt, int index, BetweenCriteria crit, ClassDescriptor cld)
  -        throws SQLException
  +            throws SQLException
       {
           FieldDescriptor fld = cld.getFieldDescriptorForPath(crit.getAttribute());
   
  -        index = bindStatementValue(stmt, index, crit.getValue(), cld , fld);
  +        index = bindStatementValue(stmt, index, crit.getValue(), cld, fld);
           return bindStatementValue(stmt, index, crit.getValue2(), cld, fld);
       }
   
  @@ -354,26 +368,26 @@
        * @param cld the ClassDescriptor
        * @return next index for PreparedStatement
        */
  -	private int bindStatement(PreparedStatement stmt, int index, InCriteria crit, ClassDescriptor cld) throws SQLException
  -	{
  -		FieldDescriptor fld = cld.getFieldDescriptorForPath(crit.getAttribute());
  -
  -		if (crit.getValue() instanceof Collection)
  -		{
  -			Collection values = (Collection) crit.getValue();
  -			Iterator iter = values.iterator();
  -
  -			while (iter.hasNext())
  -			{
  -				index = bindStatementValue(stmt, index, iter.next(), cld, fld);
  -			}
  -		}
  -		else
  -		{
  -			index = bindStatementValue(stmt, index, crit.getValue(), cld, fld);
  -		}	
  -		return index;
  -	}
  +    private int bindStatement(PreparedStatement stmt, int index, InCriteria crit, ClassDescriptor cld) throws SQLException
  +    {
  +        FieldDescriptor fld = cld.getFieldDescriptorForPath(crit.getAttribute());
  +
  +        if (crit.getValue() instanceof Collection)
  +        {
  +            Collection values = (Collection) crit.getValue();
  +            Iterator iter = values.iterator();
  +
  +            while (iter.hasNext())
  +            {
  +                index = bindStatementValue(stmt, index, iter.next(), cld, fld);
  +            }
  +        }
  +        else
  +        {
  +            index = bindStatementValue(stmt, index, crit.getValue(), cld, fld);
  +        }
  +        return index;
  +    }
   
       /**
        * bind ExistsCriteria
  @@ -384,11 +398,11 @@
        * @return next index for PreparedStatement
        */
       private int bindStatement(
  -        PreparedStatement stmt,
  -        int index,
  -        ExistsCriteria crit,
  -        ClassDescriptor cld)
  -        throws SQLException
  +            PreparedStatement stmt,
  +            int index,
  +            ExistsCriteria crit,
  +            ClassDescriptor cld)
  +            throws SQLException
       {
           Query subQuery = (Query) crit.getValue();
   
  @@ -396,10 +410,10 @@
           if (subQuery.getCriteria() != null && !subQuery.getCriteria().isEmpty())
           {
               return bindStatement(
  -                stmt,
  -                subQuery.getCriteria(),
  -                cld.getRepository().getDescriptorFor(subQuery.getSearchClass()),
  -                index);
  +                    stmt,
  +                    subQuery.getCriteria(),
  +                    cld.getRepository().getDescriptorFor(subQuery.getSearchClass()),
  +                    index);
   
               // otherwise, just ignore it
           }
  @@ -413,7 +427,7 @@
        * bind a Query based Select Statement
        */
       public int bindStatement(PreparedStatement stmt, Criteria crit, ClassDescriptor cld, int param)
  -        throws SQLException
  +            throws SQLException
       {
           if (crit != null)
           {
  @@ -456,30 +470,29 @@
       /**
        * binds the values of the object obj to the statements parameters
        */
  -	public void bindInsert(PreparedStatement stmt, ClassDescriptor cld, Object obj)
  -		throws java.sql.SQLException
  -	{
  -		Platform pf = PlatformFactory.getPlatformFor(cld.getConnectionDescriptor());
  -
  -		Object[] values;
  -		cld.updateLockingValues(obj); // BRJ : provide useful defaults for locking fields
  -
  -		values = cld.getAllValues(obj);
  -		for (int i = 0; i < values.length; i++)
  -		{
  -			Object val = values[i];
  -			if (val != null)
  -			{
  -				pf.setObjectForStatement(stmt, i + 1, val, JdbcAccess.getSqlTypeAll(cld, i));
  -			}
  -			else
  -			{
  -				pf.setNullForStatement(stmt, i + 1, JdbcAccess.getSqlTypeAll(cld, i));
  -			}
  +    public void bindInsert(PreparedStatement stmt, ClassDescriptor cld, Object obj)
  +            throws java.sql.SQLException
  +    {
  +        Platform pf = PlatformFactory.getPlatformFor(cld.getConnectionDescriptor());
   
  -		}
  -	}
  +        Object[] values;
  +        cld.updateLockingValues(obj); // BRJ : provide useful defaults for locking fields
   
  +        values = cld.getAllValues(obj);
  +        for (int i = 0; i < values.length; i++)
  +        {
  +            Object val = values[i];
  +            if (val != null)
  +            {
  +                pf.setObjectForStatement(stmt, i + 1, val, JdbcAccess.getSqlTypeAll(cld, i));
  +            }
  +            else
  +            {
  +                pf.setNullForStatement(stmt, i + 1, JdbcAccess.getSqlTypeAll(cld, i));
  +            }
  +
  +        }
  +    }
   
   
       /**
  @@ -497,20 +510,20 @@
               for (i = 0; i < values.length; i++)
               {
                   Object val = values[i];
  -            	if (val != null)
  -            	{
  +                if (val != null)
  +                {
                       pf.setObjectForStatement(stmt, i + 1, val, JdbcAccess.getSqlTypePk(cld, i));
                   }
  -            	else
  -            	{
  +                else
  +                {
                       pf.setNullForStatement(stmt, i + 1, JdbcAccess.getSqlTypePk(cld, i));
  -            	}
  +                }
               }
           }
           catch (SQLException e)
           {
               LoggerFactory.getDefaultLogger().error(
  -                "bindSelect failed for: "
  +                    "bindSelect failed for: "
                       + oid.toString()
                       + ", PK: "
                       + i
  @@ -524,9 +537,9 @@
        * binds the values of the object obj to the statements parameters
        */
       public void bindUpdate(PreparedStatement stmt, ClassDescriptor cld, Object obj)
  -        throws java.sql.SQLException
  +            throws java.sql.SQLException
       {
  -		Platform pf = PlatformFactory.getPlatformFor(cld.getConnectionDescriptor());
  +        Platform pf = PlatformFactory.getPlatformFor(cld.getConnectionDescriptor());
           int index = 1;
           Object[] values, currentLockingValues;
   
  @@ -539,7 +552,7 @@
           {
               Object val = values[i];
               if (val != null)
  -                pf.setObjectForStatement(stmt,index, val, JdbcAccess.getSqlTypeNonPk(cld, i));
  +                pf.setObjectForStatement(stmt, index, val, JdbcAccess.getSqlTypeNonPk(cld, i));
               else
                   pf.setNullForStatement(stmt, index, JdbcAccess.getSqlTypeNonPk(cld, i));
               index++;
  @@ -574,7 +587,7 @@
        * return a prepared DELETE Statement fitting for the given ClassDescriptor
        */
       public PreparedStatement getDeleteStatement(ClassDescriptor cds)
  -        throws PersistenceBrokerSQLException
  +            throws PersistenceBrokerSQLException
       {
           try
           {
  @@ -598,7 +611,7 @@
        * return a prepared Insert Statement fitting for the given ClassDescriptor
        */
       public PreparedStatement getInsertStatement(ClassDescriptor cds)
  -        throws PersistenceBrokerSQLException
  +            throws PersistenceBrokerSQLException
       {
           try
           {
  @@ -614,7 +627,7 @@
        * return a generic Statement for the given ClassDescriptor
        */
       public PreparedStatement getPreparedStatement(ClassDescriptor cds, String sql, boolean scrollable)
  -        throws PersistenceBrokerException
  +            throws PersistenceBrokerException
       {
           return getStatementsForClass(cds).getPreparedStmt(sql, scrollable);
       }
  @@ -623,7 +636,7 @@
        * return a prepared Select Statement for the given ClassDescriptor
        */
       public PreparedStatement getSelectByPKStatement(ClassDescriptor cds)
  -        throws PersistenceBrokerSQLException
  +            throws PersistenceBrokerSQLException
       {
           try
           {
  @@ -639,7 +652,7 @@
        * return a prepared Update Statement fitting to the given ClassDescriptor
        */
       public PreparedStatement getUpdateStatement(ClassDescriptor cds)
  -        throws PersistenceBrokerSQLException
  +            throws PersistenceBrokerSQLException
       {
           try
           {
  @@ -651,40 +664,40 @@
           }
       }
   
  -     /**
  -      * MBAIRD: I would prefer to remove statement caching completely, but instead we
  -      * deal with edge cases.
  -      */
  -	private void closeIfNotCached(Statement stmt)
  -	{
  -	     boolean doClose = true;
  -	     synchronized (statementTable)
  -	     {
  -		     for (Iterator it = statementTable.values().iterator();it.hasNext();)
  -		     {
  -		         StatementsForClassIF sfc = (StatementsForClassIF) it.next();
  -		         if (sfc != null)
  -		         {
  -		             if (sfc.isCached(stmt))
  -		             {
  -		                 doClose = false;
  -		                 break;
  -		             }
  -		         }
  -		     }
  -	     }
  -	     if (doClose)
  -	     {
  -	         try
  -	         {
  -	             stmt.close();
  -	         }
  -	         catch(SQLException ignored)
  -	         {
  -	             LoggerFactory.getDefaultLogger().warn("closing statement had error: "+ ignored.getMessage());
  -	         }
  -	     }
  -	 }
  +    /**
  +     * MBAIRD: I would prefer to remove statement caching completely, but instead we
  +     * deal with edge cases.
  +     */
  +    private void closeIfNotCached(Statement stmt)
  +    {
  +        boolean doClose = true;
  +        synchronized (statementTable)
  +        {
  +            for (Iterator it = statementTable.values().iterator(); it.hasNext();)
  +            {
  +                StatementsForClassIF sfc = (StatementsForClassIF) it.next();
  +                if (sfc != null)
  +                {
  +                    if (sfc.isCached(stmt))
  +                    {
  +                        doClose = false;
  +                        break;
  +                    }
  +                }
  +            }
  +        }
  +        if (doClose)
  +        {
  +            try
  +            {
  +                stmt.close();
  +            }
  +            catch (SQLException ignored)
  +            {
  +                LoggerFactory.getDefaultLogger().warn("closing statement had error: " + ignored.getMessage());
  +            }
  +        }
  +    }
   
   
   }