Results 1 to 3 of 3

Thread: Problem with JdbcTemplate and transactions not rolling back

  1. #1
    Join Date
    Jun 2010
    Posts
    5

    Default Problem with JdbcTemplate and transactions not rolling back

    I'm having an issue trying to implement transactions in a spring mvc project - when I throw an exception, the inserts I've performed aren't rolled back.

    I've confirmed that DataSourceTransactionManager is being used - I've stepped through the code in debug and watched doBegin and doRollback being called, but I can't see any reason why my inserts are remaining.

    Here's the gist of the code. The insert() method is called from within an @Controller class which I've omitted for brevity.

    Code:
    	<context:component-scan base-package="com.test"/>
    	
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"/>
    	</bean>
    	
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource" ref="dataSource"/>
    	</bean>
    	
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    		<property name="url" value="jdbc:mysql://127.0.0.1:3306/sso"/>
    		<property name="username" value="ausername"/>
    		<property name="password" value="apassword"/>
    		<property name="defaultAutoCommit" value="false"/>
    	</bean>
    Code:
    package com.test;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    @Service
    public class TestDao {
    
    	private static final String CLEAR_SQL = 
    		"delete from test";
    	
    	private static final String INSERT_SQL = 
    		"insert into test (id, name) values (?, ?)";
    	
    	@Autowired
    	private JdbcTemplate jdbcTemplate; 
    	
    	public void clear() {
                jdbcTemplate.update(CLEAR_SQL, new Object[] {});
    	}
    
    	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    	public void insert() {
                jdbcTemplate.update(INSERT_SQL, new Object[] {1, "alpha"});
                jdbcTemplate.update(INSERT_SQL, new Object[] {2, "beta"});
                throw new RuntimeException();
    	}
    	
    }

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Please use the search.... This question is being answered 10 times a week.

    1) Make sure you use InnoDB tables, MyISAM tables aren't transactional
    2) there is no tx:annotation-driven, so basically your @Transactional is useless.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2010
    Posts
    5

    Default

    Thanks Martin

    I already searched. None of the solutions I found worked for me. The innodb tables suggestion sounds a good candidate though

    The sourced I pasted above includes a tx:annotation-driven tag btw.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •