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 03:04:10 UTC

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

mattbaird    2003/06/25 18:04:10

  Modified:    src/java/org/apache/ojb/broker/util/batch
                        BatchConnection.java
                        PreparedStatementInvocationHandler.java
               src/java/org/apache/ojb/broker/platforms
                        PlatformOracle9iImpl.java
  Log:
  Oracle 9i specific batching, about 2X faster than normal JDBC batching. You must have Oracle 9i and the proper 9i JDBC driver.
  
  Revision  Changes    Path
  1.10      +44 -9     db-ojb/src/java/org/apache/ojb/broker/util/batch/BatchConnection.java
  
  Index: BatchConnection.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/batch/BatchConnection.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BatchConnection.java	27 Mar 2003 06:13:21 -0000	1.9
  +++ BatchConnection.java	26 Jun 2003 01:04:10 -0000	1.10
  @@ -75,9 +75,13 @@
   //#endif
   
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.PBKey;
  +import org.apache.ojb.broker.platforms.PlatformFactory;
  +import org.apache.ojb.broker.platforms.PlatformException;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.DescriptorRepository;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  +import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
   
   /**
    * The implementation of {@link java.sql.Connection} which
  @@ -104,11 +108,12 @@
       private HashSet _deleted;
       private HashSet _dontInsert;
       private int count = 0;
  +	private JdbcConnectionDescriptor m_jcd;
   
       public BatchConnection(Connection conn, PersistenceBroker broker)
       {
           super(conn);
  -
  +		m_jcd = broker.serviceConnectionManager().getConnectionDescriptor();
           _fkInfo = (HashMap) _pbkeyToFKInfo.get(broker.getPBKey());
           if (_fkInfo != null)
           {
  @@ -236,8 +241,8 @@
        * If UPDATE, INSERT or DELETE, return BatchPreparedStatement,
        * otherwise return null.
        */
  -    private PreparedStatement prepareBatchStatement(String sql)
  -    {
  +    private PreparedStatement prepareBatchStatement(String sql) throws PlatformException
  +	{
           String sqlCmd = sql.substring(0, 7);
   
           if (sqlCmd.equals("UPDATE ") || sqlCmd.equals("DELETE ")
  @@ -248,7 +253,7 @@
               {
                   stmt = (PreparedStatement) Proxy.newProxyInstance(getClass().getClassLoader(),
                           new Class[]{PreparedStatement.class, BatchPreparedStatement.class},
  -                        new PreparedStatementInvocationHandler(this, sql));
  +                        new PreparedStatementInvocationHandler(this, sql, m_jcd));
                   _statements.put(sql, stmt);
               }
               return stmt;
  @@ -262,9 +267,24 @@
       public PreparedStatement prepareStatement(String sql)
               throws SQLException
       {
  -        PreparedStatement stmt = prepareBatchStatement(sql);
  +		PreparedStatement stmt = null;
  +		try
  +		{
  +			stmt = prepareBatchStatement(sql);
  +		}
  +		catch (PlatformException e)
  +		{
  +			if (e.getCause() instanceof SQLException)
  +			{
  +				throw (SQLException)e.getCause();
  +			}
  +			else
  +			{
  +				throw new SQLException(e.getMessage());
  +			}
  +		}
   
  -        if (stmt == null)
  +		if (stmt == null)
           {
               stmt = getDelegate().prepareStatement(sql);
           }
  @@ -276,9 +296,24 @@
                                                 int resultSetConcurrency)
               throws SQLException
       {
  -        PreparedStatement stmt = prepareBatchStatement(sql);
  +		PreparedStatement stmt = null;
  +		try
  +		{
  +			stmt = prepareBatchStatement(sql);
  +		}
  +		catch (PlatformException e)
  +		{
  +			if (e.getCause() instanceof SQLException)
  +			{
  +				throw (SQLException)e.getCause();
  +			}
  +			else
  +			{
  +				throw new SQLException(e.getMessage());
  +			}
  +		}
   
  -        if (stmt == null)
  +		if (stmt == null)
           {
               stmt = getDelegate().prepareStatement(sql, resultSetType, resultSetConcurrency);
           }
  
  
  
  1.5       +73 -19    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PreparedStatementInvocationHandler.java	27 Mar 2003 06:25:08 -0000	1.4
  +++ PreparedStatementInvocationHandler.java	26 Jun 2003 01:04:10 -0000	1.5
  @@ -55,6 +55,11 @@
    */
   
   
  +import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
  +import org.apache.ojb.broker.platforms.PlatformFactory;
  +import org.apache.ojb.broker.platforms.PlatformException;
  +import org.apache.ojb.broker.platforms.Platform;
  +
   import java.lang.reflect.Method;
   import java.lang.reflect.InvocationTargetException;
   import java.math.BigDecimal;
  @@ -84,24 +89,13 @@
   
       private final static Integer ONE = new Integer(1);
   
  -    private final static Method ADD_BATCH;
  +    private static Method ADD_BATCH;
   
       private final static Method SET_BIG_DECIMAL;
   
       static
       {
  -        Method addBatch = null;
           Method setBigDecimal = null;
  -
  -        try
  -        {
  -            addBatch = PreparedStatement.class.getMethod("addBatch", null);
  -        }
  -        catch (Exception ex)
  -        {
  -        }
  -        ADD_BATCH = addBatch;
  -
           try
           {
               setBigDecimal = PreparedStatement.class.getMethod("setBigDecimal",
  @@ -121,11 +115,28 @@
   
       private ArrayList _params = new ArrayList();
   
  -    public PreparedStatementInvocationHandler(BatchConnection batchConn, String sql)
  +	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)
  +		{
  +		}
  +	}
   
       public Object invoke(Object proxy, Method method, Object[] args)
               throws Throwable
  @@ -164,14 +175,39 @@
               return;
           }
           stmt = conn.prepareStatement(_sql);
  -        try
  +		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 
                   {
  -                    ((Method) _methods.get(i)).invoke(stmt, (Object[]) _params.get(i));
  -                } 
  +					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());
  @@ -194,8 +230,26 @@
                       }
                   }
               }
  -            stmt.executeBatch();
  -        }
  +			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();
  
  
  
  1.3       +14 -7     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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PlatformOracle9iImpl.java	25 Jun 2003 23:08:44 -0000	1.2
  +++ PlatformOracle9iImpl.java	26 Jun 2003 01:04:10 -0000	1.3
  @@ -82,10 +82,11 @@
   {
   	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 final Class[] PARAM_TYPE = {Integer.TYPE};
   	private static boolean SET_EXECUTE_BATCH_METHOD_EXISTS = true;
   	private static boolean SEND_BATCH_METHOD_EXISTS = true;
  -
  +	private static Method SET_EXECUTE_BATCH = null;
  +	private static Method SEND_BATCH = null;
   	/**
   	 * 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
  @@ -100,8 +101,11 @@
   		{
   			try
   			{
  -				Method setExecuteBatch = stmt.getClass().getMethod("setExecuteBatch", PARAM_TYPE);
  -				setExecuteBatch.invoke(stmt, BATCH_SIZE);
  +				if (SET_EXECUTE_BATCH == null)
  +				{
  +					SET_EXECUTE_BATCH = stmt.getClass().getMethod("setExecuteBatch", PARAM_TYPE);
  +				}
  +				SET_EXECUTE_BATCH.invoke(stmt, BATCH_SIZE);
   			}
   			catch (IllegalAccessException e)
   			{
  @@ -166,10 +170,13 @@
   		{
   			try
   			{
  -				Method sendBatch = stmt.getClass().getMethod("sendBatch", null);
  -				int numberOfItemsSubmitted = ((Integer) sendBatch.invoke(stmt, null)).intValue();
  +				if (SEND_BATCH == null)
  +				{
  +					SEND_BATCH = stmt.getClass().getMethod("sendBatch", null);
  +				}
  +				int numberOfItemsSubmitted = ((Integer) SEND_BATCH.invoke(stmt, null)).intValue();
   				retval = new int[numberOfItemsSubmitted];
  -				for (int i = 0; i<= numberOfItemsSubmitted; i++)
  +				for (int i = 0; i < numberOfItemsSubmitted; i++)
   				{
   					retval[i] = 1;
   				}