Murthy
Murthy

Reputation: 77

Inserting data into more than one table in spring-batch using ibatis

I am using IbatisBatchItemWriter to write complex object into multiple tables.

Here is my object how it looks like

public class SfObject{
  protected List<Person> person;  
}
public class Person {
  protected String personId;
  protected XMLGregorianCalendar dateOfBirth;
  protected String countryOfBirth;
  protected String regionOfBirth;
  protected String placeOfBirth;
  protected String birthName;
  protected XMLGregorianCalendar dateOfDeath;
  protected XMLGregorianCalendar lastModifiedOn;
  protected List<EmailInformation> emailInformation;
}
public class EmailInformation {
  protected String emailType;
  protected String emailAddress;
  protected XMLGregorianCalendar lastModifiedOn;
}

And here is my ibatis configuration to insert above objests

<insert id="insertCompoundEmployeeData" parameterClass="com.domain.SfObject">
 <iterate property="person">
  insert into E_Person_Info
   (person_id,
   person_birth_dt,
   person_country_of_birth,
   person_region_of_birth,
   person_place_of_birth,
   person_birth_name,
   person_death_dt,
   last_modified_on
   )        
  values (#person[].personId#,
   #person[].dateOfBirth,
   #person[].countryOfBirth#,
   #person[].regionOfBirth#,
   #person[].placeOfBirth#,
   #person[].birthName#,
   #person[].dateOfDeath#,
   #person[].lastModifiedOn#
  );
  <iterate property="person[].emailInformation">
   insert into E_Email_Info
    (email_info_person_id,
    email_info_email_type,
    email_info_email_address,
    last_modified_on
   )        
   values (#person[].personId#,
    #person[].emailInformation[].emailType#,
    #person[].emailInformation[].emailAddress#,
    #person[].emailInformation[].lastModifiedOn#
   );                       
  </iterate>                                                
 </iterate>       
</insert>

I am not sure whether i could use above config to insert data into more than one table, but when i executed the above code i am getting below error for batch of 10 records. Btw, email information is not mandatory so, it may be null in some person object.

Stacktrace
[08.08.2014 17:30:07] DEBUG: WebservicePagingItemReader.doRead() - Reading page 0
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=1
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=2
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=3
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=4
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=5
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=6
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=7
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=8
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=9
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.isComplete() - Repeat is complete according to policy and result value.
[08.08.2014 17:30:09] DEBUG: IbatisBatchItemWriter.write() - Executing batch with 10 items.
[08.08.2014 17:30:09] DEBUG: SqlMapClientTemplate.execute() - Opened SqlMapSession [com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl@168afdd] for iBATIS operation
[08.08.2014 17:30:10] DEBUG: Connection.debug() - {conn-100000} Connection
[08.08.2014 17:30:10] DEBUG: SqlMapClientTemplate.execute() - Obtained JDBC Connection [Transaction-aware proxy for target Connection  from DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@8eae04]] for iBATIS operation
[08.08.2014 17:30:10] DEBUG: Connection.debug() - {conn-100000} Preparing Statement:        insert into E_Person_Info        (person_id,        person_birth_dt,        person_country_of_birth,        person_region_of_birth,        person_place_of_birth,        person_birth_name,        person_death_dt,        last_modified_on        )         values (?,         ?,         ?,         ?,         ?,         ?,         ?,         ?         );                               
[08.08.2014 17:30:10] DEBUG: Connection.debug() - {conn-100000} Preparing Statement:        insert into E_Person_Info        (person_id,        person_birth_dt,        person_country_of_birth,        person_region_of_birth,        person_place_of_birth,        person_birth_name,        person_death_dt,        last_modified_on        )         values (?,         ?,         ?,         ?,         ?,         ?,         ?,         ?         );          insert into E_Email_Info          (email_info_person_id,          email_info_email_type,          email_info_email_address,          last_modified_on          )           values (?,           ?,           ?,           ?           );                                        
[08.08.2014 17:30:10] DEBUG: Connection.debug() - {conn-100000} Preparing Statement:        insert into E_Person_Info        (person_id,        person_birth_dt,        person_country_of_birth,        person_region_of_birth,        person_place_of_birth,        person_birth_name,        person_death_dt,        last_modified_on        )         values (?,         ?,         ?,         ?,         ?,         ?,         ?,         ?         );                               
[08.08.2014 17:30:10] DEBUG: TaskletStep.doInChunkContext() - Applying contribution: [StepContribution: read=10, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
[08.08.2014 17:30:10] DEBUG: TaskletStep.doInChunkContext() - Rollback for Exception: org.springframework.dao.InvalidDataAccessResourceUsageException: Batch execution returned invalid results. Expected 1 but number of BatchResult objects returned was 3
[08.08.2014 17:30:10] DEBUG: DataSourceTransactionManager.processRollback() - Initiating transaction rollback
[08.08.2014 17:30:10] DEBUG: DataSourceTransactionManager.doRollback() - Rolling back JDBC transaction on Connection [net.sourceforge.jtds.jdbc.ConnectionJDBC3@190d8e1]
[08.08.2014 17:30:10] DEBUG: DataSourceTransactionManager.doCleanupAfterCompletion() - Releasing JDBC Connection [net.sourceforge.jtds.jdbc.ConnectionJDBC3@190d8e1] after transaction
[08.08.2014 17:30:10] DEBUG: DataSourceUtils.doReleaseConnection() - Returning JDBC Connection to DataSource
[08.08.2014 17:30:10] DEBUG: RepeatTemplate.doHandle() - Handling exception: org.springframework.dao.InvalidDataAccessResourceUsageException, caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: Batch execution returned invalid results. Expected 1 but number of BatchResult objects returned was 3
[08.08.2014 17:30:10] DEBUG: RepeatTemplate.executeInternal() - Handling fatal exception explicitly (rethrowing first of 1): org.springframework.dao.InvalidDataAccessResourceUsageException: Batch execution returned invalid results. Expected 1 but number of BatchResult objects returned was 3
[08.08.2014 17:30:10] ERROR: AbstractStep.execute() - Encountered an error executing the step
org.springframework.dao.InvalidDataAccessResourceUsageException: Batch execution returned invalid results. Expected 1 but number of BatchResult objects returned was 3
    at org.springframework.batch.item.database.IbatisBatchItemWriter.write(IbatisBatchItemWriter.java:140)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:156)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:137)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:252)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:178)
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:74)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:268)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:76)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:367)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:242)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198)
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:348)
    at org.springframework.batch.core.job.flow.FlowJob.access$0(FlowJob.java:1)
    at org.springframework.batch.core.job.flow.FlowJob$JobFlowExecutor.executeStep(FlowJob.java:135)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:103)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:250)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:110)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:105)
    at com.CtrlMPojoForBatch.initiateSpringBatchProcess(CtrlMPojoForBatch.java:92)
    at com.CtrlMPojoForBatch.main(CtrlMPojoForBatch.java:33)
[08.08.2014 17:30:10] WARN: CustomStepExecutionListner.afterStep() - Failure occured executing the step readWriteExchagnerateConversionData
[08.08.2014 17:30:10] WARN: CustomStepExecutionListner.afterStep() - Initiating the rollback operation... 
[08.08.2014 17:30:10] WARN: CustomStepExecutionListner.afterStep() - Rollback completed!

Upvotes: 0

Views: 1617

Answers (1)

Michael Minella
Michael Minella

Reputation: 21463

Assuming you're using the IbatisBatchItemWriter provided in Spring Batch (it's been deprecated in favor of the ones provided by the MyBatis project), set the assertUpdates to false. This will prevent Spring Batch from verifying that only one update was made per item.

Upvotes: 2

Related Questions