Mybatis - Spring batch operations Not Working

183 views Asked by At

im trying to batch insert a number of records using mybatis.

this is in my applicationContext

 <bean id="refDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName">
     <value>com.mysql.cj.jdbc.Driver</value>
   </property>
   <property name="url">
     <value>jdbc:mysql://localhost:3306/somedatabase?rewriteBatchedStatements=true</value>
   </property>
   <property name="username"><value>root</value></property>
   <property name="password"><value>root</value></property>
 </bean>
 <bean id="refSqlMapClientIbatis" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="configLocation" value="/WEB-INF/jsp/ref/sql-map-config.xml" />
   <property name="dataSource" ref="refDataSource" />
 </bean>
 
 <bean id="refSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
   <constructor-arg index="0" ref="refSqlMapClientIbatis" />
 </bean>
 <bean id="refSqlMapClient" class="mypackage.base.IbatisSqlMapClient">
   <property name="sqlSession" ref="refSqlSession" />
 </bean>
 
 <bean id="refBatchSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
   <constructor-arg index="0" ref="refSqlMapClientIbatis" />
   <constructor-arg index="1" value="BATCH" />
 </bean>
 <bean id="refBatchSqlMapClient" class="mypackage.base.MybatisSqlSession">
   <property name="batchSqlSession" ref="refBatchSqlSession" />
 </bean>
 
 <bean id="RefBaseDAO" abstract="true">
   <property name="sqlMap" ref="refSqlMapClient"/>
   <property name="batchMap" ref="refBatchSqlMapClient"/>
 </bean>
 `

when i use batchMap to insert inside of a loop all commands are executed on the spot without me calling flush .

this is the log file which shows that they're getting executed without me calling any execute or commit methods.

 java.sql.PreparedStatement.addBatch: INSERT INTO  fz_ref_um_rates
        (um_rate_value, um_id, to_um_id, relation_between_prod_spec, um_rate_product_id, um_rate_product_assigned_spec_id, brch_id, created_by, create_date)
        VALUES
        (2.0, '952', '391', N, null, null, '284', 2, NOW());

[09/12/2022 11:48:52] org.slf4j.jul.JDK14LoggerAdapter.innerNormalizedLoggingCallHandler(JDK14LoggerAdapter.java:156)
 [INFO]: java.sql.Statement.executeBatch: 

[09/12/2022 11:48:52] org.slf4j.jul.JDK14LoggerAdapter.innerNormalizedLoggingCallHandler(JDK14LoggerAdapter.java:156)
 [INFO]: java.sql.PreparedStatement.addBatch: INSERT INTO  fz_ref_um_rates
        (um_rate_value, um_id, to_um_id, relation_between_prod_spec, um_rate_product_id, um_rate_product_assigned_spec_id, brch_id, created_by, create_date)
        VALUES
        (3.0, '952', '339', N, null, null, '284', 2, NOW());

[09/12/2022 11:48:52] org.slf4j.jul.JDK14LoggerAdapter.innerNormalizedLoggingCallHandler(JDK14LoggerAdapter.java:156)
 [INFO]: java.sql.Statement.executeBatch: 

[09/12/2022 11:48:52] org.slf4j.jul.JDK14LoggerAdapter.innerNormalizedLoggingCallHandler(JDK14LoggerAdapter.java:156)
 [INFO]: java.sql.PreparedStatement.addBatch: INSERT INTO  fz_ref_um_rates
        (um_rate_value, um_id, to_um_id, relation_between_prod_spec, um_rate_product_id, um_rate_product_assigned_spec_id, brch_id, created_by, create_date)
        VALUES
        (45.0, '952', '338', N, null, null, '284', 2, NOW());

[09/12/2022 11:48:52] org.slf4j.jul.JDK14LoggerAdapter.innerNormalizedLoggingCallHandler(JDK14LoggerAdapter.java:156)
 [INFO]: java.sql.Statement.executeBatch: 

```
`

> this is my batchmap class

    public class MybatisSqlSession {
        private SqlSession batchSqlSession = null;
    
    public Object insert(String string, Object object) throws FZDAOException {
            try {
                System.out.println("insert called");
                return batchSqlSession.insert(string, object);
    
            } catch (SqlSessionException ex) {
                throw new MyException(ex.getMessage(), ex);
            }
        }
    }

> this is where im calling and getting the above logs :

    public void insertUmRates(List<UmRatesVO> addList) throws FZDAOException
        {
            for(UmRatesVO umRatesVO : addList)
            {
                batchMap.insert("UmRatesQueries.insertUmRates", umRatesVO);
            }
        }


if any further code is needed let me know 
 
0

There are 0 answers