事务并发-隔离级别

    xiaoxiao2025-03-17  40

    1.并发问题:

    问题1:脏读(dirty read)

    A事务读到B事务没有提交的数据,并且A来修改这个数据,如果恰巧B做事务的回滚,那么A事务读到的数据就是错误的

    问题2:不可重复读(unrepeatable read)

    指的是A事务读取了B事务已经提交了的更改数据,假设A取款的过程中B向账户汇入100,A事务两次读取数据不一致。

    问题3:幻读(phantom read)

    A事务读取B事务新增的数据,假设银行做在一个A事务中统计,在统计过程中B新增了用户,A的事务中两次统计不同。

    问题4:第一类更新丢失

    问题5:第二类更新丢失

    2.隔离级别

    SQL92标准提供了4个隔离级别,会给我们自动的根据不同的事务的隔离级别加不同的锁。

    数据库的隔离级别越高,并发性就越差,性能就越低。

    oracle的隔离级别默认是READ COMMITED

    mysql的隔离级别默认是REPEATABLE READ

    3.事务控制配置文件方式

    <?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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--数据源配置--> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring10"></property> <property name="username" value="root"/> <property name="password" value="123"/> </bean> <!--定义事务的管理器--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--通知的配置--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!--具体的方法的配置--> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="query*" read-only="true"/> </tx:attributes> </tx:advice> <!--切面配置--> <aop:config> <!-- 切点配置:一般情况我们把切点配置在servcie层--> <aop:pointcut id="mycut" expression="execution(* spring.service..*.*(..))"/> <!-- advice-ref:管理通知 pointcut-ref:关联切点 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/> </aop:config> <bean id="accountDao" class="spring.dao.Impl.AccountDaoImpl"> <property name="ds" ref="dataSource"/> </bean> <bean id="accountService" class="spring.service.impl.AccountServiceImpl"> <property name="aDao" ref="accountDao"/> </bean> </beans>

     

    最新回复(0)