Announcement Announcement Module
Collapse
No announcement yet.
Can't get EntityManger in PhaseListener (NullPointerException) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't get EntityManger in PhaseListener (NullPointerException)

    I'm trying to write a PhaseListener that can read/write from my database - but trying to inject @EntityManager, or injecting another class (a DAO) that then contains an injected @EntityManager keeps giving me a NullPointerException.

    Is there any way to inject (or otherwise access the EntityManager) in a PhaseListener?

    A few details about my setup:

    Here is my applicationContext.xml
    Code:
        <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
                    <property name="showSql" value="true" />
                </bean>
            </property>
            <property name="loadTimeWeaver">
                <bean class="org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver"/>
            </property>
        </bean>
    
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
        <tx:annotation-driven transaction-manager="transactionManager" />
        <context:annotation-config />
    And injecting entity manager with
    Code:
        @PersistenceContext
        private EntityManager em;
    works properly in other Java classes.

    Here is the PhaseListener (very basic at the moment!)
    Code:
    public class AccessStatisticsTracker implements PhaseListener {
        @PersistenceContext
        private EntityManager em;
    
        @Override
        public void afterPhase(PhaseEvent event) {
            FacesContext context = FacesContext.getCurrentInstance();
            FacesContext.getCurrentInstance().getExternalContext().log("AFTER - " + event.getPhaseId());
    
            List<Person> people = (List<Person>) em.find(Person.class, new Long(51));
        }
    
        @Override
        public void beforePhase(PhaseEvent event) {
            FacesContext.getCurrentInstance().getExternalContext().log("BEFORE - " + event.getPhaseId());
        }
    
        public PhaseId getPhaseId() {
            return PhaseId.RENDER_RESPONSE;
        }
    }
    and here's what happens:
    Code:
    PWC1412: WebModule[/csb] ServletContext.log():AFTER - RENDER_RESPONSE 6
    phase(RENDER_RESPONSE 6,com.sun.faces.context.FacesContextImpl@429fdea9) threw exception: java.lang.NullPointerException null
    com.sixamsoftware.util.AccessStatisticsTracker.afterPhase(AccessStatisticsTracker.java:32)
    com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:280)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
    org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:333)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
    org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
    org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:873)
    org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:723)
    org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:558)
    org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:490)
    org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:382)
    com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:68)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:111)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:313)
    org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
    org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
    com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
    org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
    org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
    org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
    org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
    org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288)
    com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647)
    com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579)
    com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
    com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
    com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
    com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
    com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:380)
    com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
    com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
    Can anyone tell me whether is it (im)possible to get EntityManager in a PhaseListener, and how to do it?

    Let me know if you need any more details about my setup.

    Thanks
    Last edited by Taadow; Jan 10th, 2010, 04:48 PM. Reason: added more details

  • #2
    Your PhaseListener isn't under control of spring so you can not inject anything into it. You will need to get a reference to the applicationcontext and then retrieve it yourself.

    Comment


    • #3
      JSF does not currently provide dependency injection for PhaseListeners. This is currently being discussed as a new feature in JSF2.next

      Please voice your need in any of the JSF community forums, it will make our argument all that much stronger, and make it that much more likely to be included.

      http://www.javaserverfaces.org <-- JSF community home-page

      Thanks!
      --Lincoln
      http://ocpsoft.com
      http://scrumshark.com

      Comment

      Working...
      X