[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

Asked by ucacppw in Spring, Java Programming Language

I have a legacy webapp which uses a servlet filter to wrap each request as follows

           
HibernateUtil.beginTransaction();
chain.doFilter(request, response);         
(boilerplate code which then calls one of the following...)          
HibernateUtil.commitTransaction();            
or            
HibernateUtil.rollbackTransaction();

HibernateUtil created a sessionFactory according to info in hibernate.cfg.xml as follows (I was using hibernate3 contextual sessions)

<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/filmSource</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>      
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="film/hibernate/Commission.hbm.xml" />
</session-factory>
</hibernate-configuration>

Everything worked fine.

However the app has now been integrated with Spring and I want to configure the Hibernate session in springs applicationContext.xml

Here is what I currently have in there..

  <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/filmSource"/>
  </bean>
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
    <list>
      <value>film/hibernate/Commission.hbm.xml</value>
    </list>
    </property>
<property name="hibernateProperties">

<props>
  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  <prop key="hibernate.cache.use_second_level_cache">true</prop>
  <prop key="hibernate.current_session_context_class">thread</prop>
  <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
  <prop key="hibernate.show_sql">true</prop>
</props>
    </property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>

<bean id="hibernateUtil" class="film.web.framework.util.HibernateUtil"/>

and here is my hibernateUtil which has now been modified for Spring.

public class HibernateUtil {
      private static Log log = LogFactory.getLog(HibernateUtil.class);

      private static SessionFactory sessionFactory;

      @Autowired
      public void setMySessionFactory(SessionFactory value) {
            sessionFactory = value;      
      }
      

      public static SessionFactory getSessionFactory() {
            return sessionFactory;
      }

      public static Session getSession() {
            return getSessionFactory().getCurrentSession();
      }

      public static void beginTransaction() {
            getSessionFactory().getCurrentSession().beginTransaction();

      }

      public static void commitTransaction() {
            getSessionFactory().getCurrentSession().getTransaction().commit();
      }

      public static void rollbackTransaction() {
            getSessionFactory().getCurrentSession().getTransaction().rollback();
      }

}

The problem is when the request filter calls HibernateUtil.beginTransaction() I get the following error:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here.
 
Related Solutions
Keywords: No Hibernate Session bound to thread…
 
Loading Advertisement...
 
[+][-]07/20/09 07:41 AM, ID: 24895491Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/20/09 09:01 AM, ID: 24896342Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/03/09 06:06 AM, ID: 25003936Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Spring, Java Programming Language
Sign Up Now!
Solution Provided By: ucacppw
Participating Experts: 1
Solution Grade: A
 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20090701_SELECT_ZONES