1. 程式人生 > 實用技巧 >android使用---->常用元件1

android使用---->常用元件1

一、回顧事務

事務的重要性

  • 事務在專案開發過程非常重要,涉及到資料的一致性的問題,不容馬虎!
  • 事務管理是企業級應用程式開發中必備技術,用來確保資料的完整性和一致性

事務就是把一系列的動作當成一個獨立的工作單元,這些動作要麼全部完成,要麼全部不起作用

事務第四個屬性

  • 原子性(atomicity)
  • 事務是原子性操作,由一系列動作組成,事務的原子性確保動作要麼全部完成,要麼完全不起 作用
  • 一致性(consistency)
    • 一旦所有事務動作完成,事務就要被提交。資料和資源處於一種滿足業務規則的一致性狀態中
  • 隔離性(isolation)
    • 可能會出現多個事務會同時處理相同的資料的情況,因此每個事務都應該與其他事務隔離開來,防止資料損壞
  • 永續性(durability)
    • 事務一旦完成,無論系統發生什麼錯誤,結果都不會受到影響。通常情況下,事務的結果被寫 到持久化儲存器中

二、、Spring中的事務管理

Spring在不同的事務管理API之上定義了一個抽象層,使得開發人員不必瞭解底層的事務管理API就可以 使用Spring的事務管理機制。Spring支援程式設計式事務管理和宣告式的事務管理

  • 程式設計式事務管理
    • 將事務管理程式碼嵌到業務方法中來控制事務的提交和回滾 缺點:必須在每個事務操作業務邏輯中包含額外的事務管理程式碼
  • 宣告式事務管理
    • 一般情況下比程式設計式事務好用
    • 將事務管理程式碼從業務方法中分離出來,以宣告的方式來實現事務管理
    • 把事務管理作為橫切關注點,通過aop方法模組化。Spring中通過Spring AOP框架支援宣告式事務管理

使用Spring管理事務

參考:官方文件

標頭檔案的約束匯入

使用Spring管理事務,需要標頭檔案的約束匯入 : 如tx

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
事務管理器
  • 無論使用Spring的哪種事務管理策略(程式設計式或者宣告式)事務管理器都是必須的
  • 即 Spring的核心事務管理抽象,管理封裝了一組獨立於技術的方法
JDBC事務
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />
</bean
配置事務的通知

配置好事務管理器後,需要去配置事務的通知

<!--配置事務通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!--配置哪些方法使用什麼樣的事務,配置事務的傳播特性-->
        <tx:method name="add" propagation="REQUIRED"/>
        <tx:method name="delete" propagation="REQUIRED"/>
        <tx:method name="update" propagation="REQUIRED"/>
        <tx:method name="search*" propagation="REQUIRED"/>
        <tx:method name="get" read-only="true"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
spring事務傳播特性

事務傳播行為就是多個事務方法相互呼叫時,事務如何在這些方法間傳播。spring支援7種事務傳播行 為:

  • propagation_requierd:如果當前沒有事務,就新建一個事務,如果已存在一個事務中,加入到這 個事務中,這是常見的選擇
  • propagation_supports:支援當前事務,如果沒有當前事務,就以非事務方法執行
  • propagation_mandatory:使用當前事務,如果沒有當前事務,就丟擲異常
  • propagation_required_new:新建事務,如果當前存在事務,把當前事務掛起
  • propagation_not_supported:以非事務方式執行操作,如果當前存在事務,就把當前事務掛起
  • propagation_never:以非事務方式執行操作,如果當前事務存在則丟擲異常
  • propagation_nested:如果當前存在事務,則在巢狀事務內執行。如果當前沒有事務,則執行與 propagation_required類似的操作

Spring 預設的事務傳播行為是 PROPAGATION_REQUIRED,它適合於絕大多數的情況

假設 ServiveX#methodX() 都工作在事務環境下(即都被 Spring 事務增強了),假設程式中存在如下的 呼叫鏈:Service1#method1()->Service2#method2()->Service3#method3(),那麼這 3 個服務類的 3 個方法通過 Spring 的事務傳播機制都工作在同一個事務中

舉個栗子,我們剛才的幾個方法存在呼叫,所以會被放在一組事務當中!

配置AOP
<!--配置aop織入事務--> <aop:config>
<aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.lf.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
接下來就按使用正常步驟進行測試即可

此處省略

完整的使用Spring管理事務配置檔案(如spring-dao.xml)
<?xml version="1.0" encoding="UTF8"?>
<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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--DataSource:使用Spring的資料來源替換Mybatis的配置  c3p0  dbcp  druid
    我們這裡使用Spring提供的JDBC : org.springframework.jdbc.datasource
    -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--繫結Mabatis配置檔案-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/lf/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate:就是我們使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用構造器注入sqlSessionFactory,因為它沒有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>


    <!--配置宣告式事務-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--結合AOP實現事務的織入-->
    <!--配置事務通知;-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--給那些方法配置事務-->
        <!--配置事務的傳播特性:new  propagation= -->
        <tx:attributes>
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="query" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事務切入-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.lf.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>


</beans>