Writing Data using Spring Batch JpaItemWriter incomplete
<p>I'm trying to perform some data maintenance operations using Spring Batch (3.0.6). I'm reading data from JPA (Hibernate 5.0.1 implementation), alter it and trying to save it back to the database using JPA again. </p>

<p>The job execution is successful, however, after having a look at the database I see, that only a few entries have actually been altered (ALL entries should have been altered). It seems the data is not flushed correctly / the transaction isn't being committed, logging revealed that all items are processed in the appropriate processor AND the appropriate writer.</p>

<p>Probably I'm missing something out regarding the transactionManager, but I'm having a hard time figuring it out as the job context looks sane to me:</p>

<pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:batch="http://www.springframework.org/schema/batch" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"&gt;

    &lt;bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" /&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="jobListener" class="batch.enricher.JobListener" /&gt;

    &lt;job id="hibernateJob" restartable="false"
    xmlns="http://www.springframework.org/schema/batch"&gt;
        &lt;step id="step1"&gt;
            &lt;tasklet transaction-manager="transactionManager"&gt;
                &lt;chunk reader="hibernateItemReader" processor="enrichmentProcessor"
                writer="entityWriter" commit-interval="50"&gt;
                &lt;/chunk&gt;
            &lt;/tasklet&gt;
        &lt;/step&gt;
        &lt;batch:listeners&gt;
            &lt;batch:listener ref="jobListener" /&gt;
        &lt;/batch:listeners&gt;
    &lt;/job&gt;

    &lt;bean id="entityManagerFactoryRegistry" class="database.EntityManagerFactoryRegistry" /&gt;

    &lt;bean id="enrichmentProcessor" class="batch.enricher.EnrichmentProcessor" /&gt;

    &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
        &lt;property name="entityManagerFactory" value="#{entityManagerFactoryRegistry.emf}" /&gt;
    &lt;/bean&gt;

    &lt;bean id="entityWriter" class="batch.enricher.EntityWriter"&gt;
        &lt;property name="entityManagerFactory" value="#{entityManagerFactoryRegistry.emf}" /&gt;
    &lt;/bean&gt;

    &lt;bean id="hibernateItemReader"
    class="org.springframework.batch.item.database.JpaPagingItemReader"&gt;
        &lt;property name="queryString" value="from Relation" /&gt;
        &lt;property name="entityManagerFactory" value="#{entityManagerFactoryRegistry.emf}" /&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</code></pre>

<p>The implementations couldn't be more trivial I guess: </p>

<pre><code>public class EntityWriter extends JpaItemWriter&lt;Relation&gt; {
}

public class EnrichmentProcessor implements ItemProcessor&lt;Relation, Relation&gt;
{
    @Override
    public Relation process(Relation item) throws Exception
    {
        // Do something with EACH item...
        // Logging revealed that all items are correctly processed
        return item;
    }
}

public class JobListener implements JobExecutionListener
{
    @Override
    public void beforeJob(JobExecution jobExecution)
    {
        // Perform some trivial initializing...
    }

    @Override
    public void afterJob(JobExecution jobExecution)
    {
    }
}
</code></pre>

<p>I already tried to overwrite the appropriate writing methods in <code>EntityWriter</code> (doWrite and write), without remarkable success. I'm using <code>CommandLineJobRunner</code> to run the code. </p>

<p><strong>EDIT</strong>: 
For a total 64 items, the first 60 items are updated correctly, using a commit-interval and pagesize of 10. I grepped the log to only include the interesting statements. I only included the last two pagereads (items 50 - 64)</p>

<pre><code>[DEBUG] 2016-04-13 11:29:54.753 [main] JpaTransactionManager - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:54.753 [main] JpaTransactionManager - Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@377a58f6] for JPA transaction
[DEBUG] 2016-04-13 11:29:54.754 [main] JpaPagingItemReader - Reading page 5
[DEBUG] 2016-04-13 11:29:54.831 [main] JpaItemWriter - Writing to JPA with 10 items.
[DEBUG] 2016-04-13 11:29:54.889 [main] JpaItemWriter - 10 entities merged.
[DEBUG] 2016-04-13 11:29:54.889 [main] JpaItemWriter - 0 entities found in persistence context.
[DEBUG] 2016-04-13 11:29:54.890 [main] ResourcelessTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.updateExecutionContext]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:54.892 [main] ResourcelessTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:54.892 [main] ResourcelessTransactionManager - Committing resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@3d7c7168]
[DEBUG] 2016-04-13 11:29:54.892 [main] ResourcelessTransactionManager - Resuming suspended transaction after completion of inner transaction
[DEBUG] 2016-04-13 11:29:54.892 [main] ResourcelessTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.update]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:54.898 [main] ResourcelessTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:54.899 [main] ResourcelessTransactionManager - Committing resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@337df06d]
[DEBUG] 2016-04-13 11:29:54.899 [main] ResourcelessTransactionManager - Resuming suspended transaction after completion of inner transaction
[DEBUG] 2016-04-13 11:29:54.899 [main] JpaTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:54.899 [main] JpaTransactionManager - Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@377a58f6]
[DEBUG] 2016-04-13 11:29:54.900 [main] JpaTransactionManager - Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@377a58f6] after transaction
[DEBUG] 2016-04-13 11:29:54.901 [main] JpaTransactionManager - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:54.902 [main] JpaTransactionManager - Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4bc82040] for JPA transaction
[DEBUG] 2016-04-13 11:29:54.904 [main] JpaPagingItemReader - Reading page 6
[DEBUG] 2016-04-13 11:29:54.959 [main] JpaItemWriter - Writing to JPA with 4 items.
[DEBUG] 2016-04-13 11:29:54.986 [main] JpaItemWriter - 4 entities merged.
[DEBUG] 2016-04-13 11:29:54.986 [main] JpaItemWriter - 0 entities found in persistence context.
[DEBUG] 2016-04-13 11:29:54.987 [main] ResourcelessTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.updateExecutionContext]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:54.988 [main] ResourcelessTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:54.988 [main] ResourcelessTransactionManager - Committing resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@1d8fdd4f]
[DEBUG] 2016-04-13 11:29:54.988 [main] ResourcelessTransactionManager - Resuming suspended transaction after completion of inner transaction
[DEBUG] 2016-04-13 11:29:54.988 [main] ResourcelessTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.update]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:54.991 [main] ResourcelessTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:54.991 [main] ResourcelessTransactionManager - Committing resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@1b446bed]
[DEBUG] 2016-04-13 11:29:54.992 [main] ResourcelessTransactionManager - Resuming suspended transaction after completion of inner transaction
[DEBUG] 2016-04-13 11:29:54.992 [main] JpaTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:54.992 [main] JpaTransactionManager - Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4bc82040]
[DEBUG] 2016-04-13 11:29:54.994 [main] JpaTransactionManager - Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4bc82040] after transaction
[DEBUG] 2016-04-13 11:29:54.995 [main] ResourcelessTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.updateExecutionContext]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:54.996 [main] ResourcelessTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:54.997 [main] ResourcelessTransactionManager - Committing resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@5c2dd133]
[DEBUG] 2016-04-13 11:29:54.997 [main] ResourcelessTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.update]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:55.002 [main] ResourcelessTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:55.002 [main] ResourcelessTransactionManager - Committing resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@2bb9da60]
[DEBUG] 2016-04-13 11:29:55.003 [main] ResourcelessTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.updateExecutionContext]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:55.005 [main] ResourcelessTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:55.005 [main] ResourcelessTransactionManager - Committing resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@358cce2e]
[DEBUG] 2016-04-13 11:29:55.008 [main] ResourcelessTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.update]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[DEBUG] 2016-04-13 11:29:55.012 [main] ResourcelessTransactionManager - Initiating transaction commit
[DEBUG] 2016-04-13 11:29:55.012 [main] ResourcelessTransactionManager - Committing resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@5875c7d8]
</code></pre>