<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特有的。有保存点的说法。内部出错,对外部嵌套的没影响。但外部出错了,内部就要回滚的