首页 > 其他 > 详细

SSM 事务

时间:2017-12-27 12:55:12      阅读:197      评论:0      收藏:0      [点我收藏+]

配置文件

<!-- 定义事务管理器 -->
<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{
}
}
}

抛出异常事物回滚

SSM 事务

原文:https://www.cnblogs.com/SeaWxx/p/8124830.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!