您的位置:首页 > 编程语言 > Java开发

SSH 事务管理配置详解

2012-09-10 00:00 411 查看
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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息