Guice CreationException with PersistFilter

I have a webapp that uses Guice and JPA/Hibernate. I'm trying to set up an importer to bring in a bunch of data, but I keep getting the following exception:

com.google.inject.CreationException: Guice creation errors:||1) Error in custom provider, java.lang.NullPointerException|  while locating com.google.inject.persist.jpa.JpaPersistService|  while locating javax.persistence.EntityManager|  at myApplication.services.SynchronizerService.<init>(SynchronizerService.java:86)|  while locating myApplication.services.SynchronizerService|  while locating myApplication.api.ISynchronizer|  at myApplication.portal.web.PortalWebModule$DataBootstrap.<init>(PortalWebModule.java:137)|  at myApplication.portal.web.PortalWebModule.setupDataImport(PortalWebModule.java:83)|  while locating myApplication.portal.web.PortalWebModule$DataBootstrap||1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:183)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
    at com.google.inject.Guice.createInjector(Guice.java:95)
    at com.google.inject.Guice.createInjector(Guice.java:72)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at myApplication.portal.web.PortalListener.getInjector(PortalListener.java:36)
    at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)
    at myApplication.portal.web.PortalListener.contextInitialized(PortalListener.java:24)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:775)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:767)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1252)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:710)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:293)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
    at org.eclipse.jetty.server.Server.doStart(Server.java:282)
    at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:520)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:365)
    at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:523)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: 
java.lang.NullPointerException
    at com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.java:70)
    at com.google.inject.persist.jpa.JpaPersistService.get(JpaPersistService.java:50)
    at com.google.inject.persist.jpa.JpaPersistService.get(JpaPersistService.java:34)
    at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:55)
    at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
    at myApplication.services.SynchronizerService$DealerLineProcessor.<init>(SynchronizerService.java:484)
    at myApplication.services.SynchronizerService$DealerLineProcessor.<init>(SynchronizerService.java:479)
    at myApplication.services.SynchronizerService.<init>(SynchronizerService.java:90)
    at myApplication.services.SynchronizerService$$EnhancerByGuice$$3b543af5.<init>(<generated>)
    at myApplication.services.SynchronizerService$$EnhancerByGuice$$3b543af5$$FastClassByGuice$$285038d1.newInstance(<generated>)
    at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)
    at com.google.inject.internal.ProxyFactory$ProxyConstructor.newInstance(ProxyFactory.java:260)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:54)
    at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
    at myApplication.portal.web.PortalWebModule$DataBootstrap.refresh(PortalWebModule.java:145)
    at myApplication.portal.web.PortalWebModule$DataBootstrap.<init>(PortalWebModule.java:139)
    at myApplication.portal.web.PortalWebModule$DataBootstrap$$FastClassByGuice$$8f91c171.newInstance(<generated>)
    at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)
    at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:60)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.Scopes$1$1.get(Scopes.java:65)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
    at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:204)
    at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:198)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
    at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:198)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:179)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
    at com.google.inject.Guice.createInjector(Guice.java:95)
    at com.google.inject.Guice.createInjector(Guice.java:72)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at myApplication.portal.web.PortalListener.getInjector(PortalListener.java:36)
    at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)
    at myApplication.portal.web.PortalListener.contextInitialized(PortalListener.java:24)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:775)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:767)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1252)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:710)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:293)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
    at org.eclipse.jetty.server.Server.doStart(Server.java:282)
    at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:520)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:365)
    at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:523)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

And here's the offending code. The notable parts are configureServlets(), setupDataImport(), and the DataBootstrap class.

package MyApplication.portal.web;

//imports

/**
 * Portal web module
 * 
 * @author mint
 * 
 */
public final class PortalWebModule extends ServletModule
{
    private final Map<String, String> config = new HashMap<String, String>();
    private final static Logger LOG = LoggerFactory.getLogger(PortalWebModule.class);

    @SuppressWarnings("unused")
    private final ServletContext context;

    public PortalWebModule(final ServletContext context)
    {
        this.context = context;
        this.config.put("com.sun.jersey.config.feature.DisableWADL", "true");
        this.config.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
        this.config.put("com.sun.jersey.spi.container.ContainerRequestFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter");
        this.config.put("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter");
    }

    @Override
    protected void configureServlets()
    {
        install(new WamModule(true));
        install(new PortalModule());
        install(new PortalResourcesModule());           

        setupEmbeddedDB();

        // Serve it...
        serve(PortalConfig.ROUTE_API)
                .with(GuiceContainer.class, this.config);

        filter("/*")
        .through(PersistFilter.class);

        // Handle CORS
        filter("/*")
                .through(CorsFilter.class);

        setupDataImport();

    }

    /**
     * Convenience method to setup and start data import with initial data.
     */
    private void setupDataImport()
    {
        this.bind(DataBootstrap.class)
                .asEagerSingleton();

        LOG.info("Started Data import");
    }

    /**
     * Enable use of embedded DB console.
     */
    private void setupEmbeddedDB()
    {
        if (WamConfig.DEBUG)
        {
            final Map<String, String> config = new HashMap<String, String>();
            config.put("webAllowOthers", "true");

            this.bind(WebServlet.class)
                    .asEagerSingleton();

            // Serve it...
            serve("/h2*")
                    .with(WebServlet.class, config);

            LOG.warn("Enabling Embedded DB access through browser");
        }
    }

    /**
     * This is a helper to start the data service.
     */
    //@Singleton
    private static class DataBootstrap
    {       
        //This ISynchronizer has the offending EntityManager
        private Provider<ISynchronizer> serviceProvider;
        @Inject
        public DataBootstrap(final Provider<ISynchronizer> serviceProvider) 
                throws SynchronizationException
        {
            this.serviceProvider = serviceProvider;
            refresh();
        }

        public void refresh(){
            try
            {
                ISynchronizer service = this.serviceProvider.get();
                service.refresh(false);
            }
            catch (SynchronizationException x)
            {
                // TODO Auto-generated catch block
                x.printStackTrace();
            }
        }
    }

}

I've read that this error usually comes up when you try to use an EntityManager before PersistService.start() is called, but the PersistFilter is doing that, and if I try explicitly starting it, I get java.lang.IllegalStateException: Persistence service was already initialized. I've also tried playing around with some other tactics: switching between straight instances and Providers, moving things around, insantiating things without the DataBootstrap class... but I couldn't get anywhere with any of them.

I'm pretty much out of ideas... Does anyone else know what's going on?

Thanks, Tyler

Answers


I found this question, and the workaround he mentioned worked. Basically, I called PersistService.start() elsewhere, and instead of using the default PersistFilter, I used a custom one which catches the IllegalStateException.


Need Your Help

No output produced for C# Console Application on 'Start without Debugging'

c# debugging visual-studio-2015

When attempting to run a C# console application without the debugger (ie Ctrl+F5) no output appears in the terminal.