The problem: Import a large number of items into a database, commiting valid ones and keeping track of invalid ones. All in a single transaction.

Traditional solution: Tedious to write. Programatically start a transaction and set a savepoint for each processed item. Rollback to savepoint if an exception occurs or commit if all ok.

Spring solution: Easy, just modifying the transaction demarcation. Along with traditional EJB transaction demarcation startegies, PROPAGATION_NESTED provides the needed behaviour.

Have the descriptor look like:

<bean id=”txProxyTemplate” abstract=”true” class=”org.springframework.transaction.interceptor.TransactionProxyFactoryBean”>

<property name=”transactionManager” ref=”transactionManager”/>
<property name=”transactionAttributes”>


<prop key=”import*”>PROPAGATION_REQUIRED</prop>


<prop key=”importNewItem”>PROPAGATION_NESTED, -ImportException</prop>

<prop key=”*”>PROPAGATION_REQUIRED, readOnly</prop>




And code like:

void import(){

while( moreToImport) {

try {


}catch(ImportException ie){






The outer method (import) will start the transaction and inner method (importNewItem) will start nested transactions. The programmer can choose to catch inner methods exceptions, thus commiting the outer transaction, or let them propagate, rolling back the outer transaction.

More on nested transactions:

One response to “Spring magic – Nested transactions

  1. What is the transaction manager used ?

    I want to use nested transaction using spring’s declarative transaction management. we are already using JPA transaction manager which doesn’t support nested transaction (propagation). Even JTA implementation like Atomikos doesn’t support it. It seems like only transaction manager I can use is DataSourceTransactionManager (which I am not sure if I can use with JPA).

    Do you know any alternatives to use spring’s declarative tx management for nested transactions.



Leave a Comment:

Your email address will not be published. Required fields are marked *