ssh框架中事务管理配置

来源:互联网 发布:淘宝返现短信怎么写 编辑:程序博客网 时间:2024/06/09 15:46

方法一:

<bean id="txManager" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   <property name="transactionManager" ref="transactionManager" />   <property name="transactionAttributes">    <props>        <prop key="remove*">PROPAGATION_REQUIRED,-DataAccessException</prop>     <prop key="update*">PROPAGATION_REQUIRED,-DataAccessException</prop>     <prop key="handle*">PROPAGATION_REQUIRED,-DataAccessException</prop>     <prop key="save*">PROPAGATION_REQUIRED,-DataAccessException</prop>     <prop key="init*">PROPAGATION_REQUIRED,-DataAccessException</prop>     <prop key="create*">PROPAGATION_REQUIRED,-DataAccessException</prop>     <prop key="*">PROPAGATION_REQUIRED</prop>    </props>    </property></bean>


这样配置可以对各个层的业务进行管理.在业务层里调用dao接口,根据事务可以同时提交,同时回滚.

方法二:

<!--   定义一个切面--><tx:advice id="txAdvice" transaction-manager="transactionManager">   <tx:attributes>    <tx:method name="handleAdd*" propagation="REQUIRED" rollback-for="AccessException"/>    <tx:method name="handleUpdate*" propagation="REQUIRED" rollback-for="AccessException"/>    <tx:method name="handleGet*" propagation="REQUIRED" />    <tx:method name="handleDel*" propagation="REQUIRED" rollback-for="AccessException" />    <tx:method name="remove*" read-only="false" propagation="REQUIRED" rollback-for="AccessException" />    <tx:method name="update*" propagation="REQUIRED" rollback-for="AccessException"/>    <tx:method name="add*" propagation="REQUIRED" rollback-for="AccessException" />    <tx:method name="save*" propagation="REQUIRED" rollback-for="AccessException"/>    <tx:method name="delete*" propagation="REQUIRED" rollback-for="AccessException" />    <tx:method name="modify*" propagation="REQUIRED" rollback-for="AccessException" />    <tx:method name="create*" propagation="REQUIRED" rollback-for="AccessException" />    <tx:method name="init*" propagation="REQUIRED" rollback-for="AccessException" />   </tx:attributes></tx:advice><!--   哪些业务类使用切面--><aop:config>   <aop:pointcut id="reg"    expression="execution(* com.service.impl..*.*(..))"/>   <aop:advisor advice-ref="txAdvice" pointcut-ref="reg" /></aop:config>


   第二种方法对应的web.xml配置


<filter>   <filter-name>OpenSessionInViewFilter</filter-name>   <filter-class>    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter   </filter-class>   <init-param>    <param-name>sessionFactoryBeanName</param-name>    <param-value>sessionFactory</param-value>   </init-param>   <init-param><!---必须配置,且必须为true(开启),因为OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自动被 Spring 的事务管理器探测到->            <param-name>singleSession</param-name>            <param-value>true</param-value>               </init-param>         <init-param><!--必须配置,否则在service层调用dao方法时会出现在只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许 的问题-->        <param-name>flushMode </param-name>      <param-value>AUTO</param-value>                 </init-param> </filter>.....<filter-mapping>   <filter-name>OpenSessionInViewFilter</filter-name>   <url-pattern>*.do</url-pattern></filter-mapping>


web.xml中为什么要配置OpenSessionInViewFilter?
Hibernate 允许对关联对象、属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之

内进行。如果 Service 层返回一个启用了延迟加载功能的领域对象给 Web 层,当 Web 层访问到那些需要延迟加载的数

据时,由于加载领域对象的 Hibernate Session 已经关闭,这些导致延迟加载数据的访问异常


OpenSessionInViewFilter的主要功能是用来把一个

HibernateSession和一次完整的请求过程对应的线程相绑定,它将自动被Spring的事务管理器探测到。目的是为了实

现"Open Session in View"的模式。它允许在事务提交后延迟加载显示所需要的对象。

原创粉丝点击