5 Replies Latest reply on Oct 10, 2008 10:40 AM by Sefai Tando?an

    Reference cannot be cast to SessionFactory (Quartz)

    Sefai Tando?an Newbie

      I have two different applications that uses Quartz and I get two different exceptions...


      First one:



      2008-04-27 00:00:00,017 ERROR [org.quartz.core.JobRunShell] Job DEFAULT.2f34a517:119860671f7:-7ffd threw an unhandled Exception: 
      java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.hibernate.SessionFactory
              at org.jboss.seam.persistence.ManagedHibernateSession.getSessionFactoryFromJndiOrValueBinding(ManagedHibernateSession.java:234)
              at org.jboss.seam.persistence.ManagedHibernateSession.initSession(ManagedHibernateSession.java:81)
              at org.jboss.seam.persistence.ManagedHibernateSession.getSession(ManagedHibernateSession.java:112)
              at sun.reflect.GeneratedMethodAccessor824.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
              at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
              at org.jboss.seam.Component.callComponentMethod(Component.java:2082)
              at org.jboss.seam.Component.unwrap(Component.java:2108)
              at org.jboss.seam.Component.getInstance(Component.java:1887)
              at org.jboss.seam.Component.getInstance(Component.java:1840)
              at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55)
              at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50)
              at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:166)
              at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:53)
              at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
              at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
              at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
              at org.jboss.seam.core.Expressions$1.getValue(Expressions.java:111)
              at org.jboss.seam.transaction.HibernateTransaction.initSession(HibernateTransaction.java:69)
              at org.jboss.seam.transaction.HibernateTransaction.begin(HibernateTransaction.java:80)
              at org.jboss.seam.util.Work.workInTransaction(Work.java:35)
              at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.async.AsynchronousInterceptor.aroundInvoke(AsynchronousInterceptor.java:50)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
              at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
              at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
              at org.anadolu.mifare.schedule.PersonnelTypeProcessor_$$_javassist_2.schedulePersonnelTypeSynchronization(PersonnelTypeProcessor_$$_javassist_2.java)
              at sun.reflect.GeneratedMethodAccessor821.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
              at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
              at org.jboss.seam.async.AsynchronousInvocation.call(AsynchronousInvocation.java:52)
              at org.jboss.seam.async.Asynchronous.executeInContexts(Asynchronous.java:76)
              at org.jboss.seam.async.Asynchronous.execute(Asynchronous.java:45)
              at org.jboss.seam.async.QuartzDispatcher$QuartzJob.execute(QuartzDispatcher.java:240)
              at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
              at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
      2008-04-27 00:00:00,197 ERROR [org.quartz.core.ErrorLogger] Job (DEFAULT.2f34a517:119860671f7:-7ffd threw an exception.
      org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.hibernate.SessionFactory]
              at org.quartz.core.JobRunShell.run(JobRunShell.java:214)
              at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
      * Nested Exception (Underlying Cause) ---------------
      java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.hibernate.SessionFactory
              at org.jboss.seam.persistence.ManagedHibernateSession.getSessionFactoryFromJndiOrValueBinding(ManagedHibernateSession.java:234)
              at org.jboss.seam.persistence.ManagedHibernateSession.initSession(ManagedHibernateSession.java:81)
              at org.jboss.seam.persistence.ManagedHibernateSession.getSession(ManagedHibernateSession.java:112)
              at sun.reflect.GeneratedMethodAccessor824.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
              at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
              at org.jboss.seam.Component.callComponentMethod(Component.java:2082)
              at org.jboss.seam.Component.unwrap(Component.java:2108)
              at org.jboss.seam.Component.getInstance(Component.java:1887)
              at org.jboss.seam.Component.getInstance(Component.java:1840)
              at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55)
              at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50)
              at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:166)
              at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:53)
              at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
              at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
              at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
              at org.jboss.seam.core.Expressions$1.getValue(Expressions.java:111)
              at org.jboss.seam.transaction.HibernateTransaction.initSession(HibernateTransaction.java:69)
              at org.jboss.seam.transaction.HibernateTransaction.begin(HibernateTransaction.java:80)
              at org.jboss.seam.util.Work.workInTransaction(Work.java:35)
              at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.async.AsynchronousInterceptor.aroundInvoke(AsynchronousInterceptor.java:50)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
              at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
              at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
              at org.anadolu.mifare.schedule.PersonnelTypeProcessor_$$_javassist_2.schedulePersonnelTypeSynchronization(PersonnelTypeProcessor_$$_javassist_2.java)
              at sun.reflect.GeneratedMethodAccessor821.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
              at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
              at org.jboss.seam.async.AsynchronousInvocation.call(AsynchronousInvocation.java:52)
              at org.jboss.seam.async.Asynchronous.executeInContexts(Asynchronous.java:76)
              at org.jboss.seam.async.Asynchronous.execute(Asynchronous.java:45)
              at org.jboss.seam.async.QuartzDispatcher$QuartzJob.execute(QuartzDispatcher.java:240)
              at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
              at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)



      This is the ScheduleManager class



      package org.anadolu.mifare.schedule;
      
      import java.util.Date;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Create;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.Startup;
      
      @Name("scheduleManager")
      @Startup(depends = ("cardDAO"))
      @Scope(ScopeType.APPLICATION)
      public class ScheduleManager {
      
              @In
              private PersonnelTypeProcessor personnelTypeProcessor;
      
              @Create
              public void init() {
                      schedulePersonnelTypeSynchronization();
              }
      
              private void schedulePersonnelTypeSynchronization() {
                      this.personnelTypeProcessor.schedulePersonnelTypeSynchronization(new Date(),
                                      "00 00,30 * * * ?");                             
              }
      
      }



      This is the Job class



      package org.anadolu.mifare.schedule;
      
      import java.util.Date;
      import java.util.List;
      
      import org.anadolu.mifare.dao.CardDAO;
      import org.anadolu.mifare.domain.RefectoryCardType;
      import org.anadolu.mifare.domain.ServiceType;
      import org.anadolu.personel.HataKodu;
      import org.anadolu.personel.PersonelTur;
      import org.anadolu.personel.PersonelTurBilgi;
      import org.anadolu.personel.WsYemekService;
      import org.anadolu.personel.WsYemekServiceSoap;
      import org.jboss.seam.annotations.AutoCreate;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Transactional;
      import org.jboss.seam.annotations.async.Asynchronous;
      import org.jboss.seam.annotations.async.Expiration;
      import org.jboss.seam.annotations.async.IntervalCron;
      import org.jboss.seam.async.QuartzTriggerHandle;
      import org.jboss.seam.log.Log;
      
      @Name("personnelTypeProcessor")
      @AutoCreate
      public class PersonnelTypeProcessor {
      
              @Logger
              private Log log;
      
              @In
              private CardDAO cardDAO;
      
              @Asynchronous
              @Transactional
              public QuartzTriggerHandle schedulePersonnelTypeSynchronization(@Expiration
              Date when, @IntervalCron
              String cron) {
      
                      // call some web services & update database
      
                      return null;
              }
      }



      and component.xml



      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/components"
              xmlns:core="http://jboss.com/products/seam/core"
              xmlns:persistence="http://jboss.com/products/seam/persistence"
              xmlns:security="http://jboss.com/products/seam/security"
              xmlns:transaction="http://jboss.com/products/seam/transaction"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:async="http://jboss.com/products/seam/async">
      
              <core:init debug="false" />
      
              <core:manager conversation-timeout="120000"
                      concurrent-request-timeout="500" conversation-id-parameter="cid" />
      
              <persistence:hibernate-session-factory
                      name="hibernateSessionFactory" />
      
              <persistence:managed-hibernate-session name="kartDatabase"
                      auto-create="true"
                      session-factory-jndi-name="java:/kartDatabase" />
      
              <security:identity
                      authenticate-method="#{authenticator.authenticate}" />
      
              <transaction:hibernate-transaction session="#{kartDatabase}" />
              
              <!-- Install the QuartzDispatcher -->
              <async:quartz-dispatcher />
      
      </components>



      The other application is designed with same logic and it gives org.hibernate.exception.JDBCConnectionException: Cannot open connection


      Can someone help me,thanks in advance...




        • 2. Re: Reference cannot be cast to SessionFactory (Quartz)
          Daniel Roth Apprentice

          I would look into host ip/dns/port/username/password for the connection where you get


          org.hibernate.exception.JDBCConnectionException: Cannot open connection


          Quite obvious of course, but I have, specially on macintrash, got that error, even when specifying an ip address.
          It seems like MacOS always does a lot of dns crap even when a valid ip address is specified, so if the the dns host is down, it will take the dns to timeout before it tries using the ip as is. Which, in my case, was longer than what the jdbc driver could stand.


          Some OS's are just not meant to be run as servers (or at all) :-)

          • 3. Re: Reference cannot be cast to SessionFactory (Quartz)
            Sefai Tando?an Newbie

            Thanks for the advice,but actually whole program uses the same connection,but just the quartz dispatcher throws this exception...

            • 4. Re: Reference cannot be cast to SessionFactory (Quartz)
              Daniel Roth Apprentice

              How did you configure Quartz? I have never got it working out of the box(war deployment) with just the components.xml async line. I have had to manually create the tables quartz needs and then have a seam.quartz.properties with the following content.


              # Configure Datasources
              org.quartz.dataSource.sitemonitorDS.jndiURL java:comp/env/jdbc/myJndiDB # <-- JNDI conn...
              
              # Scheduler Properties
              
              org.quartz.scheduler.instanceName Sched1
              org.quartz.scheduler.instanceId AUTO
              org.quartz.scheduler.rmi.export false
              org.quartz.scheduler.rmi.proxy false
              
              # ThreadPool
              
              org.quartz.threadPool.class org.quartz.simpl.SimpleThreadPool
              org.quartz.threadPool.threadCount 3
              
              # JobStore
              
              org.quartz.jobStore.misfireThreshold 1000
              org.quartz.jobStore.class org.quartz.impl.jdbcjobstore.JobStoreTX
              org.quartz.jobStore.driverDelegateClass org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
              org.quartz.jobStore.useProperties false
              org.quartz.jobStore.dataSource sitemonitorDS
              org.quartz.jobStore.tablePrefix QUARTZ_
              
              


              • 5. Re: Reference cannot be cast to SessionFactory (Quartz)
                Sefai Tando?an Newbie

                I think I have solved the problem by adding startup="true" and auto-create="true" parameters to the hibernate-session-factory...




                <persistence:hibernate-session-factory
                          name="hibernateSessionFactory" startup="true" auto-create="true" />



                In the below code Naming.getInitialContext().lookup(sessionFactoryJndiName) always returns a Reference when there is no session-factory present...By adding startup(true), we guarantee that SessionFactory will not be null when Quart job triggers, and SessionFactory will be loaded by  result = sessionFactory.getValue();,at least when the application is first deployed!


                private SessionFactory getSessionFactoryFromJndiOrValueBinding()
                   {
                      SessionFactory result = null;
                      //first try to find it via the value binding
                      if (sessionFactory!=null)
                      {
                         result = sessionFactory.getValue();
                      }
                      //if its not there, try JNDI
                      if (result==null)
                      {
                         try
                         {
                            result = (SessionFactory) Naming.getInitialContext().lookup(sessionFactoryJndiName);
                         }
                         catch (NamingException ne)
                         {
                            throw new IllegalArgumentException("SessionFactory not found in JNDI: " + sessionFactoryJndiName, ne);
                         }
                      }
                      return result;
                   }




                I will test more and post the results...


                But why does Naming.getInitialContext().lookup(sessionFactoryJndiName); returns a Reference if Quart Scheduler Bean requests a one, and why does it return a true SessionFactory if JSF requests, I dont know, maybe someone will explain it...