Why does Flexy-Pool cause a NoSuchElementException when configured via Spring in Apache Storm

189 Views Asked by At

I have an Apache Storm (1.1.1) based application, which uses Spring (5.0.3) to configure and inject Hibernate-based (5.2.12) database services, which connect to a PostgreSQL database.

Between Hibernate and the database, I have configured flexy-pool (2.0.0) on top of HikariCP (2.7.6) as a datasource (see Figure A). The application is launched on some Ubuntu 16.04 LTS server instances with the latest OpenJDK 8 version.

Figure A: With flexy-pool, Figure B: Without flexy-pool

I experience sad runtime exceptions related to flexy-pool after starting my Storm topology. However, this is not deterministic. Sometimes it will work, sometimes the exception below will be thrown. If I do not use flexy-pool in my application (see Figure B) everything works everytime as expected.

Caused by: 
 org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.vladmihalcea.flexypool.config.Configuration]: Factory method 'build' threw exception; nested exception is java.util.NoSuchElementException at 
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at 
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) ... 42 more  
Caused by: 
 java.util.NoSuchElementException at 
sun.misc.URLClassPath$1.nextElement(URLClassPath.java:282) at 
sun.misc.URLClassPath$1.nextElement(URLClassPath.java:256) at 
java.net.URLClassLoader$3$1.run(URLClassLoader.java:603) at 
java.net.URLClassLoader$3$1.run(URLClassLoader.java:599) at 
java.security.AccessController.doPrivileged(Native Method) at 
java.net.URLClassLoader$3.next(URLClassLoader.java:598) at 
java.net.URLClassLoader$3.hasMoreElements(URLClassLoader.java:623) at 
sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45) at 
sun.misc.CompoundEnumeration.nextElement(CompoundEnumeration.java:58) at 
java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:357) at 
java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393) at 
java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474) at 
com.vladmihalcea.flexypool.metric.MetricsFactoryResolver.resolve(MetricsFactoryResolver.java:27) at 
com.vladmihalcea.flexypool.config.Configuration$Builder.build(Configuration.java:165) at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at 
java.lang.reflect.Method.invoke(Method.java:498) at 
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)

The Spring configuration used in Figure B is this one (based on the official sample):

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="springHikariCP"/>
    <property name="connectionTestQuery" value="SELECT 1"/>
    <property name="dataSourceClassName" value="${gmetric.dataSourceClassName}"/>
    <property name="dataSourceProperties">
        <props>
            <prop key="url">${shc.gmetric.url}</prop>
            <prop key="user">${shc.gmetric.username}</prop>
            <prop key="password">${shc.gmetric.pw}</prop>
        </props>
    </property>
</bean>

<bean id="poolingDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig"/>
</bean>

<bean id="configurationBuilder" class="com.vladmihalcea.flexypool.config.Configuration$Builder">
    <constructor-arg value="uniqueId"/>
    <constructor-arg ref="poolingDataSource"/>
    <constructor-arg value="#{ T(com.vladmihalcea.flexypool.adaptor.HikariCPPoolAdapter).FACTORY }"/>
</bean>

<bean id="configuration" factory-bean="configurationBuilder" factory-method="build"/>

<bean id="flexyPoolDataSource" class="com.vladmihalcea.flexypool.FlexyPoolDataSource" init-method="start" destroy-method="stop">
    <constructor-arg ref="configuration"/>
    <constructor-arg>
        <array>
            <bean class="com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory">
                <constructor-arg value="5"/>
            </bean>
            <bean class="com.vladmihalcea.flexypool.strategy.RetryConnectionAcquiringStrategy.Factory">
                <constructor-arg value="2"/>
            </bean>
        </array>
    </constructor-arg>
</bean>

<bean id="entityManagerFactoryGMetrics"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="flexyPoolDataSource"/>
    <property name="persistenceUnitName" value="${gmetric.context.name}"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <!--<property name="generateDdl" value="true" />-->
            <property name="databasePlatform" value="${hibernate.dialect}"/>
            <property name="showSql" value="${hibernate.showSQL}"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.connection.driver_class">${gmetric.driverClassName}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
        </props>
    </property>
</bean>

My questions are:

  1. Is my Spring-based (default) configuration for flexy-pool correct?
  2. Is there a solution to prevent this runtime exception related to flexy-pool?
  3. What is the cause for this NoSuchElementException?
1

There are 1 best solutions below

0
On BEST ANSWER

I found, that Dropwizard Metrics 4 (which is required by flexy-pool) was missing in my shaded jar and therefore this wired runtime exception occured:

<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>${metrics.version}</version>
</dependency>

According to the author this is related to the way flexy-pool detects the related Dropwizard Metrics handler (as it supports DM3 and DM4, which cannot be used interchangeably).