title: Persist issue with a spring batch ItemWriter using a JPA repository
body: <p>I have an issue with a spring batch <code>ItemWriter</code> that relies on a JPA repository in order to update data.</p>
  
  <p>Here it is:</p>
  
  <pre><code>@Component
  public class MessagesDigestMailerItemWriter implements ItemWriter&lt;UserAccount&gt; {
  
      private static final Logger log = LoggerFactory.getLogger(MessagesDigestMailerItemWriter.class);
  
      @Autowired
      private MessageRepository messageRepository;
  
      @Autowired
      private MailerService mailerService;
  
      @Override
      public void write(List&lt;? extends UserAccount&gt; userAccounts) throws Exception {
          log.info("Mailing messages digests and updating messages notification statuses");
  
          for (UserAccount userAccount : userAccounts) {
              if (userAccount.isEmailNotification()) {
                  mailerService.mailMessagesDigest(userAccount);
              }
              for (Message message : userAccount.getReceivedMessages()) {
                  message.setNotificationSent(true);
                  messageRepository.save(message);//NOT SAVING!!
              }
          }
      }
  }
  </code></pre>
  
  <p>Here is my <code>Step</code> configuration:</p>
  
  <pre><code>@Configuration
  public class MailStepConfiguration {
  
      @Autowired
      private StepBuilderFactory stepBuilderFactory;
  
      @Autowired
      private EntityManagerFactory entityManagerFactory;
  
      @Autowired
      private MessagesDigestMailerItemWriter itemWriter;
  
      @Bean
      public Step messagesDigestMailingStep() {
          return stepBuilderFactory.get("messagesDigestMailingStep")//
                  .&lt;UserAccount, UserAccount&gt; chunk(1)//
                  .reader(jpaPagingItemReader(entityManagerFactory))//
                  .writer(itemWriter)//
                  .build();
      }
  
      @Bean(destroyMethod = "")
      @StepScope
      public static ItemReader&lt;UserAccount&gt; jpaPagingItemReader(EntityManagerFactory entityManagerFactory) {
          final JpaPagingItemReader&lt;UserAccount&gt; reader = new JpaPagingItemReader&lt;&gt;();
          reader.setEntityManagerFactory(entityManagerFactory);
          reader.setQueryString("SELECT ua FROM UserAccount ua JOIN FETCH ua.receivedMessages msg WHERE msg.notificationSent = false AND msg.messageRead = false");
          return reader;
      }
  
  }
  </code></pre>
  
  <p>For completeness sake, here is my spring boot configuration:</p>
  
  <pre><code>@Configuration
  @EnableBatchProcessing
  @EnableAutoConfiguration
  @ComponentScan("com.bignibou.batch.configuration")
  public class Batch {
      public static void main(String[] args) {
          System.exit(SpringApplication.exit(new SpringApplicationBuilder(Batch.class).web(false).run(args)));
      }
  }
  </code></pre>
  
  <p>and my datasource config:</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories({ "com.bignibou.repository" })
  @EntityScan("com.bignibou.domain")
  public class DatasourceConfiguration {
  
      @Bean
      @ConfigurationProperties("spring.datasource.batch")
      public DataSource batchDatasource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      @Primary
      @ConfigurationProperties("spring.datasource.application")
      public DataSource applicationDatasource() {
          return DataSourceBuilder.create().build();
      }
  }
  </code></pre>
  
  <p>I noticed that the execution flow get into the ItemWriter's write method and the <code>messageRepository.save(message);</code> does get executed but the data is not updated.</p>
  
  <p>I suspect it is a transaction issue but I am not sure how to solve this problem...</p>
  
  <p><strong>edit</strong>: I forgot to mention that I have <strong>two</strong> Postgres databases: </p>
  
  <ol>
  <li>one for the job repository data </li>
  <li>another one for the application data. </li>
  </ol>
  
  <p>I can confirm that data is written into the job repository database. The issue is with the application data. I am required to use distributed transactions bearing in mind the fact that I have two PG databases?</p>