Announcement Announcement Module
Collapse
No announcement yet.
Does spring have a plan to support iBATIS batch execution? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does spring have a plan to support iBATIS batch execution?

    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. ---
    Code:
    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

  • #2
    Tak,

    Why can't you just do:

    Code:
    		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...

    Comment


    • #3
      These are two examples of the actual code in our app using iBatis batch.




      Code:
      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);
      			}
      		});
      
      	}

      Code:
      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;
      			}
      		});
      
      	}

      Comment


      • #4
        Thanks Colin,
        You saved my life.

        Tak

        Comment


        • #5
          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

          Code:
          		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_updateByPrimaryKey", record);
          				}
          
          				int rowsaffected = executor.executeBatch();
          				
          				logger.info("rows afftected: " + rowsaffected);
          
          				return new Integer(rowsaffected);
          			}
          		});

          Comment

          Working...
          X