1 Reply Latest reply on Oct 25, 2006 3:56 PM by bmcgovern

    Hibernate -  Unable to locate current JTA transaction

    bmcgovern

      My problem: I am trying to get transactions working with hibernate 3 and jboss 4.0.4.

      I deployed my har file and it worked. I can get a currentSession() from JNDI, but I cannot get a transaction to work. It throughs Unable to locate current JTA transaction exception.

      I'm not using EJBs, rather DAO. What am i doing wrong?


      datasource-ds.xml
      ---------------------------------------------------------------

      <datasources>
       <local-tx-datasource>
       <jndi-name>TeenFitData</jndi-name>
       <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
       <connection-url>jdbc:jtds:sqlserver://192.168.100.102:1433;databaseName=Teenfit_Hibernate;tds=8.0;lastupdatecount=true</connection-url>
       <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
       <user-name>user</user-name>
       <password>pass</password>
       </local-tx-datasource>
       </datasources>
      


      jboss-service.xml
      ---------------------------------------------------------------
      <server>
       <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.adminguide:name=ExampleSessionFactory">
       <attribute name="DatasourceName">java:/TeenFitData</attribute>
       <attribute name="Dialect"> org.hibernate.dialect.SQLServerDialect</attribute>
       <attribute name="SessionFactoryName">java:/teenfit/TeenFitDataSessionFactory </attribute>
       <attribute name="CacheProviderClass"> org.hibernate.cache.HashtableCacheProvider</attribute>
       <attribute name="Hbm2ddlAuto">create-drop</attribute>
       <attribute name="ShowSqlEnabled">true</attribute>
       </mbean>
       </server>


      Data code
      -----------------------------------------------
      InitialContext ctx = new InitialContext();
       SessionFactory factory = (SessionFactory) ctx.lookup("java:/teenfit/TeenFitDataSessionFactory");
       //Session hsession = TeenFitHibernateUtil.getSessionFactory().getCurrentSession();
       Session hsession = factory.getCurrentSession();
       Transaction zTransaction = null;
       //zTransaction = (Transaction)new InitialContext().lookup("java:comp/UserTransaction");
      
       zTransaction = hsession.beginTransaction();
       System.out.println("**TRANSACTION FOUNT**" + hsession.getTransaction().toString());
      
       hsession.saveOrUpdate(HibernateTFUser);
      
      
       zTransaction.commit();


        • 1. Re: Hibernate -  Unable to locate current JTA transaction
          bmcgovern

          I seemed to get around it with the following code.

          But does anyone know if im on the right track here? Is openSession() worse than getCurrentSession()?

          Session hsession = null;
           try {
          
           InitialContext ctx = new InitialContext();
          
           //UserTransaction tx = (UserTransaction)ctx.lookup("java:comp/UserTransaction");
           //tx.begin();
           SessionFactory factory = (SessionFactory) ctx.lookup("java:/teenfit/TeenFitDataSessionFactory");
           hsession = factory.openSession();
          
           // Session hsession =
           // TeenFitHibernateUtil.getSessionFactory().getCurrentSession();
           // zTransaction = (Transaction)new
           // InitialContext().lookup("java:comp/UserTransaction");
          
           //Transaction t = hsession.getTransaction();
          // System.out.println("**TRANSACTION FOUND**:: tx = " + tx.toString());
           System.out.println("**TRANSACTION FOUND**:: session = " + hsession.getTransaction().toString());
           hsession.getTransaction().begin();
          
           hsession.saveOrUpdate(HibernateTFUser);
           hsession.saveOrUpdate(HibernateTFUser2);
          
           hsession.getTransaction().commit();
          
          
           //tx.commit();
          
           }catch (Exception zEX){
           System.out.println("Eception:" + zEX.toString());
           hsession.getTransaction().rollback();
           }