Spring+Hibernate: Inserting mutliple records and @Transactional
<p>I have an application built on Spring+Hibernate. I'm trying to insert data into the database from a list of entries in a file. The basic idea is to insert all the records which have valid data and log an error report for all the entries that haven't been inserted. </p>

<p>A service class, which is @Transactional, calls another service class to read the file and insert into the DB. At moment, I insert the records one after the other. Maybe I'll change this to Batch Processing. The insertion is executed in a try catch block.</p>

<p>The pseudo-code:</p>

<pre><code>@Transactional
class MainService {     
     public void insertFromFile(File inputFile) {
          subServuce(toIterator(inputFile)); // toIterator(..) is just for understanding. 
     } 
}

class SubService {
    public void insert(Iterator input) {
        while (input.hasNext()) {
             try {
                 DBService.save(input.next());
             } catch (Exception e) {
                 // Log the error and continue with next records
             }
        }
    }
}

@Transactional
class DBService {
     public void save(Data input) {
          generalDao.save(input);
     }
}
</code></pre>

<p>Tried:</p>

<pre><code>class SubService {
    public void insert(Iterator input) {
        while (input.hasNext()) {
             try {
                 save(input.next());
             } catch (Exception e) {
                 // Log the error and continue with next records
             }
        }
    }

    // @Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = Exception.class)
    @Transactional(propagation = Propagation.NOT_SUPPORTED, noRollbackFor = Exception.class)
    public void save(Data input) {
        DBService.save(input);
    }
}
</code></pre>

<p>Even though the exception is handled, the entire transaction is being rolled back and I get this error <code>Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly</code></p>

<p>I tried specifying <code>@Transactional(noRollBack = Exception.class)</code> on <code>SubService.insert(..)</code>, that did not work. </p>

<p>The following are my questions:</p>

<ol>
<li>How do I tell Spring not to roll back and continue with the other records.</li>
<li>Is it good practice to not to roll back while inserting multiple records? Doesn't this defy the Atomicity rule?</li>
<li><p>In batch processing, how is this handled? If an error occurs are all inserted or none?</p>

<p>Solution:</p></li>
</ol>

<p>With the help of @Madhusudana Reddy Sunnapu's answer, was able to solve this. </p>

<p>Created a layer between SubService and DBService with the Propagation.REQUIRES_NEW. The issue was that even though SubService.save(..) is annotated with @Transactional, as it belongs to the same Bean, the proxy will not treat it as a different session. </p>

<pre><code>SubServiceDao {
    // @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void save(Data input) {
        DBService.save(input);
    }
}

class SubService {
    public void insert(Iterator input) {
        while (input.hasNext()) {
             try {
                 SubServiceDao.save(input.next());
             } catch (Exception e) {
                 // Log the error and continue with next records
             }
        }
    }
}
</code></pre>