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(); } }


Reply With Quote