Announcement Announcement Module
Collapse
No announcement yet.
No CurrentSessionContext configured!, No session currently bound to execution context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No CurrentSessionContext configured!, No session currently bound to execution context

    Using spring 3.1.0.RC1, Hibernate 4.0.0.CR5 and JPA. I have an integration test that was producing the following error:
    Code:
    No CurrentSessionContext configured!
    So I discovered a hibernate property called hibernate.current_session_context_class.

    I adjusted the configuration of the LocalContainerEntityManagerFactoryBean to include:
    Code:
    <prop key="hibernate.current_session_context_class">managed</prop>
    I re-ran the integration test and received:
    Code:
    No session currently bound to execution context
    I looked into the source code for org.springframework.orm.hibernate4.LocalSessionFac toryBuilder and noticed that it was setting the property hibernate.current_session_context_class to SpringSessionContext.

    So I modified the hibernate.current_session_context_class to:
    Code:
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    and that seems to have resolved the problem.

    Q: have others experienced this? is it exclusive to spring 3.1.0.RC1 & hibernate 4.0.0.CR5?

    Q: is there a spring/hibernate/jpa tutorial/article that uses this hibernate.current_session_context_class property?

    Thanks in advance.

  • #2
    Q: have others experienced this? is it exclusive to spring 3.1.0.RC1 & hibernate 4.0.0.CR5?
    Not in this combination but yes if you would have used the search you would have found lots of threads.

    Q: is there a spring/hibernate/jpa tutorial/article that uses this hibernate.current_session_context_class property?
    No and you should NEVER mess around with that property when combining Hibernate with Spring because, as you already noticed, that destroys proper session and transaction management. The only time you want to set this property is if you use Spring and Hibernate in a JTA environment, else don't use it.

    Comment


    • #3
      Marten:
      Not in this combination but yes if you would have used the search you would have found lots of threads.
      Actually I had searched the spring forums for this but did not see any (vs. 'lots of threads'). I just did another spring forum search on 'CurrentSessionContext' and the only thing that came up was this post. Perhaps you can share the search you did to show 'lots of threads', I would appreciate the lesson.

      No and you should NEVER mess around with that property when combining Hibernate with Spring because, as you already noticed, that destroys proper session and transaction management. The only time you want to set this property is if you use Spring and Hibernate in a JTA environment, else don't use it.
      So, let me get this straight Marten. The hibernate.current_session_context_class should never be used when combining Hibernate with Spring? Really? In the post it indicates that without this property there is a No CurrentSessionContext configured! error. Did not get proper transaction behavior until that property was added. Is this a problem with the org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean in Spring 3.1.0.RC1?

      Marten, help me to understand the issue at hand.

      Comment


      • #4
        I answer this same question about at least 2 times a week, so trust me there are more threads on this. If I do a search on 'current_session_context_class' I get about 5 pages with results...

        What I wonder is how do you get your EntityManagerFactory up/configured, because the difference is probably in there. Make sure you are setting the correct JpaDialect etc. I really never have set this attribute before combining Spring and Hibernate (not even when using JPA 1.x or 2.x).

        Comment


        • #5
          Zip file with maven project for Marten

          Marten,

          I have uploaded a zip file (for-marten.zip). It is a maven project which demonstrates the issues we have been discussing in this thread.

          I have included a README file at the top level of the project that provides more detail of the files in the project.

          --- BELOW IS FROM THE README (albeit with prettier formatting) ---

          Hello Marten,

          Thank you for looking at this issue.

          This maven project includes a very simple domain model (FootballPlayer), dao (FootballPlayerDao) and service (SportsService).
          The intention is to reproduce the situation described in the Spring Forum thread (http://forum.springsource.org/showth...cution-context)
          entitled "No CurrentSessionContext configured!, No session currently bound to execution context".

          NOTE: this project uses spring 3.1.0.RC1 and hibernate 4.0.0.CR5 as mentioned in the forum post.
          NOTE: the sessionFactory (TEST2 & TEST3) is derived from an EntityManagerFactory.

          This project contains 3 tests (described below):
          TEST1) TraditionalLocalSessionFactoryBeanTest
          TEST2) EntityManagerFactoryDerivedSessionFactoryTest
          TEST3) EntityManagerFactoryDerivedSessionFactoryWithoutUs ingHibernate_current_session_context_classProperty Test

          It is TEST3 that fails without the property "hibernate.current_session_context_class" set.

          ----------------------------------------------------------------------------------------

          TEST1:TraditionalLocalSessionFactoryBeanTest
          - this is a safe test. it is based on (as the name implies) a traditional LocalSessionFactoryBean usage.

          TEST2:EntityManagerFactoryDerivedSessionFactoryTes t
          - this test passes
          - this test uses a configuration file (EntityManagerFactory-derived-SessionFactory-test-spring-main-config.xml)
          that includes the setting of the "hibernate.current_session_context_class" to "org.springframework.orm.hibernate4.SpringSessionC ontext".

          TEST3:EntityManagerFactoryDerivedSessionFactoryWit houtUsingHibernate_current_session_context_classPr opertyTest
          - this test FAILs
          - this test does not include the "hibernate.current_session_context_class" property in the EntityManagerFactory configuration.
          - when run this test fails with: "No CurrentSessionContext configured!" as described in the forum post.
          Code:
          org.hibernate.HibernateException: No CurrentSessionContext configured!
          	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:872)
          	at com.javapda.formarten.dao.FootballPlayerHibernateDao.getCurrentSession(FootballPlayerHibernateDao.java:24)
          	at com.javapda.formarten.dao.FootballPlayerHibernateDao.save(FootballPlayerHibernateDao.java:41)
          	at com.javapda.formarten.dao.SportsServiceImpl.saveFootballPlayer(SportsServiceImpl.java:29)
          	at com.javapda.formarten.EntityManagerFactoryDerivedSessionFactoryWithoutUsingHibernate_current_session_context_classPropertyTest.testing(EntityManagerFactoryDerivedSessionFactoryWithoutUsingHibernate_current_session_context_classPropertyTest.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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
          	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
          	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
          	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
          	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
          	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
          	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
          	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
          	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
          	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
          	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
          	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
          	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
          	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
          	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
          	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
          	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
          	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
          	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
          	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
          	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
          	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
          	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
          	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
          	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
          - if you uncomment the line "<prop key="hibernate.current_session_context_class">mana ged</prop>" and re-run the TEST3 test you get: "No session currently bound to execution context"
          Code:
          org.hibernate.HibernateException: No session currently bound to execution context
          	at org.hibernate.context.internal.ManagedSessionContext.currentSession(ManagedSessionContext.java:72)
          	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:874)
          	at com.javapda.formarten.dao.FootballPlayerHibernateDao.getCurrentSession(FootballPlayerHibernateDao.java:24)
          	at com.javapda.formarten.dao.FootballPlayerHibernateDao.save(FootballPlayerHibernateDao.java:41)
          	at com.javapda.formarten.dao.SportsServiceImpl.saveFootballPlayer(SportsServiceImpl.java:29)
          	at com.javapda.formarten.EntityManagerFactoryDerivedSessionFactoryWithoutUsingHibernate_current_session_context_classPropertyTest.testing(EntityManagerFactoryDerivedSessionFactoryWithoutUsingHibernate_current_session_context_classPropertyTest.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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
          	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
          	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
          	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
          	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
          	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
          	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
          	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
          	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
          	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
          	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
          	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
          	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
          	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
          	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
          	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
          	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
          	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
          	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
          	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
          	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
          	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
          	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
          	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
          	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

          Comment


          • #6
            The problem is you are doing strange things. You are creating an entity manager and then start using a session factory.... Don't... If I change your project to simply use an EntityManager instead of a session factory (obtained through an EntityManagerFactory) it works like a charm.

            Comment


            • #7
              Thank you and sessionFactory derived from an EntityManager

              Marten,

              First of all, thank you for taking the time to look at this.

              The project under consideration has traditionally been a non-JPA spring/hibernate project. We are converting to JPA.

              What is different about the session factory obtained via the EntityManager?

              Why do things begin to work once the "hibernate.current_session_context_class" is set to the "org.springframework.orm.hibernate4.SpringSessionC ontext"?

              Thanks again.

              Comment


              • #8
                How transactions are created/managed differs in JPA and plain Hibernate. Whilst the EntityManager wraps around a session it works differently (internally) with the SessionFactory to obtain a session, which (apparently) doesn't need a current session context. If you use plain hibernate it will need this (I didn't delve into the gory depths of hibernate to check why and how this is just by observation). So if you really want to use the session factory directly simply add the property, however I would change the get current session method to simply return the wrapped session from the entity manager.

                Code:
                    @PersistenceContext
                    private EntityManager entityManager;
                
                    @Deprecated
                    protected Session getCurrentSession()  {
                    	return (Session) entityManager.getDelegate();
                    }
                That way you don't need direct access to the session factory anymore (and are a step closer to migrate to JPA).

                Comment


                • #9
                  Great, it works....

                  Fantastic! I tried your implementation and it works nicely.

                  If you ever get to Tucson, AZ, stop by and I'll buy you a refreshment!

                  Thanks again.

                  Comment

                  Working...
                  X