6 Replies Latest reply on Jun 21, 2005 6:37 AM by rjkimme

    New to CMT - hibernate can't find transaction

    jrayburn

      Hi,

      I'm working on integrating Hibernate 3 with JBOSS. I'm new to CMT and JBOSS, but I've been using Hibernate for a while.

      The problem I'm having is that a call to HibernateContext (which I know calls SessionFactory.getCurrentSession() under the covers) is throwing a HibernateException "Unable to locate current JTA transaction" from within an Session Bean.

      Here is the EJB descriptor snippet:

       <session >
       <description>[CDATA[Stateless SessionBean]]</description>
       <display-name>ManageOrderEJB</display-name>
       <ejb-name>SessionBeans/ManageOrderEJB</ejb-name>
       <home>com.company.ManageOrderEJBRemoteHome</home>
       <remote>com.company.ManageOrderEJBRemote</remote>
       <ejb-class>com.company.ManageOrderEJB</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Container</transaction-type>
       </session>
      


      The code is:

       /**
       * @ejb.interface-method
       * view-type="remote"
       */
       public Order getOrder String id )
      throws RemoteException {
       Session session = getJTASession();
       ...
       }
       public static Session getJTASession() {
       return HibernateContext.getSession("java:/hibernate/SessionFactory");
       }
      


      Ok - here is where my complete ignorance comes into play - why is there no JTA transaction available?

      Thanks for helping me out with my first JBOSS/CMT issue!

      - John

        • 1. Re: New to CMT - hibernate can't find transaction
          jrayburn

          Figured it out - I didn't understand the semantics of "Supports" - switching it to "Required" fixes the issue.

          Thanks for the patience to anyone who reads this and thinks RTM :)

          John

          • 2. Re: New to CMT - hibernate can't find transaction
            rjkimme

            I am obviously slower than you so could you please explain what you fixed to get this to work? I am using jboss 4 and hibernate 3. Here is what I try:

            sessionFactory = new Configuration().configure().buildSessionFactory();
            s = sessionFactory.getCurrentSession();

            I get the error thtat Unable to locate current JTA transaction at ....getCurrentSession

            I see that you are calling getSession(with jndi) instead of getCurrentSession(), why?

            • 3. Re: New to CMT - hibernate can't find transaction
              jrayburn

              The call that I am making is the same as getCurrentSession(); I had just written this code before I was aware of that method. I have since switched to calling getCurrentSession().

              To fix this code, I changed the transactionality of the EJB from Supports to type="Required" - supports indicates that if a transaction already exists, I will take part in it, required creates a transaction if one does not already exist, but if one exists, it simply joins it.

              If you use getCurrentSession, you must require a transaction exists.

              I hope that helps!

              - John

              • 4. Re: New to CMT - hibernate can't find transaction
                rjkimme

                I was trying to get around using EJB's but I may be forced to. I want to use just pojo's but I can't seem to get things up and running fully. Have you always used EJB? From all that I have read it seems that I should be able to use just pojo and dao pattern but the question is ??????

                • 5. Re: New to CMT - hibernate can't find transaction
                  jrayburn

                  Yes, you can use POJO and dao pattern, which I've done most of the time with my Hibernate work. However, if you do that, then consider using springs "HibernateDaoSupport" instead of getCurrentSession for session management. You cannot use getCurrentSession unless you or the container has set up a JTA Transaction.

                  I hope that helps!

                  - John

                  • 6. Re: New to CMT - hibernate can't find transaction
                    rjkimme

                    Thanks for the response and I started looking at Spring but have not downloaded it yet. We do have JTA configured in our hibernate-service.xml and Jboss does support it so I believe I can go that route and not add another layer (Spring)? Our debug says we are getting a JTA transaction but we changed our code to use session.get() and openSession. getCurrentSession kept throwing up an error that no transaction existed. It looks as if all is going well but our query does not return any rows. It should return 1.

                    Do you by chance have a quick configuration for installing and setting up spring. I am currently using hibernate 3, jboss 4, struts and sybase 12.5? I have a hibernate.cfg.xml and a hiberante-service.xml. If it is a 5 minutes thing then that might be a viable solution. What does Spring do exactly?

                    In the event you might be able to email me some samples: jacky@itmc-wo.com
                    Thank you