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

  • transactionManager does not roll back transaction on Exception

    Below are codes from my xml file

    <bean id="testDao" class="myapp.TestImpl">
    	<property name="dataSource" ref="myDataSource" />
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="myDataSource" />
    <bean id="testDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    	<property name="proxyInterfaces">
    	<property name="target">
    		<ref bean="testDao" />
    	<property name="transactionManager">
    		<ref bean="transactionManager" />
    	<property name="transactionAttributes">
    			<prop key="*">
    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, 10: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....


    • #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.



      • #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).


        • #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:
          In applicationContext.xml:
          	<context:component-scan base-package="">
          		<context:exclude-filter type="regex" expression="net\.company\.internal\.*web.*"/>
          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="">
          		<context:include-filter type="regex" expression="net\.company\.internal.*web.*"/>
          There are probably better ways to do this, but this is working for me right now.