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。
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。
相关文章推荐
- spring的annotation-driven配置事务管理器详解
- spring的annotation-driven配置事务管理器详解
- Spring详解之三:配置事务管理
- Spring配置文件详解三:Spring声明式事务管理
- spring的annotation-driven配置事务管理器详解
- spring的annotation-driven配置事务管理器详解
- ssh-配置事务管理器
- spring的annotation-driven配置事务管理器详解
- spring的annotation-driven配置事务管理器详解
- SSH中事务配置详解(使用tx标签配置的拦截器)
- spring的annotation-driven配置事务管理器详解
- Spring+SpringMVC配置事务管理无效原因及解决办法详解
- SSH中事务配置详解
- SSH中事务配置详解
- spring :annotation-driven配置事务管理器详解
- Spring中annotation-driven配置事务管理器详解
- spring的annotation-driven配置事务管理器详解
- spring的annotation-driven配置事务管理器详解
- SSH中事务配置详解(全注解)
- SSH中事务配置详解(使用拦截器)