You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by sh33dafi <ya...@toyota-europe.com> on 2008/12/03 14:38:33 UTC

Batch execution of stored procedures

Hello, 

I'm trying to batch execute some stored procedures that will insert data
into a table. 

I've followed the instructions in the JavaDoc and I think the following code
has to work: 

getSqlMapClientTemplate().execute(new SqlMapClientCallback() { 
            public Object doInSqlMapClient(SqlMapExecutor executor) throws
SQLException { 
                executor.startBatch(); 
                if (LOGGER.isDebugEnabled()) { 
                    LOGGER.debug("Batch execution started"); 
                } 

                for (Iterator iter = basePrices.iterator(); iter.hasNext();)
{ 
                    final BasePriceInformation basePriceInformation =
(BasePriceInformation) iter.next(); 
                   
basePriceInformation.setPreparedDataSetId(preparedDataset.getId()); 
                    basePriceInformation.setDistGuid(nmsc.getDistGuid()); 
                    executor.insert("PreparedDataset.createBasePrice",
basePriceInformation); 
                    if (LOGGER.isDebugEnabled()) { 
                        LOGGER.debug("preparing statement for batch
executing"); 
                    } 
                } 

                executor.executeBatch(); 
                if (LOGGER.isDebugEnabled()) { 
                    LOGGER.debug("Executed batch"); 
                } 
                return null; 
            } 
        }); 

After some investigation we've discovered that each statement is directly
executed instead of added to the batch and then executed all at once at the
end. 

When searching around and debugging in the iBatis code there appears to be a
difference in the way an update statement is executed between general
statements and stored procedure calls. 

If you compare the implementation of
com.ibatis.sqlmap.engine.mapping.statement.ProcedureStatement.class 

protected int sqlExecuteUpdate(RequestScope request, Connection conn, String
sqlString, Object[] parameters) throws SQLException { 
    return getSqlExecutor().executeUpdateProcedure(request, conn,
sqlString.trim(), parameters); 
} 

with the code in
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.class 

protected int sqlExecuteUpdate(RequestScope request, Connection conn, String
sqlString, Object[] parameters) throws SQLException { 
    if (request.getSession().isInBatch()) { 
      getSqlExecutor().addBatch(request, conn, sqlString, parameters); 
      return 0; 
    } else { 
      return getSqlExecutor().executeUpdate(request, conn, sqlString,
parameters); 
    } 
  } 

It's clear that the implementation for stored procedures is lacking the if
statement to deside if the statement needs to be added to the batch or needs
to be executed immediatly 

Is there a reason why this has been left out for stored procedure
statements? 

Kind regards
-- 
View this message in context: http://www.nabble.com/Batch-execution-of-stored-procedures-tp20813022p20813022.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.