不能用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
编写HibernateInterceptor,负责开启和关闭Session,以及处理异常。 ```java public class HibernateInterceptor implements Interceptor { public void destroy() {} public void init() {} public String ...
使用Hibernate拦截器检测Persistence Context中的实体更改 使用Hibernate拦截器对数据库中的实体更改做出React 使用观察者设计模式来监视实体持久性更改并对其做出React 使用命令设计模式提供一种通用方法,可将...
3. **使用HibernateTemplate或HibernateInterceptor**:Spring提供了HibernateTemplate或HibernateInterceptor,这两个工具类可以帮助我们更好地管理和控制Hibernate的操作,避免直接操作Session,降低出错的可能性...
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> ...
6. **Hibernate Interceptor**:可以自定义拦截器,实现对 Hibernate 操作的扩展,如日志记录、性能监控等。 7. **AOP(Aspect Oriented Programming)**:Spring 的 AOP 功能可用于实现如日志、事务管理等横切关注...
`HibernateInterceptor`是一个非常强大的接口,允许开发者自定义Hibernate的行为。通过实现这个接口,可以在Hibernate执行某些操作前后添加自定义的逻辑,比如日志记录、安全性检查等。 总的来说,Hibernate的高级...
Spring提供了HibernateTemplate和HibernateInterceptor等工具类,简化了事务管理,并且通过AOP(面向切面编程)实现了声明式事务,使得事务处理更加透明化。此外,Spring还允许我们方便地进行数据源配置,以及自动...
- **HibernateInterceptor**:这是一个非常灵活的机制,允许在Hibernate内部操作的不同阶段进行拦截。通过实现`HibernateInterceptor`接口,开发者可以自定义拦截逻辑,例如日志记录、安全性检查等。 通过上述内容...
同时,`SessionFactory`和`Session`的创建可以通过`LocalSessionFactoryBean`和`HibernateTemplate`或`HibernateInterceptor`进行注解配置。 Struts2作为MVC框架,负责处理HTTP请求和响应。它的注解如`@Action`、`@...
3. 配置HibernateTemplate或HibernateInterceptor:这两个是Spring提供的便捷工具,用于简化Hibernate操作。 4. 配置DAO(Data Access Object):创建DAO层,使用HibernateTemplate或Session来执行数据库操作。 5. ...
我们可以使用`HibernateTemplate`或`HibernateInterceptor`进行进一步的数据访问操作。例如,使用`HibernateTemplate`: ```xml <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5....
2. 使用Spring的`HibernateTemplate`或`HibernateInterceptor`进行数据访问操作。 3. 将Hibernate的DAO层注入到Spring MVC的Controller中,实现业务逻辑。 4. 配置Spring的事务管理器,例如`...
2. **SessionFactory和Session的管理**:Spring通过`SessionFactoryBean`来配置和管理`SessionFactory`,并使用`HibernateTemplate`或`HibernateInterceptor`来管理`Session`,避免了手动创建和关闭`Session`可能...
同时,它还提供了`HibernateTemplate`和`HibernateInterceptor`,帮助开发者更方便地操作Hibernate。 2. **Hibernate核心库**:这包括了Hibernate的主要API,如`Session`, `SessionFactory`, `Query`等,以及实体...
而Hibernate则可以通过SessionFactory的getCurrentSession()方法,结合Spring的HibernateTemplate或HibernateInterceptor,确保每个数据库操作都在正确的数据源上下文中进行。 为了便于理解和实践,示例压缩包可能...
5. 在Struts2中配置Hibernate插件,如Hibernate Interceptor,以支持事务管理。 在实际开发中,还需要考虑异常处理、安全性、性能优化等方面的问题。例如,使用AOP进行异常统一处理,利用缓存提高数据读取效率,...
在源码中,`JdbcTemplate`和`SimpleJdbcInsert`是简化JDBC操作的工具类,而`HibernateTemplate`和`HibernateInterceptor`则提供了对Hibernate的支持。 除了以上核心模块,Spring Framework 5.0.14.RELEASE还包含了...
`JdbcTemplate`和`SimpleJdbcInsert`简化了JDBC操作,`HibernateTemplate`和`HibernateInterceptor`提供了对Hibernate的支持。 10. **Spring Boot**:Spring Boot简化了Spring应用的启动和配置。`SpringApplication...