Spring and Hibernate’s getCurrentSession() ErrorS!

If you are using Spring to wrap a Hibernate SessionFactory and you are not using Spring-managed transactions, you may run into an issue. The reason is that Spring by default will wrap Hibernate’sSessionFactory implementation and delegate to its own transactional version. If you are just using the simpleThreadLocal-based session-per-request functionality, then when you attempt to open the Session, you will get anIllegalStateException thrown, with the error message "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here". This happens because Spring’s SessionFactoryUtils checks if the Session is bound to Spring’s transactional support, and by default throws an error if it is not.

The solution to this is to set the property

<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>

in the Spring config. This will return the “raw” SessionFactoryinstead of the proxied one. A snippet of code fromAbstractSessionFactoryBean shows where the check is done:

 
/**
   * Wrap the given SessionFactory with a transaction-aware proxy, if demanded.
   @param rawSf the raw SessionFactory as built by <code>buildSessionFactory()</code>
   @return the SessionFactory reference to expose
   @see #buildSessionFactory()
   @see #getTransactionAwareSessionFactoryProxy
   */
  protected SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) {
    if (isExposeTransactionAwareSessionFactory()) {
       return getTransactionAwareSessionFactoryProxy(rawSf);
    }
    else {
      return rawSf;
    }
  }

A sample Spring config is shown below.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
     <property name="exposeTransactionAwareSessionFactory"><value>false</value></property>
    <property name="annotatedPackages">
      <list>
        <value>uk.co.researchkitchen.hibernate</value>
      </list>
    </property>
    <property name="annotatedClasses">
        <list>
                <value>uk.co.researchkitchen.hibernate.Product</value>
                <value>uk.co.researchkitchen.hibernate.ProductDescription</value>
        </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
        hibernate.show_sql=true
            hibernate.hbm2ddl.auto=create
            hibernate.current_session_context_class=thread
      </value>
    </property>
  </bean>

</beans>
http://www.theresearchkitchen.com/blog/archives/73

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值