SSH 事务管理配置详解

SSH配置教程多如牛毛,但最近配置确确实实遇到了很棘手的问题,比如能执行插入查找功能却不能删除,下面详细说下配置流程:
web.xml文件:
<!--hibernateFilter 过滤器要放到struts前面  -->
<filter>
<filter-name>hibernateFilter</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>
<!-- OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自动被 Spring 的事务管理器探测到-->
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>

    <!--不配置会出现service层调用dao层只读模式下写操作不被引许-->
<init-param><
       <param-name>flushMode</param-name>
       <param-value>AUTO</param-value>
        </init-param>
</filter>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


application-context.xml文件:
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:annotation-config />

<!-- 事务处理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 事务控制 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true" />
</tx:attributes>
</tx:advice>

<aop:config>
<!-- 管理事务操作 -->
<aop:pointcut id="servicesPointcut"
expression="execution(* com.*.service.*.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesPointcut" />
</aop:config>

一些需需要注意的地方:
1、OpenSessionInViewFilter作用是提供对hibernate延迟加载的支持,当我们查询一个具有延迟加载的领域对象,在web层访问这个对象延迟加载的属性时会报错,因为此时执行查询操作的session已经关闭了,OpenSessionViewFilter将这个session和一个完整的请求线程绑定了,也就是在整个请求过程中session都是开放的,所以就能访问延迟加载的属性。
2、<aop:aspectj-autoproxy proxy-target-class="true" />这里为true了,Spring会使用CGLIB代理,如果Service采用构造函数注入,还需要生成一个默认的空构造函数,CGLIB会用到。如果为false,Spring将使用JDK动态代理还进行AOP。

转载于:https://my.oschina.net/johny/blog/77685

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值