hbase支持事務嗎,8 -- 深入使用Spring -- 6...2 Spring支持的事務策略

 2023-11-18 阅读 18 评论 0

摘要:      8.6.2 使用XML Schema配置事務策略         Spring 同時支持編程式事務策略和聲明式事務策略,通常都推薦采用聲明式事務策略。         ⊙ 聲明式事務能大大降低開發者的代碼書寫量,而且聲明式事務幾乎不影響應用的代碼。因此ÿ

      8.6.2 使用XML Schema配置事務策略

        Spring 同時支持編程式事務策略和聲明式事務策略,通常都推薦采用聲明式事務策略。

        ⊙ 聲明式事務能大大降低開發者的代碼書寫量,而且聲明式事務幾乎不影響應用的代碼。因此,無論底層事務策略如何變化,應用程序都無須任何改變。

hbase支持事務嗎。        ⊙ 應用程序代碼無須任何事務處理代碼,可以更專注于業務邏輯的實現。

        ⊙ Spring則可對任何POJO的方法提供事務管理,而且Spring的聲明式事務管理無須容器的支持,可在任何環境下使用。

        ⊙ EJB的CMT無法提供聲明式回滾規則:而通過配置文件,Spring可指定事務在遇到特定異常時自動回滾。

        ⊙ 由于Spring采用AOP的方式管理事務,因此,可以在事務回滾動作中插入用戶自己的動作,而不僅僅是執行系統默認的回滾。

Redis事務、        Spring 2.x 的XML Schema方式提供了簡潔的事務配置策略,Spring 2.x提供了 tx:命名空間 來配置事務管理,tx:命名空間下提供了<tx:advice.../>元素來配置事務增強處理,一旦使用該元素配置了事務增強處理,就可直接使用<aop:advisor.../>元素啟用自動代理了。

        配置<tx:advice.../>元素時除了需要transaction-manager屬性指定事務管理器之外,還需要配置一個<attributes.../>子元素,該子元素里又可包含多個<method.../>子元素。

        配置<tx:advice.../>元素的重點就是配置<method.../>子元素,實際上每個<method.../>子元素都為一批方法指定了所需的事務定義,包括事務傳播屬性、事務隔離屬性、事務超時屬性、只讀事務、對指定異常回滾、對指定異常不回滾等。

        配置<method.../>子元素可以指定如下屬性:

項目中如何使用事務、          ⊙ name : 必選屬性,與該事務語義關聯的方法名。該屬性支持使用通配符,例如:‘add*’、‘delete*’、‘get*byId’、‘update*’等。

          ⊙ propagation : 指定事務傳播行為,該屬性值可為Propagation枚舉類的任一枚舉值。該屬性的默認值為Propagation.REQUIRED。

          ⊙ isolation : 指定事務隔離級別,該屬性值可為Isolation枚舉類的任一枚舉值。默認值為Isolation.DEFAULT。

          ⊙ timeout : 指定事務超時的時間(以秒為單位),指定-1意味著不超時,該屬性默認值是-1.

Spring Framework,          ⊙ read-only : 指定事務是否只讀。該屬性的默認值是false。

          ⊙ rollback-for : 指定觸發事務回滾的異常類(應使用全限定類名),該屬性可指定多個異常類,多個異常類之間以英文逗號隔開。

          ⊙ no-rollback-for : 指定不觸發事務回滾的異常類(應使用全限定類名),該屬性可指定多個異常類,多個異常類之間以英文逗號隔開。

        <method.../>子元素的propagation屬性用于指定事務傳播行為,Spring支持的事務傳播行為如下:

事務的、          ⊙ PROPAGATION_MANDATORY(強制的) : 要求調用方法的線程必須處于事務環境中,否則拋出異常。

          ⊙ PROPAGATION_NEVER(決不) : 不允許調用該方法的線程處于事務中,如果調用該方法的線程處于事務環境中,則拋出異常。

          ⊙ PROPAGATION_SUPPORTS(支持) : 如果當前執行線程處于事務環境中,則使用當前事務,否則不使用事務。          

          ⊙ PROPAGATION_NOT_SUPPORED (被支持的): 如果調用該方法的線程處于事務環境中,則先暫停當前事務,然后執行該方法。

Spring中事務管理的兩種方式,          ⊙ PROPAGATION_NESTED(嵌套的) : 即使執行該方法的線程已處于事務環境中,也依然啟動新的事務,方法在嵌套的事務里執行;即使執行該方法的線程并未處于事務環境中,也啟動新的事務,然后執行該方法,此時與PROPAGATION_REQUIRED相同。

          ⊙ PROPAGATION_REQUIRED(必需的) : 要求在事務環境中執行該方法,如果當前執行線程已處于事務環境中,則直接調用;如果當前執行線程不處于事務環境中,則啟動新的事務后執行該方法。

          ⊙ PROPAGATION_REQUIES_NEW (需求): 該方法要求在新的事務環境中執行,如果當前執行線程已處于事務環境中,則先暫停當前事務,啟動新事物后執行該方法;如果當前調用線程不處于事務環境中,則啟動新的事務后執行方法。

        Class : NewsDaoImpl

package edu.pri.lime._8_6_2.dao;import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class NewsDaoImpl implements NewsDao {private DataSource ds;public void setDs(DataSource ds) {this.ds = ds;}@Overridepublic void insert(String title, String content) {JdbcTemplate jt = new JdbcTemplate(ds);jt.update("insert into news_inf values(null,?,?)", title, content);// 兩次插入的數據違反唯一鍵約束jt.update("insert into news_inf values(null,?,?)", title, content);// 如果沒有事務控制,則第一條記錄可以被插入// 如果增加事務控制,將發現第一條記錄也插不進去
    }}

innodb支持事務、?        XML :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd語義約束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans" 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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 掃描Spring的組件 --><context:component-scan base-package="edu.pri.lime._8_6_2.dao"/><!-- 定義數據源Bean,使用C3P0數據源實現,并注入數據源的必要信息 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost/spring"/><property name="user" value="root"/><property name="password" value="System"/><property name="maxPoolSize" value="40"/><property name="minPoolSize" value="2"/><property name="initialPoolSize" value="2"/><property name="maxIdleTime" value="30"/></bean><!-- 配置JDBC數據源的局部事務管理器,使用DataSourceTransactionManager類 --><!-- 該類實現了PlatformTransactionManager接口,是針對采用數據源連接的特定實現 --><!-- 配置DataSourceTransactionmanager時需要依賴注入DataSource的引用 --><bean id="transactionManager" class="org.spring.framework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 配置一個業務邏輯Bean --><bean id="newsDao" class="edu.pri.lime._8_6_2.dao.NewsDaoImpl"><property name="ds" ref="dataSource"/></bean><!-- 配置事務增強處理Bean,指定事務管理器 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><!-- 用于配置詳細的事務定義 --><tx:attributes><!-- 所有以get開頭的方法是只讀的 --><tx:method name="get*" read-only="true"/><!-- 其他方法使用默認的事務設置,指定超時時長為5秒 --><tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" timeout="5"/></tx:attributes></tx:advice><!-- AOP配置的元素 --><aop:config><!-- 配置一個切入點,匹配edu.pri.lime._8_6_2.dao包下所有以Impl結尾的類里所有方法的執行 --><aop:pointcut expression="execution(* edu.pri.lime._8_6_2.dao.*Impl.*(..))" id="myPointcut"/><!-- 指定在myPointcut切入點應用txAdvice事務增強處理 --><aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/></aop:config>
</beans>

        Class : SpringTest

package edu.pri.lime._8_6_2;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import edu.pri.lime._8_6_2.dao.NewsDao;
import edu.pri.lime._8_6_2.dao.NewsDaoImpl;public class SpringTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("app_8_6_2_tx.xml");// 獲取事務代理BeanNewsDao dao = ctx.getBean("newsDao", NewsDaoImpl.class);// 執行插入操作dao.insert("lime", "Oracle");}
}

        當使用<tx:advisor.../>為目標Bean生成事務代理之后,SpringAOP將會把負責事務操作的增強處理織入目標Bean的業務方法中。

        Spring 不僅支持對接口的代理,而且也可對具體類生成代理,只要設置proxytargetClass屬性為true既可以了。如果目標Bean沒有實現熱河接口,proxyTargetClass屬性默認被設為true,此時Spring會對具體類生成代理。當然,通常建議面向接口編程,而不要面向具體的實現類編程。

Spring詳解?        當采用<aop:advisor.../>元素將Advice和切入點綁定時,實際上是由Spring提供的Bean后處理器完成的。Spring提供了BeanNameAutoProxyCreator、DefaultAdvisorAutoProxyCreator兩個Bean后處理器,它們都可以對容器中的Bean執行后處理(為他們織入切面中包含的增強處理)。當配置<aop:advisor.../>元素時傳入一個txAdvice事務增強處理,所以Bean后處理器將為所有Bean實例里匹配切入點的方法織入事務操作的增強處理。

        在這種聲明式事務策略下,Spring也允許為不同的業務邏輯方法指定不同的事務策略:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd語義約束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans" 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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 掃描Spring的組件 --><context:component-scan base-package="edu.pri.lime._8_6_2.dao"/><!-- 定義數據源Bean,使用C3P0數據源實現,并注入數據源的必要信息 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost/spring"/><property name="user" value="root"/><property name="password" value="System"/><property name="maxPoolSize" value="40"/><property name="minPoolSize" value="2"/><property name="initialPoolSize" value="2"/><property name="maxIdleTime" value="30"/></bean><!-- 配置JDBC數據源的局部事務管理器,使用DataSourceTransactionManager類 --><!-- 該類實現了PlatformTransactionManager接口,是針對采用數據源連接的特定實現 --><!-- 配置DataSourceTransactionmanager時需要依賴注入DataSource的引用 --><bean id="transactionManager" class="org.spring.framework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 配置一個業務邏輯Bean --><bean id="newsDao" class="edu.pri.lime._8_6_2.dao.NewsDaoImpl"><property name="ds" ref="dataSource"/></bean><!-- 配置兩個事務增強處理 --><tx:advice id="txAdvice"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice><tx:advice id="noTxAdvice"><tx:attributes><tx:method name="*" propagation="NEVER"/></tx:attributes></tx:advice><aop:config><!-- 配置一個切入點,匹配userService Bean 中所有方法的執行 --><aop:pointcut expression="bean(userService)" id="txOperation"/><!-- 配置一個切入點,匹配包下所有以Impl結尾的類中所有方法的執行 --><aop:pointcut expression="execution(* edu.pri.lime._8_6_2.dao.*Impl.*(..))" id="noTxOperation"/><!-- 將txOperation切入點和defaultTxAdvice切面綁定在一起 --><aop:advisor advice-ref="defaultTxAdvice" pointcut-ref="txOperation"/><!-- 將noTxOperation切入點和noTxAdvice切面綁定在一起 --><aop:advisor advice-ref="noTxAdvice" pointcut-ref="noTxOperation"/></aop:config><!-- 配置第一個業務邏輯Bean,該Bean的名字為userService,匹配txOperation切入點將被織入defaultTxAdvice切面里的增強處理 --><bean id="userService" class=""></bean><!-- 配置第二個業務邏輯Bean,實現類位于包下,將被織入noTxAdvice切面里的增強處理 --><bean id="anotherFooService" class=""/>
</beans>

        如果想讓事務在遇到特定的cheched異常時自動回滾,則可借助于rollback-for屬性。

        在默認情況下,只有當方法引發運行時異常和unchecked異常時,Spring事務機制才會自動回滾事務。也就是說,只有當拋出一個RuntimeException或其子類實例,或Error對象時,Spring才會自動回滾事務。如果事務方法拋出checked異常,則事務不會自動回滾。

        通過使用rollback-for屬性可強制Spring遇到特定checked異常時自動回滾事務:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd語義約束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans" 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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" read-only="true"rollback-for="exception.NoItemException" /><tx:method name="*" /></tx:attributes></tx:advice></beans>

        如果想讓Spring遇到特定runtime異常時強制不會滾事務,則可通過no-rollback-for屬性來指定:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd語義約束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans" 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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" read-only="true"no-rollback-for="exception.AuctionException" /><tx:method name="*" /></tx:attributes></tx:advice></beans>

啦啦啦

啦啦啦

啦啦啦

啦啦啦

啦啦啦

啦啦啦

啦啦啦

啦啦啦

啦啦啦

轉載于:https://www.cnblogs.com/ClassNotFoundException/p/6596819.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/174346.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息