[劉陽Java]_Spring對Transaction的支援_第12講

來源:互聯網
上載者:User

標籤:設計   date   oca   java   注意   xsd   blog   aop   pac   

Spring對資料庫交易管理是非常優秀,它利用AOP方式來管理資料庫操作的事務。優點:主要體現了業務功能設計不會和事務代碼耦合。在使用Spring對Transaction支援中建議採用聲明式交易管理來完成。

1. Spring對Transaction設計的代碼步驟如下(重點關注設定檔編寫,我們以JDBC交易管理來給大家進行闡述)

  • 配置資料來源
  • 配置交易管理員,DataSourceTransactionManager。這是交易管理員針對管理JDBC事務
  • 通過AOP讓service包下所有Bean的所有方法擁有事務
  • 配置JdbcTemplate
  • 配置context:component-scan(掃描加入Spring註解的類),將標註Spring註解的類自動轉化Bean,同時完成Bean的注入

2. 設定檔代碼如下

<?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:p="http://www.springframework.org/schema/p"    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-3.0.xsd       http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.0.xsd       http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <!-- 掃描類包,將標註Spring註解的類自動轉化Bean,同時完成Bean的注入 -->    <context:component-scan base-package="com.gxaedu.dao"/>    <context:component-scan base-package="com.gxaedu.service"/>    <!-- 配置資料來源 -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        destroy-method="close"         p:driverClassName="com.mysql.jdbc.Driver"        p:url="jdbc:mysql://localhost:3306/sampledb"         p:username="root"        p:password="1234" />    <!-- 配置Jdbc模板  -->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"        p:dataSource-ref="dataSource" />            <!-- 配置交易管理員 -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"        p:dataSource-ref="dataSource" />            <!-- 通過AOP配置提供事務增強,讓service包下所有Bean的所有方法擁有事務 -->    <aop:config proxy-target-class="true">        <aop:pointcut id="serviceMethod"            expression=" execution(* com.gxaedu.service..*(..))" />        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />    </aop:config>    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="add*" propagation="REQUIRED"/>            <tx:method name="update*" propagation="REQUIRED"/>            <tx:method name="*" read-only="true"/>        </tx:attributes>    </tx:advice></beans>

 注意上面的設定檔有幾個關鍵點,下面我會逐一的介紹一下

  • proxy-target-class
  • tx:advice, tx:attributes, tx:method

3. 我們也不知道上面的配置是否真正的成功,請按照下面的方式進行測試。這一點大家要注意

  • 注意如果配置事務是介面代理,則測試程式是介面
  • 交叉測試,一個Service裡面有兩個以上的Dao對象。讓第一個Dao對象SQL語句能夠執行成功,但是第二個Dao對象SQL語句操作失敗,看事務能不能管理Service中的方法,讓資料全部復原

[劉陽Java]_Spring對Transaction的支援_第12講

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.