Unable to register MBean [com.haulmont.cuba.core.jmx.PersistenceManager

Hi Cuba-Platform followers,

I’ve just deployed onto Production Tomcat server a new version of my application and next error was thrown:

17-Sep-2019 18:47:47.615 GRAVE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Excepción enviando evento inicializado de contexto a instancia de escuchador de clase [com.haulmont.cuba.core.sys.singleapp.SingleAppCoreServletListener]
 java.lang.RuntimeException: An error occurred while starting single WAR application
	at com.haulmont.cuba.core.sys.singleapp.SingleAppCoreServletListener.contextInitialized(SingleAppCoreServletListener.java:107)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4817)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5283)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1140)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [com.haulmont.cuba.core.jmx.PersistenceManager@14f32589] with key 'app-core.cuba:type=PersistenceManager'; nested exception is javax.management.InstanceAlreadyExistsException: app-core.cuba:type=PersistenceManager
	at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:625)
	at org.springframework.jmx.export.MBeanExporter.lambda$registerBeans$2(MBeanExporter.java:551)
	at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
	at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:551)
	at org.springframework.jmx.export.MBeanExporter.afterSingletonsInstantiated(MBeanExporter.java:434)
	at com.haulmont.cuba.core.sys.jmx.MBeanExporter.afterSingletonsInstantiated(MBeanExporter.java:67)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:866)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:95)
	at com.haulmont.cuba.core.sys.CubaClassPathXmlApplicationContext.<init>(CubaClassPathXmlApplicationContext.java:27)
	at com.haulmont.cuba.core.sys.CubaCoreApplicationContext.<init>(CubaCoreApplicationContext.java:26)
	at com.haulmont.cuba.core.sys.singleapp.SingleAppCoreContextLoader$1.<init>(SingleAppCoreContextLoader.java:99)
	at com.haulmont.cuba.core.sys.singleapp.SingleAppCoreContextLoader.createApplicationContext(SingleAppCoreContextLoader.java:99)
	at com.haulmont.cuba.core.sys.singleapp.SingleAppCoreContextLoader.createApplicationContext(SingleAppCoreContextLoader.java:43)
	at com.haulmont.cuba.core.sys.AbstractAppContextLoader.initAppContext(AbstractAppContextLoader.java:62)
	at com.haulmont.cuba.core.sys.AbstractWebAppContextLoader.contextInitialized(AbstractWebAppContextLoader.java:78)
	at com.haulmont.cuba.core.sys.singleapp.SingleAppCoreContextLoader.contextInitialized(SingleAppCoreContextLoader.java:64)
	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.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282)
	at com.haulmont.cuba.core.sys.singleapp.SingleAppCoreServletListener.contextInitialized(SingleAppCoreServletListener.java:101)
	... 13 more
Caused by: javax.management.InstanceAlreadyExistsException: app-core.cuba:type=PersistenceManager
	at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
	at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:137)
	at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:679)
	at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:615)
	... 37 more

On the development environment this error didn´t appear and the only the only enhancement that incorporates this version that makes use of the PersistenceManager object is in the next service vean:

import com.company.logistica.entity.FacProvEstatEnum;

import javax.inject.Inject;
import java.util.List;

@Service(FacturaComiService.NAME)
public class FacturaComiServiceBean implements FacturaComiService {
    @Inject
    private Persistence persistence;

    @Override
    public void baixaFactProv(FacturaComi facturaComi) {
        List<FacturaProv> facturaProvList;
        facturaProvList = facturaComi.getFacturesProveidor();
        //Inici transacció
        try (Transaction tx = persistence.createTransaction()){
            //Obtenir l'Entity Manager de la transacció activa:
            EntityManager em = persistence.getEntityManager();
            //Obtenim els registres de les factures de proveïdor de la corresponent debit note per canviar l'estat i deixar a null el camp de debit note en les factures de proveïdor:
            for (FacturaProv facturaProv : facturaProvList) {
                //Crees una nova instància dins de l'Entity Manager associada a la instància de la factura del proveïdor i queden ambdues associades mitjançant l'Entity Manager
                FacturaProv facturaProv1 = em.merge(facturaProv);
                //Modifiques els atributs en la instància de l'Entity Manager. Amb un commit o un flush() s'actualitzaran els canvis a BBDD
                facturaProv1.setEstat(FacProvEstatEnum.Pendent);
                facturaProv1.setFacComiId(null);
            }
            //Commit de la transacció
            tx.commit();
        }
    }
}

I would be very grateful if someone could help me to solve this error.
Thanks in advance.

Hi,

javax.management.InstanceAlreadyExistsException: app-core.cuba:type=PersistenceManager

This error means:
“I tried to register a MBean with name app-core.cuba:type=PersistenceManager but I could not do it because another MBean with this name is already registered, and MBean names must be unique”.

MBean name is formed by using cuba.webContextName property:

cuba.webContextName = app-core

It may mean two things:

  1. You have two web applications in your tomcat with the same value of “cuba.webContextName”

  2. You were re-deploying “app-core” web application but previous version of the app failed to stop for some reason. In this case restarting the tomcat will help.

Thank you, Alex, for your answer.

Between the two possibilities that you mention I suppose it’s the second one, because in the first deploying of this versión the installation was stopped by an error executing a database script.

By the moment the application is working fine and users haven’t reported any related problem.

Regards