國慶過得好像有點忙,是不是生活過得簡單了,就有時間享福了?當然國慶假期也抽點時間來學習學習,不然趕不上年輕人的腳步
學啥
今天學習了一下aop的事務管理,沒分析內部,估計只不過對使用進行了封裝,讓我們使用更加簡便而已
功能
- 可以通過切面編程對指定的進行事務管理
- 可以控制異常的類型才進行回滾
- 可以控制事務的timeout
Propagation取值:
值 | 備註 |
---|---|
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 |
例子1(xml)
直接看例子再分析分析
<?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那一套,有稍微一點點不一樣
- tx:advice 事務控制點
- transaction-manager 事務管理者
例子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: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代碼
@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"/>
支持事務註解