HibernateInterceptor
3502 단어 Interceptor
링크 참조
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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Spring] Login Filter vs. Interceptor웹과 관련된 공통 관심사를 처리할 때는 HTTP의 헤더나 URL의 정보들이 필요한데, 서블릿 필터나 스프링 인터셉터는 HttpServletRequest 를 흐름 TTP 요청 -> WAS -> 필터 -> 서블릿 -> ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.