SSM Day7

Spring——事务

在这里插入图片描述

6.1 数据库事务并发问题
假设现在有两个事务:Transaction01和Transaction02并发执行。
①脏读
[1]Transaction01将某条记录的AGE值从20修改为30。
[2]Transaction02读取了Transaction01更新后的值:30。
[3]Transaction01回滚,AGE值恢复到了20。
[4]Transaction02读取到的30就是一个无效的值。
②不可重复读
[1]Transaction01读取了AGE值为20。
[2]Transaction02将AGE值修改为30。
[3]Transaction01再次读取AGE值为30,和第一次读取不一致。
③幻读
[1]Transaction01读取了STUDENT表中的一部分数据。
[2]Transaction02向STUDENT表中插入了新的行。
[3]Transaction01读取了STUDENT表时,多出了一些行。
6.2 隔离级别
数据库系统必须具有隔离并发运行各个事务的能力,使它们不会相互影响,避免各种并发问题。一个事务与其他事务隔离的程度称为隔离级别。SQL标准中规定了多种事务隔离级别,不同隔离级别对应不同的干扰程度,隔离级别越高,数据一致性就越好,但并发性越弱。
①读未提交:READ UNCOMMITTED
允许Transaction01读取Transaction02未提交的修改。
②读已提交:READ COMMITTED
要求Transaction01只能读取Transaction02已提交的修改。
③可重复读:REPEATABLE READ
确保Transaction01可以多次从一个字段中读取到相同的值,即Transaction01执行期间禁止其它事务对这个字段进行更新。
④串行化:SERIALIZABLE
确保Transaction01可以多次从一个表中读取到相同的行,在Transaction01执行期间,禁止其它事务对这个表进行添加、更新、删除操作。可以避免任何并发问题,但性能十分低下。
⑤各个隔离级别解决并发问题的能力见下表
脏读	不可重复读	幻读READ UNCOMMITTED	有	有	有READ COMMITTED	无	有	有REPEATABLE READ	无	无	有SERIALIZABLE	无	无	无

⑥各种数据库产品对事务隔离级别的支持程度
在这里插入图片描述

tx.xml(注解方式)

<?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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<context:component-scan base-package="com.atguigu"></context:component-scan>


	<!-- 0、引入外部配置文件 -->
	<context:property-placeholder location="classpath:dbconfig.properties" />
	<!-- 1、配置数据源 -->
	<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
	</bean>
	<!-- 2、配置JdbcTemplate操作数据库   value="#{pooledDataSource}"  ref="pooledDataSource"-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" value="#{pooledDataSource}"></property>
	</bean>
	
	<!-- 3、配置声明式事务
		1)、Spring中提供事务管理器(事务切面),配置这个事务管理器
		2)、开启基于注解的事务式事务;依赖tx名称空间
		3)、给事务方法加注解
	 -->
	 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="pooledDataSource"></property>
	 </bean>
	 
	 <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

BookService.java

@Service
public class BookService {
	
	@Autowired
	BookDao bookDao;
	
	
	
//	@Autowired
//	BookService bookService;
	
	/**
	 * 事务细节:
	 * isolation-Isolation:事务的隔离级别;
	 * 
	 * 
	 * 
	 * noRollbackFor-Class[]:哪些异常事务可以不回滚
	 * noRollbackForClassName-String[](String全类名):
	 * 
	 * rollbackFor-Class[]:哪些异常事务需要回滚;
	 * rollbackForClassName-String[]:
	 * 
	 * 异常分类:
	 * 		运行时异常(非检查异常):可以不用处理;默认都回滚;
	 * 		编译时异常(检查异常):要么try-catch,要么在方法上声明throws
	 * 				默认不回滚;
	 * 
	 * 事务的回滚:默认发生运行时异常都 回滚,发生编译时异常不会回滚;
	 * noRollbackFor:哪些异常事务可以不回滚;(可以让原来默认回滚的异常给他不回滚)
	 * 	noRollbackFor={ArithmeticException.class,NullPointerException.class}
	 * noRollbackForClassName
	 * 
	 * rollbackFor:原本不回滚(原本编译时异常是不回滚的)的异常指定让其回滚;
	 * 
	 * readOnly-boolean:设置事务为只读事务:
	 * 		可以进行事务优化;
	 * 		readOnly=true:加快查询速度;不用管事务那一堆操作了。
	 * 
	 * timeout-int(秒为单位):超时:事务超出指定执行时长后自动终止并回滚
	 * @throws FileNotFoundException 
	 * 
	 * 
	 * propagation-Propagation:事务的传播行为;
	 * 	传播行为(事务的传播+事务的行为);
	 * 		如果有多个事务进行嵌套运行,子事务是否要和大事务共用一个事务;
	 * 传播行为:
	 * AService{
	 * 		tx_a(){
	 * 			//a的一些方法
	 * 			tx_b(){
	 * 			}
	 * 			tx_c(){
	 * 			}
	 * 		}
	 * }
	 * 
	 * 
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void checkout(String username,String isbn){
		//1、减库存
		bookDao.updateStock(isbn);
		
		int price = bookDao.getPrice(isbn);
//		try {
//			Thread.sleep(3000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		//2、减余额
		bookDao.updateBalance(username, price);
		
		//int i = 10/0;
		//new FileInputStream("D://hahahahha.aa");
	}
	
	
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void updatePrice(String isbn,int price){
		bookDao.updatePrice(isbn, price);
	}
	
	
	
	/**
	 * 根据业务的特性;进行调整
	 * isolation=Isolation.READ_UNCOMMITTED:读出脏数据
	 * 
	 * 
	 * 		READ_COMMITTED;实际上业务逻辑中用的最多的也是这个;
	 * 		REPEATABLEP_READ;
	 * @param isbn
	 * @return
	 */
	@Transactional(readOnly=true)
	public int getPrice(String isbn){
		return bookDao.getPrice(isbn);
	}
	
	
	@Transactional
	public void mulTx(){
		
		//ioc.getBean("BookSerice");
		checkout("Tom", "ISBN-001");
		
		updatePrice("ISBN-002", 998);
		
		int i=10/0;
	}	

}

tx.xml(xml方式)

<?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:context="http://www.springframework.org/schema/context"
	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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<context:component-scan base-package="com.atguigu"></context:component-scan>


	<!-- 0、引入外部配置文件 -->
	<context:property-placeholder location="classpath:dbconfig.properties" />
	<!-- 1、配置数据源 -->
	<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
	</bean>
	<!-- 2、配置JdbcTemplate操作数据库   value="#{pooledDataSource}"  ref="pooledDataSource"-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" value="#{pooledDataSource}"></property>
	</bean>
	
	<!-- 3、配置声明式事务
		1)、Spring中提供事务管理器(事务切面),配置这个事务管理器
		2)、开启基于注解的事务式事务;依赖tx名称空间
		3)、给事务方法加注解
	 -->
	 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="pooledDataSource"></property>
	 </bean>
	 
	 <!-- 
	 基于xml配置的事务;依赖tx名称空间和aop名称空间
	 	1)、Spring中提供事务管理器(事务切面),配置这个事务管理器
	    2)、配置出事务方法;
		3)、告诉Spring哪些方法是事务方法;
			(事务切面按照我们的切入点表达式去切入事务方法)
	  -->
	 <aop:config>
	 	<aop:pointcut expression="execution(* com.atguigu.ser*.*.*(..))" id="txPoint"/>
	 	<!-- 事务建议;事务增强     advice-ref:指向事务管理器的配置 -->
	 	<aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"/>
	 </aop:config>
	 
	 <!--  配置事务管理器;    事务建议;事务增强;事务属性;
	 transaction-manager="transactionManager":指定是配置哪个事务管理器;
	 -->
	 <tx:advice id="myAdvice" transaction-manager="transactionManager">
	 		<!--事务属性  -->
	 		<tx:attributes>
	 			<!-- 指明哪些方法是事务方法;
	 			切入点表达式只是说,事务管理器要切入这些方法,
	 			哪些方法加事务使用tx:method指定的 -->
	 			<tx:method name="*"/>
	 			<tx:method name="checkout" propagation="REQUIRED" timeout="-1"/>
	 			<tx:method name="get*" read-only="true"/>
	 		</tx:attributes>
	 </tx:advice>
	 
	 <!-- 都用;重要的用配置,不重要的用注解 -->	 
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是由于文件被占用导致的。根据引用\[1\]中的描述,你可以尝试以下解决方法: 1. 关闭正在使用该文件的程序或文件夹。 2. 关闭与该文件相关的进程。你可以通过打开控制面板并终止Java进程来实现。 3. 检查是否有其他文件或目录被占用。在这个例子中,target目录被占用,通过关闭命令窗口来解决问题。 此外,根据引用\[2\]和引用\[3\]的描述,你还可以检查你的Spring配置文件,确保只扫描@Controller注解的bean,避免重复扫描。 总之,当你遇到类似的错误时,你应该仔细查看错误信息中的"Failed to delete"后面的内容,考虑是否有文件或目录被占用,并采取相应的解决方法。 #### 引用[.reference_title] - *1* [Failed to clean project: Failed to delete-springbootdemo](https://blog.csdn.net/u013716535/article/details/86183644)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [context:component-scan属性介绍,SSM的bean被扫描两次问题](https://blog.csdn.net/qq_40542534/article/details/108805603)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值