-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: No Hibernate Session bound to thread
PostPosted: Sat Nov 22, 2008 8:27 pm 
Newbie

Joined: Sun Nov 16, 2008 8:28 pm
Posts: 14
Location: Hamburg
Hi all,

my problem is a java.lang.IllegalStateException: No Hibernate Session bound to thread...

Code:
...
2008-11-23 00:40:30,612 [main] INFO  org.hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
2008-11-23 00:40:30,612 [main] INFO  org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
...
2008-11-23 00:40:30,690 [main] INFO  org.hibernate.impl.SessionFactoryImpl - building session factory
2008-11-23 00:40:31,018 [main] INFO  org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
2008-11-23 00:40:31,049 [main] INFO  org.springframework.orm.hibernate3.HibernateTransactionManager - Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@19977bd] of Hibernate SessionFactory for HibernateTransactionManager
...
java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
        at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:352)
        at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:202)
        at com.project.service.data.GeoIpHibernateDAO.locate(GeoIpHibernateDAO.java:37)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
        at $Proxy17.locate(Unknown Source)
        at com.project.service.servlet.LocaterServlet.doPost(LocaterServlet.java:40)
        at com.project.service.servlet.LocaterServlet.doGet(LocaterServlet.java:29)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Thread.java:619)


I use Hibernate 3 in combination with Spring to provide services that will be called from a Adobe Flex frontend above BlazeDS as RemoteObjects.

Code:
...
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="annotatedClasses">
         <list>
            <value>com.project.service.data.Country</value>
            <value>com.project.service.data.Language</value>
            <value>com.project.service.data.User</value>
            <value>com.project.service.data.Location</value>
            <value>com.project.service.data.GeoIp</value>
         </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
         </props>
    </property>
    <property name="dataSource">
       <ref bean="dataSource"/>
    </property>
  </bean>

   <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
         <ref local="sessionFactory"/>
      </property>
   </bean>
...
<!-- geo ip service -->
 
  <bean id="geoIpDAOBeanTarget" class="com.project.service.data.GeoIpHibernateDAO">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

  <bean id="geoIpDAOBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="txManager" />
    <property name="target" ref="geoIpDAOBeanTarget" />
    <property name="transactionAttributes">
      <props>
        <prop key="locateIp">PROPAGATION_REQUIRED</prop>
      </props>
    </property>
  </bean>
...


The configuration works very well, but now I need an additional regular Servlet and I thought that I could share the DAOs. To access the needed DAO I use the WebApplicationContext of Spring as a BeanFactory.

Code:
...
      try {
         WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
         GeoIpDAO geoIpDAO = (GeoIpDAO)context.getBean("geoIpDAOBean");
         geoIp = geoIpDAO.locate(ip);
      } catch(Exception e) {
         e.printStackTrace();
      }
...


I can access the DAO, but the mySQL query will result in the java.lang.IllegalStateException. My opinion is that a Session is given to the DAO by my xml config in applicationContext.xml.

Do I need another Session for my Servlet?

Does anybody have an idea to solve this issue?


Please help me :-|.

Thx a lot,
Nepumuk


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2008 4:21 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
Your spring config doesn't show that any transaction is configured. Spring defines contextual sessions provider that is able to participate in Spring-managed transactions (org.springframework.orm.hibernate3.SpringSessionContext). However, if no transaction settings are configured, it's not able to provide a session.

So, your task is to configure transaction settings for your DAOs.


Top
 Profile  
 
 Post subject: Yeah, this issue is solved...
PostPosted: Mon Nov 24, 2008 7:03 pm 
Newbie

Joined: Sun Nov 16, 2008 8:28 pm
Posts: 14
Location: Hamburg
You helped me on the right way.

Only the transaction management configuration was needed.

More information can be found on chapter 9 of the spring documention:
http://static.springframework.org/sprin ... ction.html

I know that is a lot of stuff. If u use annotations scoll down to 9.5.6. Using @Transactional.

I solved the problem with only two lines of code.

1. mark the DAO as transactional:
Code:
...
import org.springframework.transaction.annotation.Transactional;
...
@Transactional
public class GeoIpHibernateDAO implements GeoIpDAO {
...


2. enable the annotation driven transcation management in applicationContext.xml (where your beans are defined):
Code:
...
<!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>
...


Thx a lot for your help denis,
Nepumuk


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.