title: How to re-run a Spring Batch job?
body: <p>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;</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>I've read that this can be over-come by setting command line parameters on the job, e.g.</p>
  
  <pre><code>java -cp ${CLASSPATH} org.springframework.batch.core.launch.support.CommandLineJobRunner myJob.xml myJob date=20121025154016 -next
  </code></pre>
  
  <p>Whether I include the -next parameter or not, it will not run the job.</p>
  
  <p>My Spring Batch configuration looks as follows (batch/launchContext.xml);</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;!-- Launch context: how to launch all of the jobs --&gt;
      &lt;!-- Expects a data source configuration file to be used, with a DataSource bean 
  called "dataSource" --&gt;
  
      &lt;!-- The data source for the jobs status --&gt;
      &lt;import resource="dataSource.xml" /&gt;
  
      &lt;bean
              id="jobRepository"                        
  class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"&gt;
              &lt;property
                      name="dataSource"
                      ref="dataSource" /&gt;
              &lt;property
                      name="databaseType"
                      value="MySQL" /&gt;
              &lt;property
                      name="transactionManager"
                      ref="transactionManager"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean
              id="incrementer"
              class="org.springframework.batch.core.launch.support.RunIdIncrementer" /&gt;
  
      &lt;bean
              id="jobLauncher"
              class="org.springframework.batch.core.launch.support.SimpleJobLauncher"&gt;
              &lt;property
                      name="jobRepository"
                      ref="jobRepository" /&gt;
      &lt;/bean&gt;
  
      &lt;bean
              id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
              &lt;property
                      name="dataSource"
                      ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;bean
              id="jobExplorer"       
  class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"&gt;
              &lt;property
                      name="dataSource"
                      ref="dataSource" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And the job is like this (in the myJob.xml file)</p>
  
  <pre><code>....
  &lt;import resource="batch/launchContext.xml" /&gt;
  ...
      &lt;batch:job
              id="myJob"
              job-repository="jobRepository"
              incrementer="incrementer"&gt;
              &lt;batch:step id="step1"&gt;
                      &lt;batch:tasklet transaction-manager="transactionManager"&gt;
                              &lt;batch:chunk
                                      reader="myReader"
                                      processor="myProcessor"
                                      writer="myWriter"
                                      commit-interval="10" /&gt;
                      &lt;/batch:tasklet&gt;
              &lt;/batch:step&gt;
      &lt;/batch:job&gt;
  </code></pre>
  
  <p>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?</p>