2 Replies Latest reply on Aug 1, 2008 10:20 AM by jaikiran pai

    Exception with JTA/JNDI when moving from Tomcat

    Jeremy Davis Newbie

      I'm moving a war from Tomcat to JBoss, using the Open in View filter, and am having a problem switching to JTA transactions and JNDI lookup of the session factory.

      I'm using the caveatemptor HibernateFilter and HibernateUtil classes and the exception gets thrown when the HibernateUtil is building the session factory.

      My hibernate.cfg.xml is :

      <hibernate-configuration>
      
      <session-factory>
      
       <property name="show_sql">false</property>
       <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
       <property name="hibernate.connection.datasource">java:jdbc/CustomerReportingDB</property>
       <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
       <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
       <property name="hibernate.session_factory_name">java:hibernate/HibernateFactory</property>
       <!-- Mapping files -->
      </session-factory>
      </hibernate-configuration>
      
      


      HibernateUtil :
      public class HibernateUtil
      {
      
       private static Log log = LogFactory.getLog(HibernateUtil.class);
      
       private static Configuration configuration;
      
       private static SessionFactory sessionFactory;
      
       private static final ThreadLocal threadSession = new ThreadLocal();
      
       private static final ThreadLocal threadTransaction = new ThreadLocal();
      
       private static final ThreadLocal threadInterceptor = new ThreadLocal();
      
       // Create the initial SessionFactory from the default configuration files
       static
       {
       try
       {
       configuration = new Configuration();
       //sessionFactory = configuration.configure().buildSessionFactory();
       // We could also let Hibernate bind it to JNDI:
       configuration.configure().buildSessionFactory();
       }
       catch (Throwable ex)
       {
       // We have to catch Throwable, otherwise we will miss
       // NoClassDefFoundError and other subclasses of Error
       log.error("Building SessionFactory failed.", ex);
       throw new ExceptionInInitializerError(ex);
       }
       }
      
       /**
       * Returns the SessionFactory used for this static class.
       *
       * @return SessionFactory
       */
       public static SessionFactory getSessionFactory() {
       // Instead of a static variable, use JNDI:
       SessionFactory sessions = null;
       try {
       Context ctx = new InitialContext();
       String jndiName = "java:hibernate/HibernateFactory";
       sessions = (SessionFactory)ctx.lookup(jndiName);
       } catch (NamingException ex) {
       throw new InfrastructureException(ex);
       }
       return sessions;
      
       //return sessionFactory;
       }
      
       /**
       * Returns the original Hibernate configuration.
       *
       * @return Configuration
       */
       public static Configuration getConfiguration()
       {
       return configuration;
       }
      
       /**
       * Rebuild the SessionFactory with the static Configuration.
       *
       */
       public static void rebuildSessionFactory() throws InfrastructureException
       {
       synchronized (sessionFactory)
       {
       try
       {
       sessionFactory = getConfiguration().buildSessionFactory();
       }
       catch (Exception ex)
       {
       throw new InfrastructureException(ex);
       }
       }
       }
      
       /**
       * Rebuild the SessionFactory with the given Hibernate Configuration.
       *
       * @param cfg
       */
       public static void rebuildSessionFactory(Configuration cfg) throws InfrastructureException
       {
       synchronized (sessionFactory)
       {
       try
       {
       sessionFactory = cfg.buildSessionFactory();
       configuration = cfg;
       }
       catch (Exception ex)
       {
       throw new InfrastructureException(ex);
       }
       }
       }
      
       /**
       * Retrieves the current Session local to the thread.
       * <p/>
       * If no Session is open, opens a new Session for the running thread.
       *
       * @return Session
       */
       public static Session getSession() throws InfrastructureException
       {
       Session s = (Session) threadSession.get();
       try
       {
       if (s == null)
       {
       log.debug("Opening new Session for this thread.");
       if (getInterceptor() != null)
       {
       log.debug("Using interceptor: " + getInterceptor().getClass());
       s = getSessionFactory().openSession(getInterceptor());
       }
       else
       {
       s = getSessionFactory().openSession();
       }
       threadSession.set(s);
       }
       }
       catch (HibernateException ex)
       {
       throw new InfrastructureException(ex);
       }
       return s;
       }
      
       /**
       * Closes the Session local to the thread.
       */
       public static void closeSession() throws InfrastructureException
       {
       try
       {
       Session s = (Session) threadSession.get();
       threadSession.set(null);
       if (s != null && s.isOpen())
       {
       log.debug("Closing Session of this thread.");
       s.close();
       }
       }
       catch (HibernateException ex)
       {
       throw new InfrastructureException(ex);
       }
       }
      
       /**
       * Start a new database transaction.
       */
       public static void beginTransaction() throws InfrastructureException
       {
       Transaction tx = (Transaction) threadTransaction.get();
       try
       {
       if (tx == null)
       {
       log.debug("Starting new database transaction in this thread.");
       tx = getSession().beginTransaction();
       threadTransaction.set(tx);
       }
       }
       catch (HibernateException ex)
       {
       throw new InfrastructureException(ex);
       }
       }
      
       /**
       * Commit the database transaction.
       */
       public static void commitTransaction() throws InfrastructureException
       {
       Transaction tx = (Transaction) threadTransaction.get();
       try
       {
       if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
       {
       log.debug("Committing database transaction of this thread.");
       tx.commit();
       }
       threadTransaction.set(null);
       }
       catch (HibernateException ex)
       {
       rollbackTransaction();
       throw new InfrastructureException(ex);
       }
       }
      
       /**
       * Commit the database transaction.
       */
       public static void rollbackTransaction() throws InfrastructureException
       {
       Transaction tx = (Transaction) threadTransaction.get();
       try
       {
       threadTransaction.set(null);
       if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
       {
       log.debug("Tyring to rollback database transaction of this thread.");
       tx.rollback();
       }
       }
       catch (HibernateException ex)
       {
       throw new InfrastructureException(ex);
       }
       finally
       {
       closeSession();
       }
       }
      
       /**
       * Reconnects a Hibernate Session to the current Thread.
       *
       * @param session The Hibernate Session to be reconnected.
       */
       public static void reconnect(Session session) throws InfrastructureException
       {
       try
       {
       session.reconnect();
       threadSession.set(session);
       }
       catch (HibernateException ex)
       {
       throw new InfrastructureException(ex);
       }
       }
      
       /**
       * Disconnect and return Session from current Thread.
       *
       * @return Session the disconnected Session
       */
       public static Session disconnectSession() throws InfrastructureException
       {
      
       Session session = getSession();
       try
       {
       threadSession.set(null);
       if (session.isConnected() && session.isOpen())
       session.disconnect();
       }
       catch (HibernateException ex)
       {
       throw new InfrastructureException(ex);
       }
       return session;
       }
      
       /**
       * Register a Hibernate interceptor with the current thread.
       * <p>
       * Every Session opened is opened with this interceptor after
       * registration. Has no effect if the current Session of the
       * thread is already open, effective on next close()/getSession().
       */
       public static void registerInterceptor(Interceptor interceptor)
       {
       threadInterceptor.set(interceptor);
       }
      
       private static Interceptor getInterceptor()
       {
       Interceptor interceptor = (Interceptor) threadInterceptor.get();
       return interceptor;
       }
      
      }
      
      



      JNDI tree is :
       +- jaas (class: javax.naming.Context)
       | +- HsqlDbRealm (class: org.jboss.security.plugins.SecurityDomainContext)
       | +- jbossmq (class: org.jboss.security.plugins.SecurityDomainContext)
       | +- JmsXARealm (class: org.jboss.security.plugins.SecurityDomainContext)
       +- TransactionPropagationContextImporter (class: com.arjuna.ats.internal.jbossatx.jta.PropagationContextManager)
       +- comp.ejb3 (class: javax.naming.Context)
       | NonContext: null
       +- JmsXA (class: org.jboss.resource.adapter.jms.JmsConnectionFactoryImpl)
       +- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
       +- StdJMSPool (class: org.jboss.jms.asf.StdServerSessionPoolFactory)
       +- TransactionManager (class: com.arjuna.ats.jbossatx.jta.TransactionManagerDelegate)
       +- TransactionPropagationContextExporter (class: com.arjuna.ats.internal.jbossatx.jta.PropagationContextManager)
       +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
       +- jdbc (class: org.jnp.interfaces.NamingContext)
       | +- CustomerReportingDB (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
       +- DefaultJMSProvider (class: org.jboss.jms.jndi.JNDIProviderAdapter)
       +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
       +- Mail (class: javax.mail.Session)
       +- comp.original (class: javax.namingMain.Context)
       +- hibernate (class: org.jnp.interfaces.NamingContext)
       | +- HibernateFactory (class: org.hibernate.impl.SessionFactoryImpl)
       +- timedCacheFactory (class: javax.naming.Context)
      Failed to lookup: timedCacheFactory, errmsg=org.jboss.util.TimedCachePolicy cannot be cast to javax.naming.NamingEnumeration
       +- SecurityProxyFactory (class: org.jboss.security.SubjectSecurityProxyFactory)
       +- comp (class: javax.naming.Context)
      
      


      Any help will be much appreciated.

        • 1. Re: Exception with JTA/JNDI when moving from Tomcat
          Jeremy Davis Newbie

          Stack trace is :

          java.lang.ClassCastException: com.arjuna.ats.jbossatx.jta.TransactionManagerDelegate cannot be cast to javax.transaction.TransactionManager
           at org.hibernate.transaction.JNDITransactionManagerLookup.getTransactionManager(JNDITransactionManagerLookup.java:23)
           at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:325)
           at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
           at com.rollins.crp.manager.HibernateUtil.<clinit>(HibernateUtil.java:45)
           at com.rollins.crp.utils.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:40)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at com.rollins.crp.security.SecurityFilter.doFilter(SecurityFilter.java:88)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
           at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
           at java.lang.Thread.run(Thread.java:619)
          08:35:26,381 ERROR [[action]] Servlet.service() for servlet action threw exception
          java.lang.NoClassDefFoundError: Could not initialize class com.rollins.crp.manager.HibernateUtil
           at com.rollins.crp.utils.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:45)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at com.rollins.crp.security.SecurityFilter.doFilter(SecurityFilter.java:88)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
           at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
           at java.lang.Thread.run(Thread.java:619)
          
          


          • 2. Re: Exception with JTA/JNDI when moving from Tomcat
            jaikiran pai Master

            Looks like a classloading issue. Are you packaging JTA related jar files (jta.jar?) in your WAR? If yes, remove those from the application's packaging. JBoss already ships with these jar files. For more classloader related details in JBoss, see these:

            http://wiki.jboss.org/wiki/JBossClassLoadingUseCases

            http://wiki.jboss.org/wiki/ClassLoadingConfiguration