Spring学习 事务回滚

<span style="font-size:24px;">	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.<strong>DataSourceTransactionManager</strong>">
		<property name="dataSource" ref="dataSource" />
	</bean></span>



<span style="font-size:24px;">//	RuntimeException @Transactional(noRollbackFor = RuntimeException.class)
//	Exception @Transactional(rollbackFor = Exception.class)
	public void deleteUser(String id) {
		String sqlStr = "delete  from t_user where user_id = ? ";
		jdbcTemplate.update(sqlStr, new Object[] { id });
		throw new RuntimeException("----运行期异常----");
	}</span>

结论:默认情况下,unchecked exception是会回滚的。checked exception不会回滚。当然可以通过配置文件爱你进行修改。如上面的注释部分。


<strong style="background-color: rgb(102, 255, 255);">@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true,timeout=3000)</strong>
	@SuppressWarnings("deprecation")
	public int getMatchCount(String userName, String password) {
		String sqlStr = "SELECT count(*) FROM t_user WHERE user_name=? and password=?";
		return jdbcTemplate.queryForInt(sqlStr, new Object[] { userName,
				password });
	}

事务的传播行为 有7种,Propagation.xxxxx . 如 RQUIRED/NO_SUPPORTED/NEVER/REQUIRED_NEW/MANDATORY/SUPPORTS/NESTED.

其中NESTED是Spring特有的。有保存点的说法。内部出错,对外部嵌套的没影响。但外部出错了,内部就要回滚的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值