English 中文(简体)
Spring declarative transaction management not working
原标题:

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");
            }
        }
    }
问题回答

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.

Watch out for Tomcats auto-commit setting for datasources which commits after each statement. It sounds a bit like that to me. I have faced that problem once, not fun...





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签