?事務(wù)管理的原理:Aop為普通java類封裝事務(wù)控制,它是通過動(dòng)態(tài)代理實(shí)現(xiàn)的,spring在這段時(shí)間內(nèi)通過攔截器,加載事務(wù)切片。由于Aop的原理是動(dòng)態(tài)加載(回去研究一下proxy動(dòng)態(tài)加載)來進(jìn)行事務(wù)控制的。
事務(wù)管理的步驟(以hibernate為例):
1.定義一個(gè)hibernate適用的事務(wù)管理器,將SessionFactory注入進(jìn)去:
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
? ?? ?<property name="sessionFactory">
? ?? ?? ?<ref bean="sessionFactory"/>
? ?? ?</property>
</bean>
2.定義事務(wù)管理的策略:
<bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
? ?? ?<property name="properties">
? ?? ?? ?<props>
? ?? ?? ?? ?<prop key="add">
? ?? ?? ?? ?? ?PROPAGATION_REQUIRES_NEW
? ?? ?? ?? ?</prop>
? ?? ?? ?</props>
? ?? ?</property>
? ?</bean>
還可以定義事務(wù)隔離程度和回滾
3.定義一個(gè)事務(wù)管理器(動(dòng)態(tài)加載來實(shí)現(xiàn)的)
<bean id="studentManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
? ?? ?<property name="target">
? ?? ?? ?<ref bean="studentManager"/>
? ?? ?</property>
? ?? ?<property name="transactionManager">
? ?? ?? ?<ref bean="transactionManager"/>
? ?? ?</property>
? ?? ?<property name="transactionAttributeSource">
? ?? ?? ?<ref bean="transactionAttributeSource"/>
? ?? ?</property>
</bean>
這個(gè)bean的外觀是一個(gè)接口(StudentManagerInterface),我們指出了它的具體實(shí)現(xiàn)(studentManager),而且為它綁定了事務(wù)策略
附A pring中的所有事務(wù)策略
AOP應(yīng)用范圍
附B Spring中所有的隔離策略:
ISOLATION_DEFAULT
ISOLATION_READ_UNCOMMITED
ISOLATION_COMMITED
ISOLATION_REPEATABLE_READ
ISOLATION_SERIALIZABLE
Authentication 權(quán)限
Caching 緩存
Context passing 內(nèi)容傳遞
Error handling 錯(cuò)誤處理
Lazy loading 懶加載
Debugging 調(diào)試
logging, tracing, profiling and monitoring 記錄跟蹤 優(yōu)化 校準(zhǔn)
Performance optimization 性能優(yōu)化
Persistence 持久化
Resource pooling 資源池
Synchronization 同步
Transactions 事務(wù)
典型的hibenrnate事務(wù)管理
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory“?? class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
????????????????<property name="dataSource"><ref local="dataSource" /></property>
????????????????<property name="mappingResources">
????????????????????????<list><!-- Add list of .hbm.xml files here -->
????????????????????????????????<value>org/mzone/model/Tuser.hbm.xml</value>
????????????????????????????????<value>org/mzone/model/Article.hbm.xml</value>
????????????????????????</list>
????????????????</property>
????????????????<property name="hibernateProperties">
????????????????????????<props>
????????????????????????????????<prop ????????key="hibernate.dialect">net.sf.hibernate.dialect.SybaseDialec</prop>
????????????????????????????????<prop key="hibernate.show_sql">True</prop>
????????????????????????</props>
????????????????</property>
????????</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
????????<bean id="transactionManager"
????????????????class="org.springframework.orm.hibernate.HibernateTransactionManager">
????????????????<property name="sessionFactory"><ref local="sessionFactory" /></property>
????????</bean>????????
????????<bean id="baseTxProxy" lazy-init="true" ????????class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
????????<property name="transactionManager"><ref bean="transactionManager"/></property>
?????????? <property name="target">
?????????? ????????<ref local=" userManagerTarget " />
????????????</property>
????????<property name="transactionAttributes">
????????????<props>
????????????????????????????<prop key="save*">PROPAGATION_REQUIRED</prop>
????????????????????????<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
????????????????????????<prop key="remove*">PROPAGATION_REQUIRED</prop>
????????????????????????<prop key="*">PROPAGATION_REQUIRED</prop>
????????????</props>
????????</property>
????</bean>
????<bean id="userManagerTarget" class="org.mzone.service.impl.UserManagerImpl">
????????<property name="userDAO"><ref local="userDAO"/></property>
????????<property name="articleDao"><ref local="articleDAO"/></property>
????</bean>
userDAO和articleDao是接口