zoukankan      html  css  js  c++  java
  • spring笔记--事务管理之声明式事务

    • 事务简介:

        事务管理是企业级应用开发中必不可少的技术,主要用来确保数据的完整性和一致性,

        事务:就是一系列动作,它们被当作一个独立的工作单元,这些动作要么全部完成,要么全部不起作用.

    • Spring中使用事务:

        作为一个受欢迎的企业应用框架,Spring在不同的事务管理API上定义了一个抽象层,而开发时不必了解底层的事务管理API,就可以使用Spring的事务管理机制.

        Spring既支持编程式的事务管理,也支持声明式的事务管理,大多数情况我们选择后者.

          编程式事务管理:将事务管理代码嵌入到业务代码中来控制事务的提交和回滚,在编程式事务管理中,必须在每一个事务操作中包含额外的事务管理代码.

          声明式事务管理:将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理,事务管理作为一种横切关注点,可以通过AOP的方式模块化. 

        下面介绍一下在Spring中使用注解方式开启声明式事务的配置文件代码(beans.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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            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-4.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
         <!-- 导入资源文件 -->
         <context:property-placeholder location="db.properties"
             ignore-resource-not-found="false" />
         <!-- 配置c3p0连接池 -->
         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
         <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
        </bean>
        <!-- 配置jdbcTemplate -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
        
        </bean>
        <!--步骤一: 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 步骤二:启用事务注解 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

        步骤三: 配置完成以后,在需要开始事务的方法添加@Transactional注解,即可在该方法中开启并使用事务.

    • 事务的传播属性:

        当事务方法被另一个事务方法调用时,必须指定事务应该如何传播,例如:方法可以继续在现有事务中运行,也可以开启一个新的事务,在新的事务中执行.

        在@Transactional中,有一个参数叫propagation,用于指定事物的传播行为,默认值为REQUIRED,即使用调用方法的事务.

          @Transactional(propagation=Propagation.REQUIRED):不开启新的事务

          @Transactional(propagation=Propagation.REQUIRED_NEW):开启新的事务

        那么应该如何理解呢,假设我现在有100块钱,我需要买两本书,A和B,A卖60块钱,库存还有10本,B卖50块钱,库存也有十本,在方法buyBook()中,需要同时调用方法BuyBookA(),和buyBookB();

        如果选择REQUIRED的话,结果是一本也买不了,因为在买完A后,我只剩40块钱,在买B的时候,因为钱不够,所以购买失败,事务回滚到buyBook方法最初的位置(buyBookA方法之前),即A,B都没有买成.

        如果选择的是REQUIRED_NEW的话,那么会成功购买A,而无法购买B,因为在buyBook方法中,当调用buyBookA()时,会开启一个新事务,在购买成功时,事务提交,数据库更新,这时候我只剩40块钱,再调用buyBookB()时自然会因为钱不够而事务回滚.最后的结果就是,A购买成功,B购买失败.

    • 事务的其他属性:

        使用isolation指定事务的隔离级别,最常用的取值为READ_COMMITTED

        使用readOnly指定事物是否为只读,表示这个事务只读取数据而不更新数据,这样可以帮助数据库引擎优化事务,

        使用timeOut指定事务回滚之前可以占用的时间.

      @Transactional(isolation=Isolation.READ_COMMITTED,readOnly=true,timeout=5)

    以上都是使用注解的方式启用声明式事务,下面介绍一下使用xml文件配置事务的方式:

      beans.xml(配置bean的过程省略):

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            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-4.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
         <!-- 导入资源文件 -->
         <context:property-placeholder location="db.properties"
             ignore-resource-not-found="false" />
         <!-- 配置c3p0连接池 -->
         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
         <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
        </bean>
        <!-- 配置jdbcTemplate -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
        
        </bean>
        <!--步骤1: 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 步骤2:配置事务属性 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
       <!--根据方法名指定事物的属性-->
    <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 步骤3:配置事物切入点,把事物切入点和事务属性关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.wang.dao.UserDao.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
  • 相关阅读:
    68、成员列表初始化?
    67、类成员初始化方式?构造函数的执行顺序 ?为什么用成员初始化列表会快一 些?
    64、malloc申请的存储空间能用delete释放吗?
    63、new和delete的实现原理, delete是如何知道释放内存的大小的额?
    62、delete p、delete [] p、allocator都有什么作用?
    60、C++模板是什么,你知道底层怎么实现的?
    nyoj--814--又见拦截导弹(动态规划+贪心)
    hdoj--1950--Bridging signals(二分查找+LIS)
    nyoj--214--单调递增子序列(二)(二分查找+LIS)
    hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)
  • 原文地址:https://www.cnblogs.com/fingerboy/p/5285482.html
Copyright © 2011-2022 走看看