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 ma...@apache.org on 2003/07/29 18:54:30 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/util/batch PreparedStatementInvocationHandler.java

mattbaird    2003/07/29 09:54:30

  Modified:    src/java/org/apache/ojb/broker/util/batch
                        PreparedStatementInvocationHandler.java
  Log:
  fix problem with Method being wrong.
  
  Revision  Changes    Path
  1.7       +138 -100  db-ojb/src/java/org/apache/ojb/broker/util/batch/PreparedStatementInvocationHandler.java
  
  Index: PreparedStatementInvocationHandler.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/batch/PreparedStatementInvocationHandler.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PreparedStatementInvocationHandler.java	26 Jun 2003 19:07:50 -0000	1.6
  +++ PreparedStatementInvocationHandler.java	29 Jul 2003 16:54:29 -0000	1.7
  @@ -99,9 +99,9 @@
           try
           {
               setBigDecimal = PreparedStatement.class.getMethod("setBigDecimal",
  -                    new Class[] {Integer.TYPE, BigDecimal.class});
  +                                                              new Class[] {Integer.TYPE, BigDecimal.class});
           }
  -        catch (Exception ex)
  +        catch ( Exception ex )
           {
           }
           SET_BIG_DECIMAL = setBigDecimal;
  @@ -115,43 +115,49 @@
   
       private ArrayList _params = new ArrayList();
   
  -	private Platform m_platform = null;
  +    private Platform m_platform = null;
   
       public PreparedStatementInvocationHandler(BatchConnection batchConn, String sql, JdbcConnectionDescriptor jcd)
       {
           _batchConn = batchConn;
           _sql = sql;
  -		m_platform = PlatformFactory.getPlatformFor(jcd);
  -		try
  -		{
  -			ADD_BATCH = m_platform.getClass().getMethod("addBatch",new Class[]{PreparedStatement.class});
  -		}
  -		catch (NoSuchMethodException e)
  -		{
  -			/**
  -			 * should never happen
  -			 */
  -			ADD_BATCH = null;
  -		}
  -		catch (SecurityException e)
  -		{
  -		}
  -	}
  +        m_platform = PlatformFactory.getPlatformFor(jcd);
  +        try
  +        {
  +            ADD_BATCH = m_platform.getClass().getMethod("addBatch",new Class[]{PreparedStatement.class});
  +        }
  +        catch ( NoSuchMethodException e )
  +        {
  +            /**
  +             * should never happen
  +             */
  +            ADD_BATCH = null;
  +        }
  +        catch ( SecurityException e )
  +        {
  +        }
  +    }
   
       public Object invoke(Object proxy, Method method, Object[] args)
  -            throws Throwable
  +    throws Throwable
       {
           String name = method.getName();
  -        if (name.equals("executeUpdate")) {
  +        if ( name.equals("executeUpdate") )
  +        {
               _methods.add(ADD_BATCH);
               _params.add(null);
               _batchConn.nextExecuted(_sql);
               return ONE;
  -        } else if (name.equals("doExecute")) {
  +        }
  +        else if ( name.equals("doExecute") )
  +        {
               doExecute((Connection) args[0]);
  -        } else if (name.startsWith("set")) {
  +        }
  +        else if ( name.startsWith("set") )
  +        {
               // workaround for the bug in Sybase jConnect JDBC driver
  -            if (name.equals("setLong")) {
  +            if ( name.equals("setLong") )
  +            {
                   method = SET_BIG_DECIMAL;
                   args[1] = BigDecimal.valueOf(((Long) args[1]).longValue());
               }
  @@ -170,101 +176,112 @@
           int size;
   
           size = _methods.size();
  -        if (size == 0)
  +        if ( size == 0 )
           {
               return;
           }
           stmt = conn.prepareStatement(_sql);
  -		try
  -		{
  -			m_platform.afterStatementCreate(stmt);
  -		}
  -		catch (PlatformException e)
  -		{
  -			if (e.getCause() instanceof SQLException)
  -			{
  -				throw (SQLException)e.getCause();
  -			}
  -			else
  -			{
  -				throw new SQLException(e.getMessage());
  -			}
  -		}
  -		try
  -		{
  -			m_platform.beforeBatch(stmt);
  -		}
  -		catch (PlatformException e)
  -		{
  -			if (e.getCause() instanceof SQLException)
  -			{
  -				throw (SQLException)e.getCause();
  -			}
  -			else
  -			{
  -				throw new SQLException(e.getMessage());
  -			}
  -		}
  -		try
  -        {
  -            for (int i = 0; i < size; i++)
  -            {
  -                try 
  -                {
  -					if ((Method) _methods.get(i) == ADD_BATCH )
  -					{
  -						/**
  -						 * we invoke on the platform and pass the stmt as an arg.
  -						 */
  -						((Method) _methods.get(i)).invoke(m_platform, new Object[] {stmt});
  -					}
  -					else
  -					{
  -						((Method) _methods.get(i)).invoke(stmt, (Object[]) _params.get(i));
  -					}
  -                }
  -                catch (IllegalAccessException ex)
  -                {
  -                    throw new SQLException(ex.toString());
  -                } 
  -                catch (InvocationTargetException ex) 
  +        try
  +        {
  +            m_platform.afterStatementCreate(stmt);
  +        }
  +        catch ( PlatformException e )
  +        {
  +            if ( e.getCause() instanceof SQLException )
  +            {
  +                throw (SQLException)e.getCause();
  +            }
  +            else
  +            {
  +                throw new SQLException(e.getMessage());
  +            }
  +        }
  +        try
  +        {
  +            m_platform.beforeBatch(stmt);
  +        }
  +        catch ( PlatformException e )
  +        {
  +            if ( e.getCause() instanceof SQLException )
  +            {
  +                throw (SQLException)e.getCause();
  +            }
  +            else
  +            {
  +                throw new SQLException(e.getMessage());
  +            }
  +        }
  +        try
  +        {
  +            for ( int i = 0; i < size; i++ )
  +            {
  +                Method method = (Method) _methods.get(i);
  +                try
  +                {
  +                    if ( method.equals(ADD_BATCH) )
  +                    {
  +                        /**
  +                         * we invoke on the platform and pass the stmt as an arg.
  +                         */
  +                        m_platform.addBatch(stmt);
  +                    }
  +                    else
  +                    {
  +                        method.invoke(stmt, (Object[]) _params.get(i));
  +                    }
  +                }
  +                catch (IllegalArgumentException ex)
  +                {
  +					StringBuffer buffer = generateExceptionMessage(i, stmt, ex);
  +					throw new SQLException(buffer.toString());
  +                }
  +                catch ( IllegalAccessException ex )
  +                {
  +					StringBuffer buffer = generateExceptionMessage(i, stmt, ex);
  +                    throw new SQLException(buffer.toString());
  +                }
  +                catch ( InvocationTargetException ex )
                   {
                       Throwable th = ex.getTargetException();
   
  -                    if (th == null) 
  +                    if ( th == null )
                       {
                           th = ex;
                       }
  -                    if (th instanceof SQLException)
  +                    if ( th instanceof SQLException )
                       {
                           throw ((SQLException) th);
  -                    } 
  -                    else 
  +                    }
  +                    else
                       {
                           throw new SQLException(th.toString());
                       }
                   }
  -            }
  -			try
  -			{
  -				/**
  -				 * this will call the platform specific call
  -				 */
  -				m_platform.executeBatch(stmt);
  -			}
  -			catch (PlatformException e)
  -			{
  -				if (e.getCause() instanceof SQLException)
  +				catch (PlatformException e)
   				{
  -					throw (SQLException)e.getCause();
  +					throw new SQLException(e.toString());
   				}
  -				else
  -				{
  -					throw new SQLException(e.getMessage());
  -				}
  -			}
  +            }
  +            try
  +            {
  +                /**
  +                 * this will call the platform specific call
  +                 */
  +                m_platform.executeBatch(stmt);
  +            }
  +            catch ( PlatformException e )
  +            {
  +                if ( e.getCause() instanceof SQLException )
  +                {
  +                    throw (SQLException)e.getCause();
  +                }
  +                else
  +                {
  +                    throw new SQLException(e.getMessage());
  +                }
  +            }
   
  -		}
  +        }
           finally
           {
               stmt.close();
  @@ -272,4 +289,25 @@
               _params.clear();
           }
       }
  +    
  +    private StringBuffer generateExceptionMessage(int i, PreparedStatement stmt, Exception ex)
  +	{
  +		StringBuffer buffer = new StringBuffer();
  +		buffer.append("Method of type: ");
  +		buffer.append(((Method) _methods.get(i)));
  +		buffer.append(" invoking on instance: ");
  +		if (( _methods.get(i)).equals(ADD_BATCH))
  +			buffer.append(m_platform);
  +		else
  +			buffer.append(stmt);
  +		buffer.append(" with parameters: ");
  +		if ((_methods.get(i)).equals(ADD_BATCH))
  +			buffer.append(stmt);
  +		else
  +			buffer.append(_params.get(i));
  +		buffer.append(" with root: ");
  +		buffer.append(ex.toString());
  +		return buffer;
  +	}
   }
  +
  
  
  

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