?事務管理的原理:Aop為普通java類封裝事務控制,它是通過動態代理實現的,spring在這段時間內通過攔截器,加載事務切片。由于Aop的原理是動態加載(回去研究一下proxy動態加載)來進行事務控制的。
事務管理的步驟(以hibernate為例):
1.定義一個hibernate適用的事務管理器,將SessionFactory注入進去:
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
? ?? ?<property name="sessionFactory">
? ?? ?? ?<ref bean="sessionFactory"/>
? ?? ?</property>
</bean>
2.定義事務管理的策略:
<bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
? ?? ?<property name="properties">
? ?? ?? ?<props>
? ?? ?? ?? ?<prop key="add">
? ?? ?? ?? ?? ?PROPAGATION_REQUIRES_NEW
? ?? ?? ?? ?</prop>
? ?? ?? ?</props>
? ?? ?</property>
? ?</bean>
還可以定義事務隔離程度和回滾
3.定義一個事務管理器(動態加載來實現的)
<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>
這個bean的外觀是一個接口(StudentManagerInterface),我們指出了它的具體實現(studentManager),而且為它綁定了事務策略
附A pring中的所有事務策略
AOP應用范圍
附B Spring中所有的隔離策略:
ISOLATION_DEFAULT
ISOLATION_READ_UNCOMMITED
ISOLATION_COMMITED
ISOLATION_REPEATABLE_READ
ISOLATION_SERIALIZABLE
Authentication 權限
Caching 緩存
Context passing 內容傳遞
Error handling 錯誤處理
Lazy loading 懶加載
Debugging 調試
logging, tracing, profiling and monitoring 記錄跟蹤 優化 校準
Performance optimization 性能優化
Persistence 持久化
Resource pooling 資源池
Synchronization 同步
Transactions 事務
典型的hibenrnate事務管理
<!-- 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是接口