Announcement Announcement Module
Collapse
No announcement yet.
transactionManager does not roll back transaction on Exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • transactionManager does not roll back transaction on Exception

    Below are codes from my xml file

    Code:
    <bean id="testDao" class="myapp.TestImpl">
    	<property name="dataSource" ref="myDataSource" />
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="myDataSource" />
    </bean>
    <bean id="testDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    	<property name="proxyInterfaces">
    		<list>
    			<value>
    				myapp.TestDao
    			</value>
    		</list>
    	</property>
    	<property name="target">
    		<ref bean="testDao" />
    	</property>
    	<property name="transactionManager">
    		<ref bean="transactionManager" />
    	</property>
    	<property name="transactionAttributes">
    		<props>
    			<prop key="*">
    				PROPAGATION_REQUIRED,-Exception
    			</prop>
    		</props>
    	</property>
    </bean>
    and TestImpl.java
    Code:
    public class TestImpl extends SimpleJdbcDaoSupport implements TestDao{
    	public void insertTest(List<String> list) throws Exception {
    		for (String string : list) {
    			getJdbcTemplate().update("insert into testTab(pcode) values(?)", new Object[]{string});
    		}
    	}
    }
    The List<String> contains 4 values. First 3 values are good but the 4th value violates PK constraint. I expected that the transaction will roll back and no insertion will be done. But it not working - first 3 insertions are done. Any help?
    Last edited by satfaltu; Nov 2nd, 2010, 09:17 AM.

  • #2
    1) Make sure you use the proxied instance.
    2) If you use MySQL use InnoDB tables, MyISAM tables aren't transactional
    3) Don't use TransactionalProxyFactoryBean consider it deprecated, use aop:config or @Transactional (see tx chapter in reference guide).
    4) How are yuou testing? Make sure you use an ApplicationContext to loadyour xml and not a BeanFactory

    And please use the search as this question has been answered numerous times before....

    Comment


    • #3
      @Transactional does not rolback upon exception

      I am using @Transactional in the service layer which in turn calls corresponding DAO methods, still there is no rollback when I simulate a database constraint violation...
      1. I don't specify a rollback attribute on the annotation, since it is implicit for RuntimeException errors.
      2. Database tables are MySQL InnoDB tables
      3. DAO employs JdbcTemplate with SQL; problem method performs one insert of parent instance with multiple (batch) inserts of child instances
      The entire application (Spring MVC web application) with source code (sans lib/JARs) and the MySQL database script is attached. What am I missing?

      Please help.

      Ashwin

      Comment


      • #4
        You are using component scanning in both context, basically leading to 2 instances of your service, 1 transactional, 1 not transactional. The latter is used... Properly configure your component scanning, the applicationContext.xml should not scan controllers, the dispatcher servlet should only scan for @Controllers. (For more info do a forum search as that question has been answered numerous times before).

        Comment


        • #5
          Component scanning overly done

          Marten, thank you very, very much for pointing out my oversight! Here is how I attempted to specify component scanning across both the root context as well as that defined by DispatcherServlet:
          Code:
          In applicationContext.xml:
          	<context:component-scan base-package="net.company.internal">
          		<context:exclude-filter type="regex" expression="net\.company\.internal\.*web.*"/>
          	</context:component-scan>
          
          In mvc-dispatcher-servlet.xml:
          	<!-- Scans the class path of this application for web-related @Component to deploy as beans -->
          	<context:component-scan use-default-filters="false" base-package="net.company.internal">
          		<context:include-filter type="regex" expression="net\.company\.internal.*web.*"/>
          	</context:component-scan>
          There are probably better ways to do this, but this is working for me right now.

          Thanks!

          Ashwin

          Comment

          Working...
          X