No Hibernate Session bound to thread exception
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:
<context:annotation-config />
<context:component-scan base-package="com.maxheapsize" />
<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.saveOrUpd开发者_JS百科ate(name);
session.flush();
}
}
Spring version isn't your issue.
I'd also recommend not annotating your DAO with transactions. Those belong on a service tier that has the DAO injected in. That's where the session comes in as well: open the session for the use case, execute it, close the transaction, clean it up.
You forgot to enable annotation driven transaction management. In the link, search for tx:advice
.
Add
<beans xmlns:tx="http://www.springframework.org/schema/tx
and
<tx:annotation-driven />
to your context file.
Comment the property in the hibernate.cfg.xml file
thread
add taransaction manager :
<bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
id="hibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
and annotate all ur DAO classes with @Transactional
精彩评论