Spring3 事务不回滚的大问题

 最近在学习Spring,在配置事务管理器的时候发现怎么也不回滚,求解!

Beans.xml配置

<context:component-scan base-package="com.spring.test" />
	<tx:annotation-driven transaction-manager="transactionManager"/>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/test2?useUndicode=true&amp;characterEncoding=utf-8" />
		<property name="username" value="root" />
		<property name="password" value="weihua" />
		<property name="defaultAutoCommit" value="true"></property>
	</bean>
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

访问数据库的代码

@Repository("userDAO")
@Transactional
public class UserDAOImp implements UserDAO {

	private JdbcTemplate jdbcTemplate;

	@Resource(name = "dataSource")
	public void setDataSource(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override 
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void save(User user) throws Exception{
		jdbcTemplate.update("insert into _USER(name) values (?)", new Object[] { user
				.getName() }, new int[] {Types.VARCHAR});
				throw new Exception("Error");
		
	}

}

 JUint测试代码:

public class TransactionTest {
	private static UserDAO userDAO;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
		userDAO=(UserDAO) context.getBean("userDAO");
	}

	@Test
	public void testSave() throws Exception {
		User user=new User("袁伟华");
			userDAO.save(user);
		
	}

}

我一开始抛出的是RuntimeException,可是还是能够保存成功!也试了@Transactional(propagation=Propagation.REQUIRED,rollbackFor={Exception.class})不加花括号,结果都是一样的不回滚!不知道问题是出在哪了,在线解释……

相关推荐