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>
    -------------------------------------------------------------
    bean.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置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>
    </bean>
    <!--步骤1: 配置事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 步骤2:配置事务属性 -->
    <!-- 事务管理器 transaction-manager="txManager"是我们前面指定好了的-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <!-- 为业务bean设置一些事务行为 -->
    <tx:attributes>
    <!--根据方法名指定事物的属性-->
    <tx:method name="*"/>
    </tx:attributes>
    </tx:advice>
    <!-- 步骤3:配置事物切入点,把事物切入点和事务属性关联起来 -->
    <aop:config> <!-- 拦截点 拦截cn.itcast.service下的子包(..)所有类(*)所有方法(*)带或不带参数(..) -->
    <aop:pointcut id="transactionPointcut" expression="execution(* cn.itcast.service..*.*(..))"/>
    <!-- 事务配置通知 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
    </aop:config>


    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    </beans>

  • 相关阅读:
    如何将伪数组转换成真正的数组
    JS 中对变量类型的五种判断方法
    ajax详解
    onload和ready的区别
    ES5继承
    跨域的三种解决方式
    如何处理使用js兼容所有浏览器的问题
    Canvas修行之黑客帝国代码雨
    Webpack+React+ES6入门指南[转]
    对于Mongodb数据库的学习
  • 原文地址:https://www.cnblogs.com/hoobey/p/6033539.html
Copyright © 2011-2022 走看看