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/06/26 01:08:44 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/platforms PlatformDefaultImpl.java PlatformOracle9iImpl.java Platform.java

mattbaird    2003/06/25 16:08:44

  Modified:    src/java/org/apache/ojb/broker/platforms
                        PlatformDefaultImpl.java PlatformOracle9iImpl.java
                        Platform.java
  Log:
  Step one in the batch refactoring: add the new methods to the interface and implement them on the default as well as the Oracle9i version
  
  Revision  Changes    Path
  1.16      +31 -0     db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java
  
  Index: PlatformDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- PlatformDefaultImpl.java	9 May 2003 23:58:18 -0000	1.15
  +++ PlatformDefaultImpl.java	25 Jun 2003 23:08:44 -0000	1.16
  @@ -136,6 +136,37 @@
           //nothing
       }
   
  +	public void beforeBatch(PreparedStatement stmt) throws PlatformException
  +	{
  +		// nothing
  +	}
  +
  +	public void addBatch(PreparedStatement stmt) throws PlatformException
  +	{
  +		// nothing
  +		try
  +		{
  +			stmt.addBatch();
  +		}
  +		catch (SQLException e)
  +		{
  +			throw new PlatformException(e.getMessage(), e);
  +		}
  +	}
  +
  +	public int[] executeBatch(PreparedStatement stmt) throws PlatformException
  +	{
  +		try
  +		{
  +			return stmt.executeBatch();
  +		}
  +		catch (SQLException e)
  +		{
  +			throw new PlatformException(e.getMessage(), e);
  +		}
  +	}
  +
  +
       /**
        * @see Platform#initializeJdbcConnection
        */
  
  
  
  1.2       +119 -11   db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformOracle9iImpl.java
  
  Index: PlatformOracle9iImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformOracle9iImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PlatformOracle9iImpl.java	25 Jun 2003 21:50:30 -0000	1.1
  +++ PlatformOracle9iImpl.java	25 Jun 2003 23:08:44 -0000	1.2
  @@ -57,17 +57,10 @@
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  -import java.io.ByteArrayInputStream;
  -import java.io.InputStreamReader;
  -import java.io.Reader;
  -import java.io.StringReader;
  -import java.lang.reflect.Field;
  -import java.security.AccessController;
  -import java.security.PrivilegedAction;
  +import java.lang.reflect.InvocationTargetException;
  +import java.lang.reflect.Method;
   import java.sql.PreparedStatement;
   import java.sql.SQLException;
  -import java.sql.Statement;
  -import java.sql.Types;
   
   /**
    *  This class is a concrete implementation of <code>Platform</code>. Provides
  @@ -78,7 +71,7 @@
    *
    * see http://technet.oracle.com/products/oracle9i/daily/jun07.html
    *
  - * Optimization: use ROWNUM to minimize the effects of not having server side cursors
  + * TODO: Optimization: use ROWNUM to minimize the effects of not having server side cursors
    *
    * see http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:127412348064
    *
  @@ -87,6 +80,121 @@
   
   public class PlatformOracle9iImpl extends PlatformOracleImpl
   {
  -    private Logger logger = LoggerFactory.getLogger(PlatformOracle9iImpl.class);
  +	private Logger logger = LoggerFactory.getLogger(PlatformOracle9iImpl.class);
  +	private static final Object[] BATCH_SIZE = {new Integer(10)};
  +	private static final Class[] PARAM_TYPE = {Integer.class};
  +	private static boolean SET_EXECUTE_BATCH_METHOD_EXISTS = true;
  +	private static boolean SEND_BATCH_METHOD_EXISTS = true;
   
  +	/**
  +	 * don't bind to the Oracle drivers and thus have to cast, rather do the
  +	 * reflection to find out if we can use the setExecuteBatch method with this
  +	 * particular driver, and degrade to default JDBC batching if necessary
  +	 * @param stmt
  +	 * @throws PlatformException
  +	 */
  +	public void beforeBatch(PreparedStatement stmt) throws PlatformException
  +	{
  +		// ((OraclePreparedStatement)ps).setExecuteBatch (3);
  +		if (SET_EXECUTE_BATCH_METHOD_EXISTS && SEND_BATCH_METHOD_EXISTS)
  +		{
  +			try
  +			{
  +				Method setExecuteBatch = stmt.getClass().getMethod("setExecuteBatch", PARAM_TYPE);
  +				setExecuteBatch.invoke(stmt, BATCH_SIZE);
  +			}
  +			catch (IllegalAccessException e)
  +			{
  +				throw new PlatformException(e.getMessage(), e);
  +			}
  +			catch (IllegalArgumentException e)
  +			{
  +				throw new PlatformException(e.getMessage(), e);
  +			}
  +			catch (InvocationTargetException e)
  +			{
  +				throw new PlatformException(e.getMessage(), e);
  +			}
  +			catch (NoSuchMethodException e)
  +			{
  +				SET_EXECUTE_BATCH_METHOD_EXISTS = false;
  +			}
  +		}
  +		if (!SET_EXECUTE_BATCH_METHOD_EXISTS)
  +		{
  +			super.beforeBatch(stmt);
  +		}
  +	}
  +
  +	/**
  +	 * in oracle batching we call executeUpdate instead of addBatch
  +	 * @param stmt
  +	 * @throws PlatformException
  +	 */
  +	public void addBatch(PreparedStatement stmt) throws PlatformException
  +	{
  +		// ps.executeUpdate();
  +		if (SET_EXECUTE_BATCH_METHOD_EXISTS && SEND_BATCH_METHOD_EXISTS)
  +		{
  +			try
  +			{
  +				stmt.executeUpdate();
  +			}
  +			catch (SQLException e)
  +			{
  +				throw new PlatformException(e.getMessage(), e);
  +			}
  +		}
  +		else
  +		{
  +			super.addBatch(stmt);
  +		}
  +	}
  +
  +	/**
  +	 * equivelent of calling ((OraclePreparedStatement)ps).sendBatch() but we do it reflectively so
  +	 * we don't have to bind to the Oracle classes
  +	 * JDBC sends the queued request
  +	 * @param stmt the statement you want to execute sendBatch on.
  +	 * @return an int array of the success statuses.
  +	 * @throws PlatformException
  +	 */
  +	public int[] executeBatch(PreparedStatement stmt) throws PlatformException
  +	{
  +		int[] retval = null;
  +		if (SET_EXECUTE_BATCH_METHOD_EXISTS && SEND_BATCH_METHOD_EXISTS)
  +		{
  +			try
  +			{
  +				Method sendBatch = stmt.getClass().getMethod("sendBatch", null);
  +				int numberOfItemsSubmitted = ((Integer) sendBatch.invoke(stmt, null)).intValue();
  +				retval = new int[numberOfItemsSubmitted];
  +				for (int i = 0; i<= numberOfItemsSubmitted; i++)
  +				{
  +					retval[i] = 1;
  +				}
  +			}
  +			catch (IllegalAccessException e)
  +			{
  +				throw new PlatformException(e.getMessage(), e);
  +			}
  +			catch (IllegalArgumentException e)
  +			{
  +				throw new PlatformException(e.getMessage(), e);
  +			}
  +			catch (InvocationTargetException e)
  +			{
  +				throw new PlatformException(e.getMessage(), e);
  +			}
  +			catch (NoSuchMethodException e)
  +			{
  +				SEND_BATCH_METHOD_EXISTS = false;
  +			}
  +		}
  +		if (!SEND_BATCH_METHOD_EXISTS)
  +		{
  +			retval = super.executeBatch(stmt);
  +		}
  +		return retval;
  +	}
   }
  
  
  
  1.15      +22 -1     db-ojb/src/java/org/apache/ojb/broker/platforms/Platform.java
  
  Index: Platform.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/Platform.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Platform.java	9 May 2003 23:58:18 -0000	1.14
  +++ Platform.java	25 Jun 2003 23:08:44 -0000	1.15
  @@ -91,6 +91,27 @@
        */
       public void afterStatementClose(Statement stmt, ResultSet rs) throws PlatformException;
   
  +	/**
  +	 *
  +	 * @param stmt the statement you want to batch on
  +	 * @throws PlatformException
  +	 */
  +	public void beforeBatch(PreparedStatement stmt) throws PlatformException;
  +
  +	/**
  +	 *
  +	 * @param stmt the statement you are adding to the batch
  +	 * @throws PlatformException
  +	 */
  +	public void addBatch(PreparedStatement stmt) throws PlatformException;
  +
  +	/**
  +	 *
  +	 * @param stmt the statement you want to execute the batch on
  +	 * @throws PlatformException
  +	 */
  +	public int[] executeBatch(PreparedStatement stmt) throws PlatformException;
  +
       /**
        * callback called immediately after a JDBC Connection has been obtained
        * in ...