hk2000c技术专栏

技术源于哲学,哲学来源于生活 关心生活,关注健康,关心他人

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  111 随笔 :: 1 文章 :: 28 评论 :: 0 Trackbacks

Spring通过AOP实现声明式事务管理。通常通过TransactionProxyFactoryBean设置Spring事务代理。我们需要一个目标对象包装在事务代理中。这个目标对象一般是一个普通Java对象的bean。当我们定义TransactionProxyFactoryBean时,必须提供一个相关的 PlatformTransactionManager的引用和事务属性。 事务属性含有上面描述的事务定义。

PlatformTransactionManager

HibernateTransactionManager需要一个SessionFactory的引用

JtaTransactionManager

一.把事务放置在了DAO层:

<!—hibernateTransactionManager-->

<bean id="transactionManager"

       class="org.springframework.orm.hibernate3.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>

<!—springDAO层的事务代理-->

<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>

<!—springService层的代理-->

<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>

<!—springDAO层的代理-->

<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方法:按顺序执行数据查询,数据删除,数据插入数据库行为,如果哪一步出异常(运行时异常),事务回滚,只有所有行为都没成功,事务提交。

posted on 2007-11-01 15:38 hk2000c 阅读(5373) 评论(0)  编辑  收藏 所属分类: CMS项目开发

只有注册用户登录后才能发表评论。


网站导航: