SSM 事务

配置文件

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

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

<!-- 定义事务策略 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--所有以query开头的方法都是只读的 -->
<tx:method name="query*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
<!--其他方法使用默认事务策略 -->
<tx:method name="*" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="myPointcut" expression="execution(* cc.openkit..service.*.*(..))" />
<!--将定义好的事务处理策略应用到上述的切入点 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
</aop:config>


service层


@Service
public class AppConfigService extends BaseService<AppConfig>{

@Autowired
private AppConfigMapper appConfigMapper;

@Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public void test() {

AppConfig appConfig = new AppConfig();
appConfig.setUpBaseId(AppUtil.uuid());
AppConfig appConfig1 = new AppConfig();
appConfig1.setUpBaseId(AppUtil.uuid());
Integer integer = this.saveSelect(appConfig);
Integer integer1 =this.saveSelect(appConfig1);
Integer num=integer + integer1;
if (num==2){
System.out.println(1);
throw new RuntimeException("出现异常了");
}else{
}
}
}

抛出异常事物回滚
posted @ 2017-12-27 11:19  Sea_wxx  阅读(369)  评论(0编辑  收藏  举报