Spring 3.1 + Hibernate 4.1 Propagation.Supports问题 [英] Spring 3.1 + Hibernate 4.1 Propagation.Supports issue

查看:87
本文介绍了Spring 3.1 + Hibernate 4.1 Propagation.Supports问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的项目从Spring 3.0 + hibernate 3.6.x迁移到S3.1 + H4.1我的新代码如下所示

 < context:component-scan base-package =xyz> 
< / context:component-scan>

< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.x< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto> update< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< property name =annotatedClasses>
< list>
<值> x.y.z.entities.Student< /值>
< / list>
< / property>
< / bean>

< bean id =transactionManager
class =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>

< aop:config>
expression =execution(* x.y.z.StudentDao。*(..))/>
< / aop:config>

< tx:advice id =txAdvicetransaction-manager =transactionManager>
< tx:attributes>
< tx:method name =save *propagation =REQUIRED/>
< tx:method name =update *propagation =REQUIRED/>
< tx:method name =delete *propagation =REQUIRED/>
< tx:方法名称=get *propagation =SUPPORTS只读=true/>
< / tx:属性>
< / tx:advice>

当运行getStudent方法标记为SUPPORTS并且只读时,我得到

  org.hibernate.HibernateException:没有找到当前线程的会话
在org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1024)



< Spring 3.0和Hibernate 3.6.x现在已经改变了。如果我需要使用 sessionFactory.getCurrentSession();



为了在我的代码中获得最大的并发速度,我使用了较低级别的技术。
当执行需要几次get / save / update /查询的操作时,我按照以下方式执行:


  1. SUPPORTS

  2. 执行所有查询,这些查询也被标记为 SUPPORTS
    第一个方法。然后
  3. 开始在同一个方法内标记为 REQUIRED 的查询,这是一个点

    $ b $ p
    $ b我使用这种技术获得了很好的性能改进,但是将我的所有方法标记为REQUIRED摧毁它。



    如何解决它?

    解决方案

    在试验Spring和Hibernate 3/4时遇到同样的问题。 $ b

    看起来这是一个已知问题,在以下JIRA链接中进行了描述。



    https://jira.springsource.org/browse/SPR-9020



    它看起来像Hibernate 4版本的SpringSessionContext没有打开一个新的会话,如果没有现有的事务/会话打开和被调用的方法@Transactional配置传播=传播.UPPORTS。


    I'm migrating my project from Spring 3.0 +hibernate 3.6.x to S3.1 + H4.1

    my new code is the following

        <context:component-scan base-package="x.y.z">
    </context:component-scan>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
     <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.x</prop> 
        <prop key="hibernate.show_sql">true</prop> 
        <prop key="hibernate.hbm2ddl.auto">update</prop> 
        <prop key="hibernate.show_sql">true</prop> 
      </props>
         </property>
            <property name="annotatedClasses">
          <list>
            <value>x.y.z.entities.Student</value>        
             </list>
        </property>
      </bean>
    
    <bean id="transactionManager" 
                class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
      </bean>
    
    <aop:config>
        <aop:pointcut id="daoServicePoint" 
                expression="execution(* x.y.z.StudentDao.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoServicePoint"/>
      </aop:config>
    
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
          <tx:method name="save*" propagation="REQUIRED"/>
           <tx:method name="update*" propagation="REQUIRED"/>
           <tx:method name="delete*" propagation="REQUIRED"/>
         <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
      </tx:advice> 
    

    When running getStudent method marker as SUPPORTS and read only I'm getting

    org.hibernate.HibernateException: No Session found for current thread
        at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
        at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1024)
    

    It used to be ok with Spring 3.0 and Hibernate 3.6.x now it was changed. I undestood from Spring forums that mow I need mark transaction REQUIRED if I need to use sessionFactory.getCurrentSession();

    I used lower level technique in order to get maximum concurrent speed in my code. When performing operations which require several get/save/update/ queries i did it the following way:

    1. called method marked as SUPPORTS.
    2. Performed all get queries which are also marked as SUPPORTS inside first method.
    3. then started queries which marked as REQUIRED inside the same method and this is a point where my roll-able back transaction begins.

    I got good performance improvement using this technique, but marking all my methods as REQUIRED destroys it.

    How can work around it?

    解决方案

    I encountered the same issue when experimenting with Spring and Hibernate 3 / 4.

    It looks like this is a known issue, which is described in the following JIRA link.

    https://jira.springsource.org/browse/SPR-9020

    It looks like Hibernate 4 version of SpringSessionContext does not open a new session if there is no existing transaction/session open and the called method @Transactional is configured with propagation = Propagation.SUPPORTS.

    这篇关于Spring 3.1 + Hibernate 4.1 Propagation.Supports问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆