No Hibernate Session bound to thread exception
- by Benchik
I have Hibernate 3.6.0.Final and Spring 3.0.0.RELEASE
I get "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"
If I add the thread specification back in, I get "saveOrUpdate is not valid without active transaction"
Any ideas?
The spring-config.xml:
<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:mem:jsf2demo"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="sampleSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="sampleDataSource"/>
<property name="annotatedClasses">
<list>
<value>com.maxheapsize.jsf2demo.Book</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- prop key="hibernate.connection.pool_size">0</prop-->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<!-- prop key="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.current_session_context_class">thread</prop-->
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="sampleDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>
jdbc:hsqldb:file:/spring/db/springdb;SHUTDOWN=true
</value>
</property>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sampleSessionFactory"/>
</bean>
<bean id="daoTxTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="create*">
PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
</prop>
<prop key="get*">
PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
</prop>
</props>
</property>
</bean>
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sampleSessionFactory"/>
<property name="singleSession" value="true"/>
</bean>
<bean id="nameDao" parent="daoTxTemplate">
<property name="target">
<bean class="com.maxheapsize.dao.NameDao">
<property name="sessionFactory" ref="sampleSessionFactory"/>
</bean>
</property>
</bean>
and the DAO:
public class NameDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Transactional
@SuppressWarnings("unchecked")
public List<Name> getAll() {
Session session = this.sessionFactory.getCurrentSession();
List<Name> names = (List<Name>)session.createQuery("from Name").list();
return names;
}
//@Transactional(propagation= Propagation.REQUIRED, readOnly=false)
@Transactional
public void save(Name name){
Session session = this.sessionFactory.getCurrentSession();
session.saveOrUpdate(name);
session.flush();
}
}