[翻译]在spring框架中添加HibernateInterceptor使得quartz可以调用Hibernate Se

翻译的这篇文章包含了好几个知识点,虽然短了点儿,但是非常有用。

1.spring

2.hibernate

3.quartz--定时调度工具,spring已经做了封装,也可以单独使用。

4.OpenSessionInViewFilter--web框架下的一个filter,能够让web request使用单一的hibernate session。

有的j2ee项目在web.xml文件中添加了OpenSessionInViewFilter,其目的是给web request提供单一的hibernate session,但是它也只能给web request提供hibernate session。也就是说,如果有某一个hibernate请求不是经由web request发起的,而是由quartz这样的定时任务发起的,那么quartz怎么样才能得到hibernate session呢?

解决办法就是使用HibernateInterceptor。当quartz发起hibernate session请求时,HibernateInterceptor会提供一个hibernate session给它。

配置文件如下:

 

xml 代码

 

  1. <!-- Declaration of HibernateInterceptor -->  
  2.     <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">  
  3.          <property name="sessionFactory">  
  4.            <ref bean="sessionFactory"/>  
  5.          </property>  
  6.     </bean>       
  7.     <!-- Manager template  -->  
  8.     <bean id="txProxyTemplate" abstract="true"  
  9.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  10.         <property name="transactionManager" ref="transactionManager"/>  
  11.         <property name="transactionAttributes">  
  12.             <props>  
  13.                 <prop key="save*">PROPAGATION_REQUIRED</prop>  
  14.                 <prop key="remove*">PROPAGATION_REQUIRED</prop>  
  15.                 <prop key="update*">PROPAGATION_REQUIRED</prop>                   
  16.                 <prop key="create*">PROPAGATION_REQUIRED</prop>      
  17.                 <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>  
  18.             </props>  
  19.         </property>  
  20.         <property name="preInterceptors">  
  21.             <list>  
  22.              <ref bean="hibernateInterceptor"/>  
  23.             </list>  
  24.         </property>                           
  25.     </bean>  
  26.     <!-- Sample Manager that encapsulates business logic -->  
  27.     <bean id="userManager" parent="txProxyTemplate">  
  28.         <property name="target">  
  29.             <bean class="com.acme.service.impl.UserManagerImpl" autowire="byName"/>  
  30.         </property>  
  31.     </bean>  


其中,<!-- Declaration of HibernateInterceptor -->部分定义了HibernateInterceptor;

 <!-- Manager template  -->部分定义了模板,并且加入了HibernateInterceptor;

<!-- Sample Manager that encapsulates business logic -->部分定义业务逻辑中的bean,记住,一定让他使用模板。

译者言:

1.这样做使得非web request可以使用j2ee资源。

2.我的做法和上面有些差别,但是也成功了。 我的做法是:

我没有定义<!-- Declaration of HibernateInterceptor -->这一部分,只定义了模板,在添加bean的时候使用模板。

 

原文如下:
Using HibernateInterceptor to bind Hibernate Sessions for Quartz jobs in Spring
I use the OpenSessionInViewFilter to provide a single Hibernate Session for each request to my Spring webapplications. It works transparently and well, but only for web requests. So how do you provide a Hibernate Session to your background jobs (like Quartz scheduled jobs)?

I wire a HibernateInterceptor into my proxy template for all the managers in my application. With this configuration, when Quartz fires up a job and makes a call to the manager/service layer the HibernateInterceptor binds a Hibernate Session to the background thread. Beautiful.

It looks something like this:(代码同上)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值