views:

89

answers:

1

I am using spring 3.0.3.RELEASE along with mybatis-3.0.2 and mybatis-spring-1.0.0 running in Apache Tomcat 6.0.29 with JDK 1.6.0_21.

I created my DAO class and Service class and defined following declarative transaction control -

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="dtxops"
            expression="execution(* com.project.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="dtxops" />
    </aop:config>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

This method is in the class com.project.service.ItemDAOServiceImpl which uses ItemDAO. The SystemException is a RunTimeException. I pass 2 ids to be deleted, one id exists in the system and other does not. Since one id does not exists I get the SystemException but when I check the database the other id is deleted instead of a rollback.

public void deleteItem(List<Integer> itemIds) {
        for (int itemId : itemIds) {
            try {
                int result = itemDAO.delete(itemId);
                if (result != 1) {
                    throw new SystemException(
                            "Failed to delete item");
                }
            } catch (DataAccessException dae) {
                log.error("Failed to delete item", dae);
                throw new SystemException("Failed to delete items");
            }
        }
    }
+1  A: 

The transaction config is around the itemDao right? So each itemDAO.delete call is a separate transaction. So if the first id is found it is deleted in one txn. For second one it wont find, the exception is thrown outside the txn - no rollback.

It sounds like you need to setup the txn around deleteItem method instead.

aar