Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: no session with hibernate4

  1. #1
    Join Date
    Jan 2012
    Location
    Switzerland
    Posts
    1

    Default 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. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    Hello

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

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Jan 2012
    Posts
    4

    Default 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" />

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    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
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  5. #5
    Join Date
    Jan 2012
    Posts
    4

    Default

    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

  6. #6
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    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
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  7. #7
    Join Date
    Jan 2012
    Posts
    4

    Default

    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)

  8. #8
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    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
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  9. #9
    Join Date
    Jun 2011
    Posts
    8

    Default

    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 at 03:44 PM.

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •