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&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})不加花括号,结果都是一样的不回滚!不知道问题是出在哪了,在线解释……
相关推荐
yupi0 2020-10-10
spring 2020-08-18
编程点滴 2020-07-29
幸运小侯子 2020-07-05
itjavashuai 2020-07-04
qingjiuquan 2020-06-29
shushan 2020-06-25
小鱿鱼 2020-06-22
咻pur慢 2020-06-18
melonjj 2020-06-17
qingjiuquan 2020-06-13
neweastsun 2020-06-05
小鱿鱼 2020-06-05
mxcsdn 2020-05-31
吾日五省我身 2020-05-27
牧场SZShepherd 2020-05-27
sweetgirl0 2020-05-14