事务并发-隔离级别

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值