spring3.0交易管理配置__java

來源:互聯網
上載者:User
第一種配置方法:基於XML的交易管理

這種方法不需要對原有的業務做任何修改,通過在XML檔案中定義需要攔截方法的匹配即可完成配置,要求是,業務處理中的方法的命名要有規律,比如setXxx,xxxUpdate等等。詳細配置如下: Xml代碼   <!-- 定義交易處理類,不同的資料訪問方式,交易處理類不同       比如:Hibernate操作的HibernateTransactionManager,JDBC操作的使用DataSourceTransactionManager    -->   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">       <property name="dataSource" ref="dataSource"></property>   </bean>      <!-- 定義事務通知 -->   <tx:advice id="txAdvice" transaction-manager="transactionManager">       <!-- 定義方法的過濾規則 -->       <tx:attributes>           <!-- 所有方法都使用事務 -->           <tx:method name="*" propagation="REQUIRED"/>           <!-- 定義所有get開頭的方法都是唯讀 -->           <tx:method name="get*" read-only="true"/>       </tx:attributes>   </tx:advice>      <!-- 定義AOP配置 -->   <aop:config>       <!-- 定義一個切入點 -->       <aop:pointcut expression="execution (* com.iflysse.school.services.impl.*.*(..))" id="services"/>       <!-- 對切入點和事務的通知,進行適配 -->       <aop:advisor advice-ref="txAdvice" pointcut-ref="services"/>   </aop:config>   第二中配置方法:基於@Transactional的交易管理

這種方法,只需要在Spring設定檔中定義一個交易管理對象(如DataSourceTransactionManager),然後加入<tx:annotation-driven/>節點,引用該交易管理對象,然後即可在需要進行交易處理的方法使用@Transactional進行標註。樣本如下:

Spring配置XML檔案 Xml代碼   <!-- 定義交易處理類,不同的資料訪問方式,交易處理類不同       比如:Hibernate操作的HibernateTransactionManager,JDBC操作的使用DataSourceTransactionManager    -->   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">       <property name="dataSource" ref="dataSource"></property>   </bean>   <!-- 聲明使用註解式事務 -->   <tx:annotation-driven transaction-manager="transactionManager"/>  

Java代碼中@Transactional樣本 Java代碼   /**   * @author ZYWANG 2011-3-24   */   @Service   @Transactional(propagation = Propagation.REQUIRED)   public class TeacherServicesImpl implements TeacherServices {       @Autowired       TeacherDao dao = null;          public TeacherDao getDao() {           return dao;       }          public void setDao(TeacherDao dao) {           this.dao = dao;       }          @Override       @Transactional(readOnly=true,propagation=Propagation.NEVER)       public List<Teacher> getAllTeachers() {           return getDao().getAllTeachers();       }          @Override       public void addTeacher(Teacher teacher) {           if(teacher.getId() > 0){  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.