Spring通過AOP實現聲明式事務管理
通常通過TransactionProxyFactoryBean設置Spring事務代理
我們需要一個目標對象包裝在事務代理中
這個目標對象一般是一個普通Java對象的bean
當我們定義TransactionProxyFactoryBean時
必須提供一個相關的 PlatformTransactionManager的引用和事務屬性
事務屬性含有上面描述的事務定義
PlatformTransactionManager
HibernateTransactionManager需要一個SessionFactory的引用
JtaTransactionManager
一.把事務放置在了DAO層
<!—hibernateTransactionManager
>
<bean id=
transactionManager
class=
org
springframework
orm
hibernate
HibernateTransactionManager
>
<property name=
sessionFactory
>
<ref local=
sessionFactory
/>
</property>
</bean>
<!—DAO層接口實現
>
<bean id=
companyDAOTarget
class=
com
vstsoft
querycompany
dao
impl
CompanyDAOImpl
>
<property name=
sessionFactory
>
<ref local=
sessionFactory
/>
</property>
</bean>
<!—spring對DAO層的事務代理
>
<bean id=
companyDAOProxy
class=
org
springframework
transaction
interceptor
TransactionProxyFactoryBean
>
<property name=
transactionManager
>
<ref bean=
transactionManager
/>
</property>
<property name=
target
>
<ref local=
companyDAOTarget
/>
</property>
<property name=
transactionAttributes
>
<props>
<prop key=
insert*
>PROPAGATION_REQUIRED</prop>
<prop key=
delete*
>PROPAGATION_REQUIRED</prop>
<prop key=
find*
>
PROPAGATION_REQUIRED
readOnly
</prop>
</props>
</property>
</bean>
<!—業務層接口實現
把DAO注入到Service裡面
>
<bean name=
companyManageTarget
class=
com
vstsoft
querycompany
service
impl
CompanyManageTarget
>
<property name=
companyDAO
>
<ref bean=
companyDAOProxy
/>
</property>
</bean>
<!—spring對Service層的代理
>
<bean id=
companyManageProxy
class=
org
springframework
aop
framework
ProxyFactoryBean
>
<property name=
proxyInterfaces
>
<value>com
vstsoft
querycompany
service
CompanyManage</value>
</property>
<property name=
target
>
<ref bean=
companyManageTarget
/>
</property>
</bean>
<!—配置struts訪問
把service層注入到action裡面
>
<bean name=
/company
class=
com
vstsoft
querycompany
web
action
CompanyAction
singleton=
false
>
<property name=
companyManage
>
<ref local=
companyManageProxy
/>
</property>
</bean>
二.把事務放置在了Service層
<!—jtaTransactionManager
>
<bean id=
jtaTransactionManager
class=
org
springframework
transaction
jta
JtaTransactionManager
/>
<!—DAO層接口實現
>
<bean id=
companyDAOTarget
class=
com
vstsoft
querycompany
dao
impl
CompanyDAOImpl
>
<property name=
sessionFactory
>
<ref local=
sessionFactory
/>
</property>
</bean>
<!—spring對DAO層的代理
>
<bean id=
companyDAOProxy
class=
org
springframework
aop
framework
ProxyFactoryBean
>
<property name=
proxyInterfaces
>
<value>com
vstsoft
querycompany
dao
CompanyDAO</value>
</property>
<property name=
target
>
<ref bean=
companyDAOTarget
/>
</property>
</bean>
<!—業務層接口實現
把DAO注入到Service裡面
>
<bean name=
companyManageTarget
class=
com
vstsoft
querycompany
service
impl
CompanyManageTarget
>
<property name=
companyDAO
>
<ref bean=
companyDAOProxy
/>
</property>
</bean>
<!—spring代理業務層的事務管理
>
<bean id=
companyManageProxy
class=
org
springframework
transaction
interceptor
TransactionProxyFactoryBean
>
<property name=
transactionManager
>
<ref local=
jtaTransactionManager
/>
</property>
<property name=
transactionAttributes
>
<props>
<prop key=
set*
>PROPAGATION_REQUIRED</prop>
<prop key=
find*
>PROPAGATION_REQUIRED
readOnly</prop>
</props>
</property>
<property name=
target
>
<ref bean=
companyManageTarget
/>
</property>
</bean>
<!—配置struts訪問
把service層注入到action裡面
>
<bean name=
/company
class=
com
vstsoft
querycompany
web
action
CompanyAction
singleton=
false
>
<property name=
companyManage
>
<ref local=
companyManageProxy
/>
</property>
</bean>
在service層的接口實現CompanyManageImpl裡面有個setData方法
按順序執行數據查詢
數據刪除
數據插入數據庫行為
如果哪一步出異常(運行時異常)
事務回滾
只有所有行為都沒成功
事務提交
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28057.html