Announcement Announcement Module
Collapse
No announcement yet.
No Hibernate session bound to thread Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No Hibernate session bound to thread

    hi,

    i am getting the following exception.

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'dataService' defined in resource [/WEB-INF/spring-config.xml] of ServletContext: Initialization of bean failed; nested exception is java.lang.IllegalStateException: No Hibernate session bound to thread, and configuration does not allow creation of new one here


    my spring-config.xml is

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource"><property name="driverClassName">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
    <value>jdbc:oracle:oci:@//qatestmachine:1521/ORCL</value>
    </property>
    <property name="username">
    <value>scott</value>
    </property>
    <property name="password">
    <value>tiger</value>
    </property>
    </bean>

    <bean id="hibSessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    <list>
    <value>ex.hbm.xml</value>
    <value>ex1.Tbl.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.O racleDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.cglib.use_reflection_optimizer">tru e</prop>
    </props>
    </property>
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>

    <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.Hibernate Interceptor">
    <property name="sessionFactory">
    <ref bean="hibSessionFactory"/>
    </property>
    </bean>

    <bean id="hibernateDataService" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces">
    <value>DataServices</value>
    </property>
    <property name="interceptorNames">
    <list>
    <value>hibernateInterceptor</value>
    //implements with Factory Bean and initialization bean
    <value>hibernateDataServiceTarget</value>
    </list>
    </property>
    </bean>

    pls help.
    thanks and regards
    ajay

  • #2
    Ajay,

    Is this the full xml file,

    Cos there doesn't seem to be a bean with dataService and hibernateDataServiceTarget.

    Guess you are using a 'false' flag while getting the session,

    Code:
    Session session = SessionFactoryUtils.getSession&#40;getSessionFactory&#40;&#41;, false&#41;;
    in the hibernateDataServiceTarget bean.

    Regards,

    Amit

    Comment


    • #3
      hi amit,
      this is not my full .xml file i declear all the beans.

      yes i am using
      SessionFactoryUtils.getSession(sessionFactory, false);
      function.

      should i have to change false to true in this method.

      pls help
      ajay

      Comment


      • #4
        Hi Ajay,

        Can you copy paste some of your code and the descriptor xml. That provides more info to help.

        The source for sessionfactory utils shows the IllegalStateException being thrown when the allowCreate is somehow set to true.

        Regards,

        Amit

        Comment


        • #5
          amit i am sending some code and my discriptar file.

          code of some file

          public class HibernateTransactionProxyFactoryBean implements FactoryBean, InitializingBean {
          protected static Object proxy = null;
          protected Object target = null;
          protected Class interfaces[] = null;
          protected SessionFactory sessionFactory = null;

          public void setProxyInterfaces(String[] interfaceNames) throws ClassNotFoundException {
          interfaces = AopUtils.toInterfaceArray(interfaceNames);
          }
          public void setTarget(Object o) {
          target = o;
          }
          public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
          }

          public synchronized Object getObject() throws Exception {
          if (null == proxy) {
          ClassLoader loader = target.getClass().getClassLoader();
          InvocationHandler handler = new HibernateTransactionInvocationHandler(target, sessionFactory);
          proxy = Proxy.newProxyInstance(loader, interfaces, handler);
          }
          return proxy;
          }

          //define all the functions.
          }


          public class HibernateTransactionInvocationHandler implements InvocationHandler {

          protected Object target;
          protected SessionFactory sessionFactory;
          public HibernateTransactionInvocationHandler(Object o, SessionFactory factory) {
          target = o;
          sessionFactory = factory;
          }

          public Object invoke(Object proxy, Method method, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, Throwable {
          Session session = SessionFactoryUtils.getSession(sessionFactory, false);
          String methodName = method.getName();

          //some code is also here
          }

          application context .xml file

          <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource"> <property name="driverClassName">
          <value>oracle.jdbc.driver.OracleDriver</value>
          </property>
          <property name="url">
          <value>jdbc:oracle:oci:@//qatestmachine:1521/ORCL</value>
          </property>
          <property name="username">
          <value>scott</value>
          </property>
          <property name="password">
          <value>tiger</value>
          </property>
          </bean>
          <bean id="hibSessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
          <property name="mappingResources">
          <list>
          <value>ex.hbm.xml</value>
          <value>ex1.hbm.xml</value>
          </list>
          </property>
          <property name="hibernateProperties">
          <props>
          <prop key="hibernate.dialect">net.sf.hibernate.dialect.O racleDialect</prop>
          <prop key="hibernate.show_sql">true</prop>
          <prop key="hibernate.cglib.use_reflection_optimizer">tru e</prop>
          </props>
          </property>
          <property name="dataSource">
          <ref bean="dataSource"/>
          </property>
          </bean>

          <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.Hibernate Interceptor">
          <property name="sessionFactory">
          <ref bean="hibSessionFactory"/>
          </property>
          </bean>
          <bean id="hibernateDataServiceTarget" class="HibernateTransactionProxyFactoryBean">
          <property name="target">
          <ref bean="dataServiceImpl"/>
          </property>
          <property name="proxyInterfaces">
          <value>DataServices</value>
          </property>
          <property name="sessionFactory">
          <ref bean="hibSessionFactory"/>
          </property>
          </bean>

          <bean id="hibernateDataService" class="org.springframework.aop.framework.ProxyFact oryBean">
          <property name="proxyInterfaces">
          <value>DataServices</value>
          </property>
          <property name="interceptorNames">
          <list>
          <value>hibernateInterceptor</value>
          <value>hibernateDataServiceTarget</value>
          </list>
          </property>
          </bean>
          //target code of the hibernate
          <bean id="dataServiceImpl" class="HibernateDataService">
          </bean>

          //some more code is there
          </beans>


          thanks and regards

          Comment


          • #6
            these are the exception i am getting with above code.

            org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'hibernateDataService' defined in resource [/WEB-INF/spring-config.xml] of ServletContext: Initialization of bean failed; nested exception is java.lang.IllegalStateException: No Hibernate session bound to thread, and configuration does not allow creation of new one here
            org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:301)
            org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:208)
            org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:204)
            org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:136)
            org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:224)
            org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:284)
            org.springframework.web.context.support.XmlWebAppl icationContext.refresh(XmlWebApplicationContext.ja va:131)
            org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:156)
            org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:97)
            org.springframework.web.context.ContextLoaderServl et.init(ContextLoaderServlet.java:80)
            javax.servlet.GenericServlet.init(GenericServlet.j ava:211)
            org.apache.catalina.startup.HostConfig.deployDirec tories(HostConfig.java:701)
            org.apache.catalina.startup.HostConfig.deployApps( HostConfig.java:432)
            org.apache.catalina.startup.HostConfig.start(HostC onfig.java:983)
            org.apache.catalina.startup.HostConfig.lifecycleEv ent(HostConfig.java:349)
            org.apache.catalina.util.LifecycleSupport.fireLife cycleEvent(LifecycleSupport.java:119)
            org.apache.catalina.startup.Catalina.start(Catalin a.java:556)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
            java.lang.reflect.Method.invoke(Method.java:324)
            org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:287)
            org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:425)


            pls help where is the problem.

            Thanks & Regards

            Ajay Maheshwari

            Comment


            • #7
              Hi Ajay,

              Hope, have worked this out right, you would like to, configure hibernateDataService as a ProxyFactoryBean and apply a hibernate interceptor plus your data service target to it. Correct?

              Can the configuration and code be made a bit simpler?

              Can we use,

              Code:
              org.springframework.transaction.interceptor.TransactionProxyFactoryBean
              
              org.springframework.transaction.interceptor.TransactionInterceptor
              instead of the HibernateTransactionProxyFactoryBean (id:hibernateDataServiceTarget)

              Then the configuration could be,

              Code:
              <bean id="hibernateDataServiceTarget" class="HibernateDataService"/> 
              
              <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
              	<property name="transactionManager">
              		<ref bean="transactionManager"/>
              	</property>
              	<property name="transactionAttributeSource">
              		<value>
              			your definition for service transactions.
              		</value>
              	</property>
              </bean>
              
              <bean id="dataServiceImpl" class="org.springframework.aop.framework.ProxyFactoryBean">
              	<property name="proxyInterfaces">
              		<value>DataServices</value>
              	</property>
              	<property name="interceptorNames">
              		<list>
              			<value>transactionInterceptor</value>
              			<value>hibernateDataServiceTarget</value>
              		</list>
              	</property>
              </bean>
              Does it help?

              Regards,

              Amit[/list]

              Comment


              • #8
                hi,
                i made the changes as you mainssion. but i am getting the same exception.

                org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'hibernateDataService' defined in resource [/WEB-INF/spring-config.xml] of ServletContext: Initialization of bean failed; nested exception is java.lang.IllegalStateException: No Hibernate session bound to thread, and configuration does not allow creation of new one here

                something else we can think.

                Thanks and regards

                ajay

                Comment


                • #9
                  Hi

                  Can some one please reply to my query! spring team ?!

                  Thanks

                  Comment


                  • #10
                    hi amit,
                    i got the solution of my problems the problem was i was using spring 1.1.
                    this was the bug in spring 1.1. that was fixed on spring 1.4.

                    so, i download spring 1.4. and my application work fine.

                    thanks, your given your valueable time.

                    Ajay

                    Comment

                    Working...
                    X