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’s SessionFactory
implementation and delegate to its own transactional version. If you are just using the simple ThreadLocal
-based session-per-request functionality, then when you attempt to open the Session
, you will get an IllegalStateException
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” SessionFactory
instead of the proxied one. A snippet of code from AbstractSessionFactoryBean
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>