Reputation: 211
I have defined a Spring Batch job which runs once just fine. However it is unable to run a second time due to a duplicate entry in the primary key for the JOB_INSTANCE_ID;
SEVERE: Job Terminated in error: PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; Duplicate entry '0' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'
I've read that this can be over-come by setting command line parameters on the job, e.g.
java -cp ${CLASSPATH} org.springframework.batch.core.launch.support.CommandLineJobRunner myJob.xml myJob date=20121025154016 -next
Whether I include the -next parameter or not, it will not run the job.
My Spring Batch configuration looks as follows (batch/launchContext.xml);
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Launch context: how to launch all of the jobs -->
<!-- Expects a data source configuration file to be used, with a DataSource bean
called "dataSource" -->
<!-- The data source for the jobs status -->
<import resource="dataSource.xml" />
<bean
id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property
name="dataSource"
ref="dataSource" />
<property
name="databaseType"
value="MySQL" />
<property
name="transactionManager"
ref="transactionManager"></property>
</bean>
<bean
id="incrementer"
class="org.springframework.batch.core.launch.support.RunIdIncrementer" />
<bean
id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property
name="jobRepository"
ref="jobRepository" />
</bean>
<bean
id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property
name="dataSource"
ref="dataSource" />
</bean>
<bean
id="jobExplorer"
class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
<property
name="dataSource"
ref="dataSource" />
</bean>
</beans>
And the job is like this (in the myJob.xml file)
....
<import resource="batch/launchContext.xml" />
...
<batch:job
id="myJob"
job-repository="jobRepository"
incrementer="incrementer">
<batch:step id="step1">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk
reader="myReader"
processor="myProcessor"
writer="myWriter"
commit-interval="10" />
</batch:tasklet>
</batch:step>
</batch:job>
Ideally what I'd like is each run of the job to be persisted with the run date/time. Is there a way to configure the launcher to do this automatically?
Upvotes: 1
Views: 9084
Reputation: 211
I'd also placed this question on the official Spring Forums. And I eventually found my own answer (with a hint along the way);
http://forum.springsource.org/showthread.php?131347-How-to-re-run-a-job&p=428484#post428484
In short, the problem was that I'd deleted the value '0' from these three tables;
So when cleaning out the Spring Batch Meta-Data tables (e.g. using truncate or delete from ...) ensure the following commands are run afterwards;
INSERT INTO BATCH_STEP_EXECUTION_SEQ values(0);
INSERT INTO BATCH_JOB_EXECUTION_SEQ values(0);
INSERT INTO BATCH_JOB_SEQ values(0);
Upvotes: 7