Announcement Announcement Module
Collapse
No announcement yet.
no session with hibernate4 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • no session with hibernate4

    I tried to migrate an application using now hibernate4 instead of hibernate3. I have now the problem, that i never get a session when i call the currentSession() method on the session factory.

    When i compare the SpringSessionContext from hibernate3 package with the one from hibernate4 i see that the hibernate4 version never create a new session. The hibernate3 version does this in the method doGetSession of the SessionFactoryUtils class if actually no session can be found and this newly created session will then be registered in the TransactionSynchronizationManager. It seems that this will not be done with the hibernate4 implementations.

    Was anybody successful using the hibernate4 implementations or is this a bug in Spring 3.1.0?

  • #2
    Hello

    Have you read the Spring Core documentation?. Perhaps some extra configuration is mandatory and you didnt included yet

    Comment


    • #3
      I'm running into the same issue

      I am having the same issue... here's my dao config:
      I am using the "simple" config demonstrated in the docs for Spring 3.1 Transactions @ http://static.springsource.org/sprin...-prog-template


      Code:
       <!-- Enable annotation style of managing transactions -->
      	<tx:annotation-driven transaction-manager="transactionManager" />	
         
      	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      		<property name="dataSource" ref="dataSource" />
      		<property name="hibernateProperties">
      			<value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect</value>
      		</property>
      	</bean>
      	
      	<!-- Declare a datasource that has pooling capabilities-->	 
      	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      				destroy-method="close"
      				p:driverClass="${app.jdbc.driverClassName}"
      				p:jdbcUrl="${app.jdbc.url}"
      				p:user="${app.jdbc.username}"
      				p:password="${app.jdbc.password}"
      				p:acquireIncrement="5"
      				p:idleConnectionTestPeriod="60"
      				p:maxPoolSize="100"
      				p:maxStatements="50"
      				p:minPoolSize="10" />
      
      	<!-- Declare a transaction manager-->
      	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
             			p:sessionFactory-ref="sessionFactory" />

      Comment


      • #4
        that i never get a session when i call the currentSession() method on the session factory.
        Appear some error stack trace? If yes, post it (use code tags)

        And How you know that this no work.

        I need more details

        Comment


        • #5
          If the OP is having the same issue as me, the exception probably looks like this:

          org.hibernate.HibernateException: No Session found for current thread] with root cause org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionCo ntext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImpl.getCurre ntSession(SessionFactoryImpl.java:881)

          I have also posted this issue on Stack Overflow

          Comment


          • #6
            Hello

            Use code tags the next time

            Post the complete error stack trace, perhaps more details is there.

            I assume from your Hibernate 3 configuration you only have edited this line org.springframework.orm.hibernate4.LocalSessionFac toryBean right?

            Be sure about have read the Spring Core documentation, perhaps an extra configuration could be mandatory.

            If all is right I suggest create a JIRA issue here Spring's Issue Tracker

            Comment


            • #7
              Yes, the only change I had to make was to update to Hibernate4.LocalSessionFactory. I have read the documentation and am using the XML configuration straight out of the documentation. The doc literally says the config is "simple".

              Code:
              org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread
              	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
              	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
              	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
              	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
              
              root cause
              
              org.hibernate.HibernateException: No Session found for current thread
              	org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
              	org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:881)
              	com.matt2414.lystra.orm.UserDaoImpl.getAllUsers(UserDaoImpl.java:22)
              	com.matt2414.lystra.controllers.HomeController.home(HomeController.java:30)
              	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              	java.lang.reflect.Method.invoke(Method.java:597)
              	org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
              	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
              	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
              	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
              	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
              	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
              	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
              	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
              	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
              	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
              	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
              	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

              Comment


              • #8
                Could you create a Test Class testing only the DAO layer?

                I want to see if you get the same error stack trace

                I remember when I created a MVC application I received this error due a wrong configuration about the MVC application, but work perfectly for the test classes.

                The problem could be about that for the WebApplicationContext could be not working with the rest of your beans defined or related with the Transactional and Persistence context

                Let me know your advance

                Comment


                • #9
                  Someone suggested that you can use the EntityManager and then get a session from there:

                  Code:
                  @PersistenceContext
                  EntityManager entityManager;
                  
                  void someMethod() {
                      Session session = entityManager.unwrap(Session.class);
                      // ...
                  }
                  However, there was little more info and I am not sure if this is the preferred solution?
                  Last edited by matsev; Jan 18th, 2012, 04:44 PM.

                  Comment


                  • #10
                    Judging from your stack trace you don't have transactions correctly applied. The stack trace doesn't show a TransactionInterceptor basically meaning you don't have a proper tx setup.

                    This can be either due to that you are constructing a dao yourself or have duplicate component-scanning resulting in duplicate instances of your dao (one proxied one not proxied).

                    Comment


                    • #11
                      Having the same issue. Did you ever get a resolution to this ?

                      Comment

                      Working...
                      X