1 Reply Latest reply on Apr 4, 2005 7:19 PM by minmay

    [Hibernate] Session cannot be generated outside of transacti

    minmay

      subject: [Hibernate] Session cannot be generated outside of transaction scope

      premise:

      I have Stateless session bean that utilizes container managed transactions. This particular method requires a transaction. I am using Hibernate for dataaccess.

      my code is as follows:

      
      private String getSessionFactoryContext() throws NamingException
       {
       String value = null;
       Context ctx = new InitialContext();
       Context environment = (Context)ctx.lookup("java:comp/env");
       value = (String) environment.lookup("sessionFactory");
       return value;
       }
      
      public Session getCurrentSession(String sessionFactory)
       {
       return HibernateContext.getSession(sessionFactory);
       }
      
      public ValueObject myEJBBusinessMethod()
      {
       Session session = getCurrentSession(getSessionFactoryContext());
      ...
      
      


      When I call the myEJBBusinessMethod I get the error, the first
      line throws an exception with the following detailed message:

      "Session cannot be generated outside of transaction scope"

      I am simply trying to use container managed transactions
      within a remote stateless session bean with my session bean
      using hibernate, but I get that error, does
      anybody have a code example about the proper configuration.

      I already spent about 5 hours trolling for the answer.


        • 1. Re: [Hibernate] Session cannot be generated outside of trans
          minmay

          Problem solved.
          I had to make the transaction required.
          Previously I just made the transaction optional.
          Noticed JBoss Hibernate caveat requires a transaction
          before JBoss Hibernate attaches a transaction.

          Does this mean that JBoss Hibernate always requires a transaction?
          What if you're just doing reads, doesn't this cause performance overhead?