小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

SpringFramework(8)

 web.anywhere 2006-03-16

5、事務(wù)管理

1)事務(wù)

l         全局事務(wù)

Ø         由應(yīng)用服務(wù)器使用JTA管理

Ø         具有同多事務(wù)資源工作的能力

l         本地事務(wù)

Ø         資源規(guī)范:如和JDBC連接關(guān)聯(lián)的事務(wù)

Ø         不能夠在多事務(wù)資源之間交叉工作

Ø         不能在全局的JTA事務(wù)中運(yùn)行

l         不同的編程模型

2Spring解決方案

l         對(duì)全局和本地事務(wù)使用相同的編程模型

Ø         不同的環(huán)境采用不同的事務(wù)管理策略

l         事務(wù)管理可以:

Ø         編程方式

Ø         聲明方式(如EJB CMT

3)事務(wù)提取

l         事務(wù)通過PlatformTransactionManager接口提取

Ø         getTransaction(TransactionDefinition)

Ø         commit(TransactionStatus)

Ø         rollback(TransactionStatus)

l         TransactionDefinition:隔離、傳播、超時(shí)和只讀狀態(tài)

l         TransactionStatus

Ø         isNewTransaction()

Ø         setRollbackOnly()

Ø         isRollbackOnly()

4)事務(wù)管理

l         內(nèi)建平臺(tái)事務(wù)管理:

Ø         JtaTransacrionManager

Ø         DataSourceTransacrionManager

Ø         HibernateTransacrionManager

Ø         JdoTransacrionManager

5)例子

l         定義一個(gè)JtaTransacrionManager

<bean id="dataSource" class="...jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>MyDS</value></property>
</bean>
<bean id="transactionManager"
class="...transaction.jta.JtaTransactionManager"/>

數(shù)據(jù)源必須在應(yīng)用服務(wù)器中被配置為事務(wù)資源

l         定義一個(gè)DataSourceTransacrionManager

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
...
</bean>
<bean id="transactionManager"
class="...jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

l         定義一個(gè)HibernateTransacrionManager

<bean id="sessionFactory"
class="...orm.hibernate.LocalSessionFactoryBean">
...
</bean>
<bean id="transactionManager"
class="...orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>

使用JTA來應(yīng)用Hibernate不需要HibernateTransacrionManager,只要配置JtaTransacrionManager,給出從JNDI獲得的sessionFactory數(shù)據(jù)源

6TransactionTemplate

l         編程方式的事務(wù)管理

l         創(chuàng)建一個(gè)TransactionTemplate

PlatformTransactionManager transactionManager =
(PlatformTransactionManager) ctx.getBean("myTransactionManager");
TransactionTemplate transaction =
new TransactionTemplate(transactionManager);

l         在一個(gè)事務(wù)中執(zhí)行

transaction.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus s) {
updateOperation1();
updateOperation2();
}
});

l         TransactionTemplate的其它方法

Ø         setPropagationBehavior(int)

Ø         setIsolationLevel(int)

Ø         setReadOnly(Boolean)

Ø         setTimeout(int)

7)好處

l         不同事務(wù)管理之間轉(zhuǎn)換

Ø         只是一些配置

Ø         不需要改變代碼

l         相同的組件可以運(yùn)行在:

Ø         使用JTA事務(wù)的應(yīng)用服務(wù)器

Ø         獨(dú)立應(yīng)用程序或Web容器:使用JDBC或開源JTA(如JOTM

8)聲明式事務(wù)

l         不需要TransactionTemplate

l         使用Spring AOP實(shí)現(xiàn)

l         類似于EJB CMT:指定事務(wù)行為到獨(dú)立的方法

l         EJB CMT的區(qū)別:

Ø         能夠應(yīng)用到任何的POJO

Ø         不需要綁定到JTA(可以和JDBC、JDOHibernate一起工作)

Ø         具有聲明式rollback規(guī)則

Ø         定制事務(wù)行為

Ø         不需要支持通過遠(yuǎn)程調(diào)用來傳播事務(wù)context

9TransactionAttributeSource

l         定義事務(wù)屬性如何應(yīng)用

l         TransactionAttributeEditor讀取下面形式的定義:

Ø         PROPAGATION_NAME、ISOLATION_NAME、readOnly、+Exception1、-Exception2

Ø         異常名前面的+表示即使異常拋出,也要提交(commit);異常名前面的-表示需要回滾(rollback

l         例子:

Ø         PROPAGATION_MANDATORY、ISOLATION_DEFAULT-CreateException、-DuplicateKeyException

10)聲明事務(wù)

l         定義事務(wù)攔截器

<bean id="txAttributes"
class="...MatchAlwaysTransactionAttributeSource">
<property name="transactionAttribute">
<value>PROPAGATION_REQUIRED</value>
</property>
</bean>
<bean id="txInterceptor"
class="...transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="myTransactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="txAttributes"/>
</property>
</bean>

MatchAlwaysTransactionAttributeSource應(yīng)用相同的屬性到所有的方法

l         可變的TransactionAttributeSource

<bean id="txAttributes"
class="...interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<value>
get*=PROPAGATION_REQUIRED,readOnly
find*=PROPAGATION_REQUIRED,readOnly
load*=PROPAGATION_REQUIRED,readOnly
store*=PROPAGATION_REQUIRED
</value>
</property>
</bean>

NameMatchTransactionAttributeSource應(yīng)用特定的屬性到匹配的方法

l         自動(dòng)代理事務(wù)Beans

<bean id="autoProxyCreator"
class="...framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<value>txInterceptor</value>
</property>
<property name="beanNames">
<value>*Dao</value>
</property>
</bean>

l         使用元數(shù)據(jù)屬性

<bean id="autoproxy"
class="...aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
</bean>
<bean id="txAdvisor"
class="...transaction.interceptor.TransactionAttributeSourceAdvisor"
autowire="constructor">
</bean>
<bean id="txInterceptor"
class="...transaction.interceptor.TransactionInterceptor"
autowire="byType">
</bean>
<bean id="txAttributeSource"
class="...transaction.interceptor.AttributesTransactionAttributeSource"
autowire="constructor">
</bean>
<bean id="attributes"
class="...metadata.commons.CommonsAttributes">
</bean>


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多