Grails 3 JNDI数据源抛出javax.management.InstanceAlreadyExistsException

时间:2019-02-21 01:54:03

标签: grails datasource jndi

我的Grails 3应用程序中有两个不同的JNDI数据源。当不使用JNDI(开发人员或生产人员)时,该应用程序将启动并正确运行。但是,当使用JNDI数据源在Tomcat中构建和运行战争时,出现以下异常。它似乎起源于与jmx的冲突,但是我尝试在application.yml和数据源本身中的任何地方禁用jmx。但是,问题仍然存在。 Grails 3.2.13,Tomcat 9。

注意:使用Oracle数据库作为主数据库可以解决此问题。在我看来,Postgres的错误是由使用中的默认数据源工厂引起的。也就是说,我不知道如何纠正它;我以为禁用jmx可以做到。

application.groovy:

production:
    dataSource:
        jmxEnabled: false
        dbCreate: none
        jndiName: java:comp/env/jdbc/jpmt-prod-db
    dataSources:
        jddt4:
            jmxEnabled: false
            dbCreate: none
            jndiName: java:comp/env/jdbc/jddt4-prod-db

Tomcat xml配置:

<Resource   
    name="jdbc/jpmt-prod-db"
    auth="Container"
    type="javax.sql.DataSource"
    url="jdbc:postgresql://<pg_host>:5432/<db>?currentSchema=sch"
    driverClassName="org.postgresql.Driver"
    dialect="org.hibernate.dialect.PostgreSQLDialect"
    username="<user>" 
    password="<pswd>"
    testOnBorrow="true"
    removeAbandonedOnBorrow="true"
    removeAbandonedTimeout="30"
    logAbandoned="true"
    maxActive="20"
    maxIdle="10" 
    minIdle="5"
    minEvictableIdleTimeMillis="1800000"
    timeBetweenEvictionRunsMillis="1800000"
    numTestsPerEvictionRun="3"
    validationQuery="select 1"
    jmxEnabled="false"
    />

<Resource   
    name="jdbc/jddt4-prod-db"
    url="jdbc:oracle:thin:@<host>:<sid>"
    type="oracle.jdbc.pool.OracleDataSource"
    factory="oracle.jdbc.pool.OracleDataSourceFactory"
    driverClassName="oracle.jdbc.OracleDriver"
    dialect="org.hibernate.dialect.Oracle10gDialect"
    user="<user>" 
    password="<pswd>"
    logSql="false"
    removeAbandoned="true"
    removeAbandonedTimeout="30"
    logAbandoned="true"
    maxActive="20"
    maxIdle="10" 
    maxwait="-1"
    minEvictableIdleTimeMillis="1800000"
    timeBetweenEvictionRunsMillis="1800000"
    numTestsPerEvictionRun="3"
    validationQuery="select 1 from dual"
    jmxEnabled="false"
    />

Grails启动时出现异常:

2019-02-20 18:24:32,588 ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [org.apache.tomcat.dbcp.dbcp2.BasicDataSource@1485a2ce] with key 'dataSourceUnproxied'; nested exception is javax.management.InstanceAlreadyExistsException: Catalina:type=DataSource,host=localhost,context=/,class=javax.sql.DataSource,name="jdbc/jpmt-prod-db"
    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:628)
    at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:550)
    at org.springframework.jmx.export.MBeanExporter.afterSingletonsInstantiated(MBeanExporter.java:432)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
    at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151)
    at org.grails.boot.context.web.GrailsAppServletInitializer.createRootApplicationContext(GrailsAppServletInitializer.groovy:57)
    at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5098)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:630)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1840)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:525)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:424)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1585)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:308)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
    at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:424)
    at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:367)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:969)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:261)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
    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.apache.catalina.startup.Bootstrap.start(Bootstrap.java:350)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)

由以下原因引起:javax.management.InstanceAlreadyExistsException:Catalina:type = DataSource,host = localhost,context = /,class = javax.sql.DataSource,name =“ jdbc / jpmt-prod-db”         在com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437)         在com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898)         在com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966)         在com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)         在com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)         在com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)         在org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)         在org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:675)         在org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:618)         ...省略了54个共同的框架

1 个答案:

答案 0 :(得分:0)

答案似乎是设置spring的属性。

spring.jmx.enabled: false

此链接提供了解决方案: https://github.com/spring-projects/spring-boot/issues/9179