Unable to register MBean [HikariDataSource (HikariPool-0)] with key 'dataSource'

筅森魡賤 提交于 2019-12-02 20:25:19

I was having a similar issue, with 2 jhipster application instances running alongside on a single tomcat server. I posted this also in https://github.com/jhipster/generator-jhipster/issues/874#issuecomment-113023849

From https://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html

Every JMX MBean must have an object name. The object name is an instance of the JMX class ?ObjectName and must conform to the syntax defined by the JMX specification. Namely, the object name must contain a domain and a list of key-properties.

From http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-jmx

  1. Monitoring and management over JMX Java Management Extensions (JMX) provide a standard mechanism to monitor and manage applications. By default Spring Boot will create an MBeanServer with bean id ‘mbeanServer’ and expose any of your beans that are annotated with Spring JMX annotations (@ManagedResource, @ManagedAttribute, @ManagedOperation).

See the JmxAutoConfiguration class for more details.

Checking the code of JmxAutoConfiguration in https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java I saw that there is a spring.jmx.default-domain property that is used in the naming strategy.

Setting that property to some value in one of my apps' application.properties solved this issue

spring.jmx.default-domain: test

As the domain name is arbitrary, it seems to me like a reasonable way to avoid name clashings between two apps.

Since I had no previous experience with JMX I would appreciate feedback on this solution.

PowR

I solved this kind of problem by setting different value of spring.jmx.default-domain property for each Spring applications on my Tomcat like:

spring.jmx.default-domain=somevalue

in application.properties file.

Two things I tried and they worked for deploying two Hikari apps in the same tomcat are: Not only change poolName(1) but also change bean name for DataSource(2) Configuration.

@Bean(destroyMethod = "shutdown")
public DataSource dataSource2() {

    HikariConfig config = new HikariConfig();
    config.setPoolName("AARSHikaripool-1");

Notice bean name is datasource2 while other app has datasource!!

Update for JHipster:

Since there is no datasource bean anymore, in application.yml add the following:

spring:
    jmx:
        default-domain: [application_name]

Simply add below annotation on your Config file

@EnableMBeanExport(registration=RegistrationPolicy.IGNORE_EXISTING)

It will work for you.

This seems like your application is re-deploying, but when it un-deployed the container did not call the close() or shutdown() method on the HikariDataSource. Spring should have a "destroy" property (or something akin) that can be set for un-deployment.

Also, make sure that you are using the latest version of HikariCP (2.2.5) if possible, I believe an old version did not unregister MBeans properly.

EDIT: if you have two WARs in the same VM that need HikariCP, and you want to register MBeans, you need to set each one to use a different poolName. I see it is using the default pool name of HikariPool-0.

The following approach worked for me, randomize the pool name jmx domain (spring.jmx.default-domain)(please see JmxAutoConfiguration) :

@Bean
public DataSource dataSource() throws SQLException {
    HikariDataSource dataSource = new HikariDataSource(this);
    dataSource.setPoolName("dataSource_" + UUID.randomUUID().toString());
    return dataSource;
}

@Bean
@ConditionalOnMissingBean(value = ObjectNamingStrategy.class, search = SearchStrategy.CURRENT)
public ParentAwareNamingStrategy objectNamingStrategy() {
    ParentAwareNamingStrategy namingStrategy = new ParentAwareNamingStrategy(new AnnotationJmxAttributeSource());
    namingStrategy.setDefaultDomain("domain_" + UUID.randomUUID().toString());
    return namingStrategy;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!