美文网首页
spring学习16(事务管理)

spring学习16(事务管理)

作者: 又是那一片天 | 来源:发表于2017-08-02 16:00 被阅读0次

    是指作为单个逻辑工作单元执行的一系列操作 要么完全地执行,要么完全地不执行;

    注解配置事务

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="c3p0"></property>
    </bean>
    <!-- 启用事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    
    • 根据数据库的框架不同注册的事务管理器都不一样
    • 使用注解方式要启用注解
    使用@Transactional注解标注事务管理方法
        @Transactional(timeout=2)
        @Override
        public void purchase(String uid, String bookid) {
            // 获取书单价
            int price = bookDao.findBookPrice(bookid);
            // 更新书的库存
            bookDao.updateBookStock(bookid);
            // 更新用户余额
            bookDao.updateUser("1", price);
        }
    
    事务的传播行为

    @Transactional的propagation属性指定事务传播行为(即当前的事务方法被调其他用的事务方法)
    1)REQUIRED是默认行为 即使用当前事务
    2) REQUIRES_NEW 即使用自己的事务

    @Transactional(propagation=Propagation.REQUIRED)
    
    隔离级别

    使用属性isolation 指定事务的隔离级别,最常用的是READ_COMMITTED

    指定回滚或者不回滚异常
    • 默认情况下spring神明的事务对所有的运行时的异常进行回滚,也可以通过对应的属性进行设置
    • noRollbackFor指定不回滚的异常
    • RollbackFor只回滚的异常
    timeout指定强制回滚之前事务可占用的时间(防止一个请求占用过多的时间)

    配置文件配置事务

    • 1正常配置需要的bean
    • 2配置事务管理器
    • 3配置事务属性 隔离级别 只读 回滚..
    • 4.配置事务切入点 以及把事务切入点 和事务属性关联
    <?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.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        <context:component-scan base-package="transaction"></context:component-scan>
    
        <!-- 通过xml配置事务 -->
    
        <!-- 导入配置文件 -->
        <context:property-placeholder location="classpath:mysql" />
        <!-- 配置c3p0 -->
        <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${user}"></property>
            <property name="password" value="${password}"></property>
            <property name="jdbcUrl" value="${jdbcUrl}"></property>
            <property name="driverClass" value="${driverClass}"></property>
            <property name="initialPoolSize" value="${initPoolSize}"></property>
            <property name="maxPoolSize" value="${maxPoolSize}"></property>
        </bean>
    
        <!-- 配置 Spring 的 jdbcTemplate -->
        <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="c3p0"></property>
        </bean>
    
        <!-- 配置bean -->
        <bean id="dao" class="transaction.BookDao">
            <property name="jdbcTemplate" ref="jt"></property>
        </bean>
    
        <bean id="service" class="transaction.BookService">
            <property name="bookDao" ref="dao"></property>
        </bean>
    
        <bean id="cashier" class="transaction.CashierImp">
            <property name="service" ref="service"></property>
        </bean>
    
        <!-- 添加事务 -->
    
        <!--1. 配置事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="c3p0"></property>
        </bean>
    
        <!--2. 配置事务属性 隔离级别 只读 回滚.. -->
        <tx:advice id="advice " transaction-manager="transactionManager">
            <tx:attributes>
                <!-- name 是方法名 该方法要加入切点 -->
                <tx:method name="purchase" propagation="REQUIRED" />
                <tx:method name="get*" propagation="REQUIRED" />
            </tx:attributes>
        </tx:advice>
    
    
        <!-- 3.配置事务切入点 以及把事务切入点 和事务属性关联 -->
        <aop:config>
            <!-- 指定切入点 这个写法是将transaction包下的所有类的方法作为切点 -->
            <aop:pointcut expression="execution(*  transaction.*.*(..))"
                id="aopservice" />
            <!-- 关联事务属性 -->
            <aop:advisor advice-ref="advice" pointcut-ref="aopservice" />
        </aop:config>
        
    </beans>
    

    相关文章

      网友评论

          本文标题:spring学习16(事务管理)

          本文链接:https://www.haomeiwen.com/subject/plzelxtx.html