I am getting this exception when I call a DAO method which uses SessionFactory.getCurrentSession()
. The DAO class is annotated with @Transactional
I got the following error:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
I fixed this by changing my hibernate properties file
hibernate.current_session_context_class=thread
My code and configuration file as follows
session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.beginTransaction();
session.createQuery(Qry).executeUpdate();
session.getTransaction().commit();
on properties file
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.query_factory_class=org.hibernate.hql.ast.ASTQueryTranslatorFactory
hibernate.current_session_context_class=thread
on cofiguration file
<properties>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.query.factory_class">${hibernate.query_factory_class}</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
</props>
</property>
</properties>
Thanks,
Ashok
after I add the property:
<prop key="hibernate.current_session_context_class">thread</prop>
I get the exception like:
org.hibernate.HibernateException: createQuery is not valid without active transaction
org.hibernate.HibernateException: save is not valid without active transaction.
so I think setting that property is not a good solution.
finally I solve "No Hibernate Session bound to thread" problem :
1.<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
2.add <tx:annotation-driven />
to servlet-context.xml or dispatcher-servlet.xml
3.add @Transactional after @Service and @Repository
I had the same issue today.While searching here for solution,I have did silly mistake that is instead of importing
import org.springframework.transaction.annotation.Transactional;
unknowingly i have imported
import javax.transaction.Transactional;
Afer changing it everything worked fine.
So thought of sharing,If somebody does same mistake .
Whenever you will face below error just follow it.
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
Put a @Transactional
annotation for each method of implementing classes.
You are missing <context:annotation-config />
from your spring context so the annotations are not being scanned!
You can have the @Transactional in the child class, but you have to override each of the methods and call the super method in order to get it to work.
Example:
@Transactional(readOnly = true)
public class Bob<SomeClass> {
@Override
public SomeClass getValue() {
return super.getValue();
}
}
This allows it to set it up for each of the methods it's needed for.