國慶過得好像有點忙,是否是生活過得簡單了,就有時間享福了?固然國慶假期也抽點時間來學習學習,否則趕不上年輕人的腳步java
今天學習了一下aop的事務管理,沒分析內部,估計只不過對使用進行了封裝,讓咱們使用更加簡便而已spring
Propagation取值:express
|值|備註| |- |REQUIRED(默認值)|在有transaction狀態下執行;如當前沒有transaction,則建立新的transaction| |SUPPORTS|如當前有transaction,則在transaction狀態下執行;若是當前沒有transaction,在無transaction狀態下執行| |MANDATORY|必須在有transaction狀態下執行,若是當前沒有transaction,則拋出異常IllegalTransactionStateException| |REQUIRES_NEW|建立新的transaction並執行;若是當前已有transaction,則將當前transaction掛起| |NOT_SUPPORTED|在無transaction狀態下執行;若是當前已有transaction,則將當前transaction掛起| |NEVER|在無transaction狀態下執行;若是當前已有transaction,則拋出異常IllegalTransactionStateException|apache
直接看例子再分析分析編程
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:config> <!--定義切入點1,要對指定的進行管理事物--> <aop:pointcut id="defaultServiceOperation" expression="execution(* x.y.service.*Service.*(..))"/> <!--定義切入點2,要對指定的進行管理事物--> <aop:pointcut id="noTxServiceOperation" expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/> <!--對上面的切入點1採用defaultTxAdvice實例進行管理--> <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/> <!--對上面的切入點2採用noTxAdvice實例進行管理--> <aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/> </aop:config> <!-- this bean will be transactional (see the 'defaultServiceOperation' pointcut) --> <bean id="fooService" class="x.y.service.DefaultFooService"/> <!-- this bean will also be transactional, but with totally different transactional settings --> <bean id="anotherFooService" class="x.y.service.ddl.DefaultDdlManager"/> <!--對方法名get開始的,只容許讀,不容許修改等--> <tx:advice id="defaultTxAdvice"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--在無transaction狀態下執行;若是當前已有transaction,則拋出異常IllegalTransactionStateException。--> <tx:advice id="noTxAdvice"> <tx:attributes> <tx:method name="*" propagation="NEVER"/> </tx:attributes> </tx:advice> <!-- other transaction infrastructure beans such as a PlatformTransactionManager omitted... --> </beans>
其實仍是利用了AOP那一套,有稍微一點點不同oracle
直接看例子再分析分析學習
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--容許註解管理事務--> <tx:annotation-driven transaction-manager="txManager" order="200"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
java代碼this
@Transactional(readOnly = true) public class DefaultFooService implements FooService { public Foo getFoo(String fooName) { // do something } // these settings have precedence for this method @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public void updateFoo(Foo foo) { // do something } }
<tx:annotation-driven transaction-manager="txManager" order="200"/>
支持事務註解