Class JpaServletContextListener

    • Constructor Detail

      • JpaServletContextListener

        public JpaServletContextListener()
    • Method Detail

      • getMainPersistenceUnitName

        protected abstract String getMainPersistenceUnitName()
        Returns the name of the main persistence unit in persistence.xml file.
      • getMainJpaThreadPoolSize

        protected abstract int getMainJpaThreadPoolSize()
        Returns the size of the thread pool to be used by mainJpaExecutor.

        The value should be the same or slightly bigger than the size of the JDBC connection pool referenced in the definition of the persistence unit named getMainPersistenceUnitName() in persistence.xml file. This way scheduled tasks will be obtaining JDBC connections very fast and the connection pool will be optimally utilized.

      • createJpaExecutor

        protected ContextTrackingExecutor createJpaExecutor​(String persistenceUnitName,
                                                            int poolSize)
        Creates an executor to be associated with a persistence unit. Used to create mainJpaExecutor. Subclasses may override this method to customize executor creation. By default returns
         servletModule.newContextTrackingExecutor(
                 persistenceUnitName + JPA_EXECUTOR_NAME_SUFFIX, poolSize);
      • isSinglePersistenceUnitApp

        protected boolean isSinglePersistenceUnitApp()
        Indicates whether this app uses only 1 persistence unit. By default true.

        If true then EntityManager instances, EntityManagerFactory and its associated mainJpaExecutor injections are bound without names, so that user-defined components don't need to annotate injected fields/params with @Named(JpaServletContextListener.MAIN_PERSISTENCE_UNIT_BINDING_NAME) when there's only 1 choice.

        Apps that use multiple persistence units should create a separate injection binding name constant and a separate ContextTrackingExecutor instance for each persistence unit. Each executor's threadPool size should correspond to the connection pool size of its persistence unit.
        After that, given persistence unit's EntityManagerFactory, executor and EntityManager should be bound with the corresponding constant as the value of @Named in GuiceServletContextListener.configureInjections() similarly to the below:

         @Override
         protected boolean isSinglePersistenceUnitApp() { return false; }
        
         public static final String CHAT_LOG_NAME = "chatLogDb"; // same as in persistence.xml
         public static final int CHAT_LOG_POOL_SIZE = 10;
         EntityManagerFactory chatLogEntityManagerFactory;
         ContextTrackingExecutor chatLogJpaExecutor;
        
         @Override
         protected LinkedList configureInjections() {
             chatLogEntityManagerFactory = Persistence.createEntityManagerFactory(CHAT_LOG_NAME);
             chatLogJpaExecutor = createJpaExecutor(CHAT_LOG_NAME, CHAT_LOG_POOL_SIZE);
             var modules = new LinkedList();
             modules.add((binder) -> {
                 binder.bind(EntityManager.class)
                         .annotatedWith(Names.named(CHAT_LOG_NAME))
                         .toProvider(() -> chatLogEntityManagerFactory.createEntityManager())
                         .in(servletModule.containerCallScope);
                 binder.bind(EntityManagerFactory.class)
                         .annotatedWith(Names.named(CHAT_LOG_NAME))
                         .toInstance(chatLogEntityManagerFactory);
                 binder.bind(ContextTrackingExecutor.class)
                         .annotatedWith(Names.named(CHAT_LOG_NAME))
                         .toInstance(chatLogJpaExecutor);
             });
        
             // more modules here...
         }
        
         @Override
         public void contextDestroyed(ServletContextEvent event) {
             // user components shutdowns here...
             super.contextDestroyed(event);
             chatLogEntityManagerFactory.close();
         }
        See Also:
        example app with multiple persistence units.