PDA

View Full Version : Does spring have a plan to support iBATIS batch execution?



Tak
Aug 26th, 2004, 08:59 AM
Has anyone done iBATIS 2.0 batch execution on spring before?
Looks like there is no code about it in org.springframework.orm.ibatis.SqlMapClientTemplat e in 1.1 RC2 distribution.

To do it, I am using my own classes extended spring iBatis related classes.
But if there is plan, please let me know. I don't want to use my own version for my project.

--- Here is a snippet of my codes. ---


public Object batchExecute(List parameterObject, SqlMapClientBatchExecuteCallback action) throws DataAccessException
{
SqlMapSession session = getSqlMapClient().openSession();
try {
Connection con = DataSourceUtils.getConnection(getDataSource());
try {
session.setUserConnection(con);

session.startBatch();
Iterator detailListIte = parameterObject.iterator();
while (detailListIte.hasNext()) {
Object entry = detailListIte.next();
action.doBatchInSqlMapClient(session, entry);
}
return new Integer(session.executeBatch());

} catch (SQLException ex) {
throw getExceptionTranslator().translate("SqlMapClientTemplate.batchExecute", "(mapped statement)", ex);
} finally {
DataSourceUtils.closeConnectionIfNecessary(con, getDataSource());
}
} finally {
session.close();
}
}

public Object batchInsert(final String statementName, final List parameterObject)
throws DataAccessException
{
return batchExecute(parameterObject, new SqlMapClientBatchExecuteCallback()
{
public Object doBatchInSqlMapClient(SqlMapExecutor executor, Object entry) throws SQLException
{
return executor.insert(statementName, entry);
}
});
}
public Object batchUpdate....


Regards,
Tak

Colin Sampaleanu
Aug 26th, 2004, 10:14 AM
Tak,

Why can't you just do:



getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
executor.startBatch();
// do some iBatis operations here
executor.executeBatch();
return null;
}
});


I'm not clear on how usage of your batchExecute method would be much shorter or more convenient...

2devnull
Aug 26th, 2004, 11:34 AM
These are two examples of the actual code in our app using iBatis batch.





public void insertUserRolesById(final List lst, final String user_id) {

getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
Iterator itr = lst.iterator();

executor.startBatch();
executor.delete("Users.deleteUserRolesById", user_id);
while (itr.hasNext()) {
HashMap m = (HashMap) itr.next();
executor.insert("Users.insertUserRolesById", m);
}

int rowsaffected = executor.executeBatch();

logger.info("rows afftected by insertUserRolesById: " + rowsaffected);

return new Integer(rowsaffected);
}
});

}



public void updateElement(final MWDomain fbo) {

getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {

executor.startBatch();

executor.update("AuthMaint.updateConEditionStatus", fbo.getUpdateContentEdition());
executor.update("AuthMaint.updateCatExtendName", fbo.getUpdateCatExtendName());

Iterator itr = fbo.getContentEdition().iterator();
while (itr.hasNext())
executor.insert("AuthMaint.insertContentEdition", ((HashMap) itr.next()));

itr = fbo.getRepItem().iterator();
while (itr.hasNext())
executor.insert("AuthMaint.insertRepItem", ((HashMap) itr.next()));

itr = fbo.getDocMap().iterator();
while (itr.hasNext())
executor.insert("AuthMaint.insertDocMap", ((HashMap) itr.next()));

int rowsaffected = executor.executeBatch();
logger.info("rows afftected by updateFamily: " + rowsaffected);

return null;
}
});

}

Tak
Aug 26th, 2004, 04:01 PM
Thanks Colin,
You saved my life.

Tak

eros
Mar 2nd, 2010, 02:52 AM
the below update batch was successfully executed and the DB data was changed but the rowsaffected value still zero..

any inputs from the community... highly appreciated..

thanks



getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
Iterator<TSimHyotaku> itr = records.iterator();

executor.startBatch();
while (itr.hasNext()) {
TSimHyotaku record = itr.next();
executor.update("SDE_T_SIM_HYOTAKU.ibatorgenerated_updateByPrimaryK ey", record);
}

int rowsaffected = executor.executeBatch();

logger.info("rows afftected: " + rowsaffected);

return new Integer(rowsaffected);
}
});