spring进阶(二)事务

本文介绍了Spring框架中的事务管理机制,包括事务回滚的条件、事务配置的方法及如何通过AOP实现事务控制。重点讲解了在Service层配置事务,并强调了异常处理对于事务回滚的重要性。

1. spring事务的回滚

    spring的事务,一般配置在service层

    spring事务中,不能使用try{...}catch{.....},否则不会发生回滚

     service层抛出的异常,必须是RunTimeException或者其它子类的异常,否则不会发生回滚

示例:

public void save() throws Exception{
		User user = new User() ;
		user.setId("111111") ;
		user.setName("xiaoming") ;
		user.setPassword("password") ;
		userMapper.insert(user) ; 
		save2();
	}
	
	public void save2() throws Exception{
		User user1 = new User() ;
		user1.setId("222222") ;
		user1.setName("xiaoxi") ;
		user1.setPassword("2222222222") ;
		if(1!=3){
			throw new RuntimeException();
		}
		userMapper.insert(user1) ;
	}


2. spring事务的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation=" 
       	   http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
           http://www.springframework.org/schema/aop      
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 	   
           ">
	<!--  配置事务管理器,配置切面,配置切面的行为 -->
	
	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 
	               切面的行为,下面配置切面
	 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" /><!-- 在事务中执行 -->
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" /><!-- 有事务,在事务中执行,没有的话,不会开启新的事务 -->
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 配置切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.whir.springmvc.service.*.*(..))" />
	</aop:config>
	

</beans>




数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究(Matlab代码实现)内容概要:本文围绕“数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究”展开,提出了一种结合数据驱动与分布鲁棒优化方法的建模框架,用于解决电热综合能源系统在不确定性环境下的优化调度问题。研究采用两阶段优化结构,第一阶段进行预决策,第阶段根据实际场景进行调整,通过引入1-范数和∞-范数约束来构建不确定集,有效刻画风电、负荷等不确定性变量的波动特性,提升模型的鲁棒性和实用性。文中提供了完整的Matlab代码实现,便于读者复现和验证算法性能,并结合具体案例分析了不同约束条件下系统运行的经济性与可靠性。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及工程技术人员,尤其适合从事综合能源系统、鲁棒优化、不确定性建模等相关领域研究的专业人士。; 使用场景及目标:①掌握数据驱动的分布鲁棒优化方法在综合能源系统中的应用;②理解1-范数和∞-范数在构建不确定集中的作用与差异;③学习两阶段鲁棒优化模型的建模思路与Matlab实现技巧,用于科研复现、论文写作或工程项目建模。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现细节,重点关注不确定集构建、两阶段模型结构设计及求解器调用方式,同时可尝试更换数据或调整约束参数以加深对模型鲁棒性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值