title: Continue with transaction after exception - JPA
body: <p>I am using JPA with Spring. I am trying to do batch import. If there is problem with batch import then I would like to insert individually, and if this fails also then I would like to save to duplicates table. I wrote a logic for this but I get this error everytime:</p>
  
  <pre><code>Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
  </code></pre>
  
  <p>Mine setting for JPA are like this:</p>
  
  <pre><code>@Bean(name = "dataSource", destroyMethod = "")
    public DataSource getDataSource() {
      return new JndiDataSourceLookup().getDataSource(props.getDbJndiName());
    }
  
    @Bean
    public JpaVendorAdapter getHibernateJpaVendorAdapter() {
      return new HibernateJpaVendorAdapter();
    }
  
    @Bean
    public LocalContainerEntityManagerFactoryBean getEntityManagerFactoryBean() {
      LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean();
      lcemfb.setDataSource(getDataSource());
      lcemfb.setPersistenceUnitName("MyPU");
      lcemfb.setPackagesToScan("com.project");
      lcemfb.setJpaVendorAdapter(getHibernateJpaVendorAdapter());
      lcemfb.setJpaProperties(getHibernateProperties());
      return lcemfb;
    }
  
    @Bean
    public Properties getHibernateProperties() {
      Properties jpaProperties = new Properties();
      jpaProperties.put(DIALECT, "org.hibernate.dialect.Oracle10gDialect");
      jpaProperties.put(SHOW_SQL, true);
      jpaProperties.put(AUTOCOMMIT, true);
      jpaProperties.put(FORMAT_SQL, true);
      jpaProperties.put(USE_SQL_COMMENTS, true);
      jpaProperties.put(STATEMENT_BATCH_SIZE, 20);
      jpaProperties.put(ORDER_INSERTS, true);
      jpaProperties.put("hibernate.ejb.entitymanager_factory_name", "MyEM");
      return jpaProperties;
    }
  
    @Bean
    public JpaTransactionManager getTransactionManager() {
      return new JpaTransactionManager(getEntityManagerFactoryBean().getObject());
    }
  
    @Bean
    public PersistenceExceptionTranslationPostProcessor getPersistenceExceptionTranslationPostProcessor() {
      return new PersistenceExceptionTranslationPostProcessor();
    }
  </code></pre>
  
  <p>I get entity manager like this</p>
  
  <pre><code>@PersistenceContext(unitName = "MyPU")
    private EntityManager em;
  
    protected EntityManager em() {
      return em;
    }
  </code></pre>
  
  <p>my import method is:</p>
  
  <pre><code>  @Override
    @Transactional
    public void importBusinessFile(MultipartFile file)
        throws GeneralException, IOException {
      // process file
  
      //save batch
      dealsRepository.saveBatch(deals);
    }
  </code></pre>
  
  <p>and saveBatch method from repository:</p>
  
  <pre><code>  public void saveBatch(List&lt;Deal&gt; list) {
      for (Deal deal : list) {
        em().persist(deal);
      }
  
      try {
        em().flush();
        em().clear();
      } catch (Exception e) {
        log.info("Duplicates detected, save individually.", e);
  
        for (Deal deal : list) {
          try {
            save(deal);
          } catch (Exception ex) {
            log.error("Problem saving individual deal", e);
            // TODO write to duplicates
          }
        }
      }
    }
  </code></pre>
  
  <p>I tried setting dontRollbackOn but I can't get past this exception. I found some other similar threads but none helped me. </p>