hikari open two pool connections when spring configure ContextLoaderListener and DispatchServlet

Viewed 15

I have a legacy project in spring mvc, I put hikaricp in it, but at boot time two pool connections are created. I was reviewing the reason and I identified that in the web.xml file if I comment the org.springframework.web.context.ContextLoaderListener, there it only starts a pool connection, but when doing that, the beans managed by spring are not recognized by the beans managed by jsf.

This is my web.xml file:

    <?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID"
    version="3.1">

    <display-name>Simba</display-name>
    <welcome-file-list>
        <welcome-file>login.faces</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>720</session-timeout>
    </session-config>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <error-page>
        <error-code>404</error-code>
        <location>/pages/error/404.faces</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/pages/error/500.faces</location>
    </error-page>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <!-- <param-value>${project.build.stage}</param-value> -->
        <!-- <param-value>Development</param-value> -->
        <param-value>Production</param-value>
    </context-param>

    <filter>
        <filter-name>cache</filter-name>
        <filter-class>config.CacheFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>cache</filter-name>
        <url-pattern>*.faces</url-pattern>
    </filter-mapping>

    <mime-mapping>
        <extension>ttf</extension>
        <mime-type>application/x-font-ttf</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>woff</extension>
        <mime-type>application/x-font-woff</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>woff2</extension>
        <mime-type>application/x-font-woff2</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>eot</extension>
        <mime-type>application/x-font-eot</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>svg</extension>
        <mime-type>image/svg+xml</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jsp</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>

</web-app>

and this is my spring-servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd ">

    <!-- Add support for component scanning -->
    <context:component-scan base-package="Controller,service,Repository,simba.service,woo.service,prestashop.service" />
    <!-- Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven />
    <!-- Define Spring MVC view resolver -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    
    <!-- HikariCP -->
    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
      <property name="poolName" value="${hikari.poolName}" />
      <property name="dataSourceClassName" value="${hikari.dataSourceClassName}"/>
      <property name="minimumIdle" value="${hikari.minimumIdle}"></property>
      <property name="maximumPoolSize" value="${hikari.maximumPoolSize}" />
      <property name="autoCommit" value="${hikari.autoCommit}"/>
<!--      <property name="leakDetectionThreshold" value="${hikari.leakDetectionThreshold}" /> -->
      <property name="dataSourceProperties">
       <props>
         <prop key="serverName">${hikari.dataSource.serverName}</prop>
         <prop key="portNumber">${hikari.dataSource.portNumber}</prop>
         <prop key="databaseName">${hikari.dataSource.databaseName}</prop>
         <prop key="user">${hikari.dataSource.user}</prop>
         <prop key="password">${hikari.dataSource.password}</prop>
       </props>
      </property>
    </bean>
    
    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
      <constructor-arg ref="hikariConfig" />
    </bean>
    <!-- End HikariCP -->
    
    <bean id="flywayConfig" class="org.flywaydb.core.api.configuration.ClassicConfiguration">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
        <constructor-arg ref="flywayConfig"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" depends-on="flyway">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="Entity"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
            </props>
        </property>
    </bean>
    
    <!-- Step 3: Setup Hibernate transaction manager -->
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager" />
    
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="locations">
            <list>
                <value>classpath:hibernate.properties</value>
                <value>classpath:hikari.properties</value>          
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>
     
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000" />
    </bean>
    <!-- Add support for reading web resources: css, images, js, etc ... -->
    <mvc:resources mapping="/**" location="/resources/" cache-period="3600" />
    <mvc:resources mapping="/resources/**" location="/resources/" cache-period="3600" />
</beans>

how could i configure hikari not to create two pool connections, any suggestions?, thanks

0 Answers
Related