Question

I am using Spring3, Java 6, and Oracle. I have a service class, implementing an interface, that calls a DAO via an interface.

I want the 1st insert to rollback if the 2nd fails. For the 2nd insert, I am just throwing a RuntimeException to test the rollback of the 1st. It never rolls back. I am left with a row in the database.

Is there a step I have missed? I made sure to use an interface since I have read that Spring uses AOP proxies for transactions that require and interface.

Service:

@Service
public class APIServiceImpl implements APIService {

    @Override
    @Transactional
    public void testPromotion(int duration, String productId, String offerId) {
        Integer promotionId = apidao.insertPromotion("3", productId);
        Integer test = apidao.insertTest(offerId, productId);   //runtime exception
    }
}

DAO:

@Component
public class ApiDaoImpl implements ApiDao {

   public Integer insertPromotion(final int offerId, final String promoCode) {
        final String sqlText = "INSERT INTO promotion ("
                + "                promotion_id, "
                + "                offer_id, "
                + "                promotion_code, ") "
                + "             VALUES ("
                + "                seq_partner_promotion.nextval, "
                + "                ?, "
                + "                ?, ") ";


        return myJdbcTemplate.update(sqlText, offerId, promoCode);
    }

    public Integer insertTest(final int offerId, final String promoCode) {
        throw new RuntimeException();
    }

App Context:

<bean name="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="myDataSource"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="myDataSource"/>
</bean>
Was it helpful?

Solution

How about marking ApiDaoImpl with @Repository (instead of @Component) and @Transactional (to make sure it participates in the same transaction as the caller)?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top