不能用OpenSessionInView的情况,例如在quartz的job中需要保持hibernate的session
参考一下链接
http://forum.springsource.org/showthread.php?71337-OSIV-in-a-non-web-application
Our system is divided into two major "application" and "kernel" layers. Each layer hosts of a number of different services & components all deployed and wired together via spring. The kernel layer defines mechanism and the application layer defines policy. There is also a web-layer which calls into applications & kernel components to do configuration, monitoring..etc. At the moment, everything is running in-process.
To promote loose coupling, we generate ApplicationEvent(s) within the kernel layer which are processed asynchronously (on different thread pool threads) up in the application layer by whoever cares about them. The event handlers often call back down into kernel services which do use @Transactional but with no outer session available, I had LIE problems. At the same time, I did not want the application level event handlers to define an outer transaction scope and I also didn't want to put REQUIRES_NEW on all service methods in the kernel layer.
This is how the system is structured and this problem has nothing do with web interfaces or views. I wanted OSIV functionality inside the application event handlers. There are valid arguments against OSIV but I personally feel OSIV is necessary to get the full productivity benefit(s) of using an ORM in the first place.
First, I tried annotating the event handler with
@Transactional(propagation=Propagation.NEVER) but that didn't work. The transaction manager still didn't bind a hibernate session for me. I'm not sure why but I didn't really look into it.
My solution also uses the HibernateInterceptor. I defined the interceptor and configured it with the same settings as the OSIV filter.
Code:
<bean id="hibernateSessionAdvice" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
<property name="flushModeName" value="FLUSH_NEVER"/>
<property name="exceptionConversionEnabled" value="false"/>
</bean>
Then I created a marker annotation I can stick on any method where I want the hibernate interceptor to run.
Code:
package com.tvrc.as.support.spring;
public @interface HibernateSession {
}
I added an aop:config entry to tie the advice to any methods marked with the annotation.
Code:
<aop:config>
<aop:advisor pointcut="@annotation(com.tvrc.as.support.spring.HibernateSession)" advice-ref="hibernateSessionAdvice"/>
</aop:config>
Finally, I just annotate event handlers where I'd like OSIV functionality.
Code:
@HibernateSession
public void onApplicationEvent(ApplicationEvent arg0) {
//Call kernel services that define transactions
//Session is available to avoid LIE
}
Spring is creating the application event listeners and wiring them all up so everything works well.
分享到:
相关推荐
Hibernate4,Interceptor,spring,quartz
主要介绍了Java的Hibernate框架中的Interceptor和Collection,Hibernate是Java的SSH三大web开发框架之一,需要的朋友可以参考下
使用Hibernate拦截器检测Persistence Context中的实体更改 使用Hibernate拦截器对数据库中的实体更改做出React 使用观察者设计模式来监视实体持久性更改并对其做出React 使用命令设计模式提供一种通用方法,可将...
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> ...
HibernateInterceptor HibernateJdbcException HibernateJdbcException HibernateJpaDialect HibernateJpaVendorAdapter HibernateObjectRetrievalFailureException HibernateObjectRetrievalFailureException...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
springmvc3.0+hibernate+interceptor+AOP注解+EHcache
使用拦截器时按如下步骤进行: (1)定义实现Interceptor接口的拦截器类 (2)通过Session启用拦截器,或者通过Configuration启用全局拦截器请看示例代码
Hibernate API 1 接口类型 1 .访问数据库的各种操作的接口 包括 Session 、 Transaction .......Hibernate .......Hibernate ...Interceptor 接口。
前几天搞 Spring + Hibernate + Struts 事务配置 ,网上找了好多资料,不过好无语,大多都是 Ctrl + V,浪费俺的宝贵时间 现在我总结配出一套,给大家参考参考,可能有不足,请大家多多交流。 附:内有弹出...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
Hibernate:spring.local.hibernate.cfg.xml Struts:struts.xml、struts.properties 配置很简单,用点心看就会了,多的不说了,好好享受咯 哦 忘记介绍了, 当中还有本人写的几个自定义标签:...
spring3.2+strut2+hibernate4 注解方式。 spring.xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context=...
需要指定的是Spring 1.2.5提供了两套Hibernate的支持包,其中Hibernate 2相关的封装类位于org.springframework.orm.hibernate2.*包中,而Hibernate 3.0的封装类位于org.springframework.orm.hibernate3.*包中,...
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> *">PROPAGATION_REQUIRED *">PROPAGATION_REQUIRED *">PROPAGATION_REQUIRED *">...
BeetSql是一个全功能DAO工具,同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。 在开发效率上,无需注解,自动使用大量内置SQL,轻易完成增删...
相比于Struts 1,Struts 2加入很多新的元素,如访问截取组件(interceptor)。 在Struts 2的技术框架,所有的请求均被截取组件先处理,再交由动作类分发 。Struts.xml是Struts 2的必须配置文件,放在WEB-INF/classes...