Menu

javax.management.InstanceAlreadyExistsException: net.sf.hajdbc:cluster=

Help
anztenney
2013-02-02
2013-02-05
  • anztenney

    anztenney - 2013-02-02

    I'm working on using ha-jdbc to connect to sql server. Here is a look at my ha-jdbc file:

    <?xml version="1.0"?>
    <ha-jdbc xmlns="urn:ha-jdbc:cluster:2.1">
        <sync id="passive"/>
        <state id="simple"/>
        <cluster 
            balancer="load"
            default-sync="passive"
            dialect="standard"
            meta-data-cache="lazy"
            transaction-mode="parallel"
            auto-activate-schedule="0 0/2 * ? * *"
            failure-detect-schedule="0 0/2 * ? * *">
            <database id="warehouse_1" location="jdbc:sqlserver://server;databaseName=db1;" weight="1">
              <user>user</user>
              <password>password</password>
            </database>
            <database id="warehouse_2" location="jdbc:sqlserver://server;databaseName=db2" weight="1">
              <user>user</user>
              <password>password</password>
            </database>
            <database id="warehouse_3" location="jdbc:sqlserver://server;databaseName=db3" weight="1">
              <user>user</user>
              <password>password</password>
            </database>
        </cluster>
    </ha-jdbc>
    

    However, when I'm starting my application, I'm getting the following stacktrace:

    2013-02-01 21:11:28,435 [net.sf.hajdbc.xml.XMLDatabaseClusterConfigurationFactory - pool-2-thread-1] INFO - Initializing HA-JDBC 2.1.0-beta-3 from C:\dev\instances\TCServer\TC263 - survey_system_updates_ha\lib\ha-jdbc-warehouse.xml
    2013-02-01 21:11:29,335 [org.hibernate.util.JDBCExceptionReporter - pool-2-thread-1] WARN - SQL Error: 0, SQLState: null
    2013-02-01 21:11:29,335 [org.hibernate.util.JDBCExceptionReporter - pool-2-thread-1] ERROR - net.sf.hajdbc:cluster=warehouse,type=DatabaseCluster
    2013-02-01 21:11:29,337 [org.hibernate.cfg.SettingsFactory - pool-2-thread-1] WARN - Could not obtain connection to query metadata
    java.sql.SQLException: net.sf.hajdbc:cluster=warehouse,type=DatabaseCluster
    at net.sf.hajdbc.sql.SQLExceptionFactory.createException(SQLExceptionFactory.java:52)
    at net.sf.hajdbc.sql.SQLExceptionFactory.createException(SQLExceptionFactory.java:36)
    at net.sf.hajdbc.sql.AbstractExceptionFactory.createException(AbstractExceptionFactory.java:49)
    at net.sf.hajdbc.util.concurrent.LifecycleRegistry.get(LifecycleRegistry.java:94)
    at net.sf.hajdbc.util.concurrent.LifecycleRegistry.get(LifecycleRegistry.java:33)
    at net.sf.hajdbc.sql.Driver.connect(Driver.java:124)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:267)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:176)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:662)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:602)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:465)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:130)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:112)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:99)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:123)
    at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:114)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4766)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5262)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:897)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:873)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:649)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1581)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
    Caused by: javax.management.InstanceAlreadyExistsException: net.sf.hajdbc:cluster=warehouse,type=DatabaseCluster
    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:512)
    at net.sf.hajdbc.management.DefaultMBeanRegistrar.register(DefaultMBeanRegistrar.java:90)
    at net.sf.hajdbc.management.DefaultMBeanRegistrar.register(DefaultMBeanRegistrar.java:73)
    at net.sf.hajdbc.sql.DatabaseClusterImpl.start(DatabaseClusterImpl.java:739)
    at net.sf.hajdbc.util.concurrent.LifecycleRegistry.get(LifecycleRegistry.java:75)
    ... 47 more

    When I try and use this datasource it also throws the InstanceAlreadyExistsException, however I can't see it in my mbeans at all (via jconsole.)

     
  • Paul Ferraro

    Paul Ferraro - 2013-02-02

    I think I figured out why you're getting the InstanceAlreadyExistsException, but this is only masking the actual problem, which is that your database cluster is failing to start. I've fixed your 1st issue here:
    https://github.com/ha-jdbc/ha-jdbc/commit/af64ff4af6eb49db5ebafda3efdb53f2b15654de

    However, we still need to identify the root cause of why your database cluster fails to start. Do you see any other log messages?

    Also, for sql server, you probably want to use the "sybase" dialect.

     
  • anztenney

    anztenney - 2013-02-04

    I have switched to using the sybase dialect.
    The only message I see prior to this for hajdbc is

    2013-02-04 07:38:32,436 [net.sf.hajdbc.logging.LoggerFactory - pool-2-thread-1] DEBUG - Using SLF4J logging

    Then the above logging. Even with the above committed changes in place I am still getting the InstanceAlreadyExistsException, though the stack trace looks slightly different:

    2013-02-04 07:38:34,799 [org.hibernate.cfg.SettingsFactory - pool-2-thread-1] WARN - Could not obtain connection to query metadata
    java.sql.SQLException: net.sf.hajdbc:cluster=warehouse,type=DatabaseCluster
    at net.sf.hajdbc.sql.SQLExceptionFactory.createException(SQLExceptionFactory.java:52)
    at net.sf.hajdbc.sql.SQLExceptionFactory.createException(SQLExceptionFactory.java:36)
    at net.sf.hajdbc.sql.AbstractExceptionFactory.createException(AbstractExceptionFactory.java:49)
    at net.sf.hajdbc.util.concurrent.LifecycleRegistry.get(LifecycleRegistry.java:94)
    at net.sf.hajdbc.util.concurrent.LifecycleRegistry.get(LifecycleRegistry.java:1)
    at net.sf.hajdbc.sql.Driver.connect(Driver.java:124)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:267)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:176)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:662)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:602)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:465)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:130)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:112)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:99)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:123)
    at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:114)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4766)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5262)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:897)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:873)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:649)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1581)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
    Caused by: javax.management.InstanceAlreadyExistsException: net.sf.hajdbc:cluster=warehouse,type=DatabaseCluster
    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:512)
    at net.sf.hajdbc.management.DefaultMBeanRegistrar.register(DefaultMBeanRegistrar.java:90)
    at net.sf.hajdbc.management.DefaultMBeanRegistrar.register(DefaultMBeanRegistrar.java:73)
    at net.sf.hajdbc.sql.DatabaseClusterImpl.start(DatabaseClusterImpl.java:739)
    at net.sf.hajdbc.util.concurrent.LifecycleRegistry.get(LifecycleRegistry.java:75)
    ... 47 more

     
  • Paul Ferraro

    Paul Ferraro - 2013-02-04

    OK - it seems that your server is attempting to start multiple HA-JDBC database clusters in separate, isolated classloaders, each attempting to register mbeans with the platform mbean server. Can you elaborate on how HA-JDBC is configured in your server?

     
  • anztenney

    anztenney - 2013-02-04

    First, we are trying to upgrade from ha-jdbc-2.0.13 which we have been succesfully for 3+ years. We're mainly interested in the ability to read system properties from the config file.

    We have 3 different clusters and corresponding config files. Each of these clusters is layed out almost the same as to what is shown above. Each of the config files has a name such as ha-jdbc-warehouse.xml, ha-jdbc-warehouse2.xml, etc. We then use this in our datasource in Spring in the following fashion.

    <bean id="datasource1" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="driverClassName" value="net.sf.hajdbc.sql.Driver" />
        <property name="url" value="jdbc:ha-jdbc:warehouse"/>
        <property name="username" value="user"/>
        <property name="password" value="password"/>
        ....
    </bean>
    
     
  • Paul Ferraro

    Paul Ferraro - 2013-02-05

    OK - I figured out the problem...

    org.apache.tomcat.jdbc.pool.DataSource doesn't use the DriverManager to obtain connections, but rather instantiates a new driver instance every time it creates a connection! That's not good - since HA-JDBC retains references to each database cluster within the driver instance itself. That means that HA-JDBC will try to recreate the database cluster every time the Tomcat connection pool attempts to create a new connection! If, instead, Tomcat used the DriverManager to obtain new connections, then all would be fine.

    Unfortunately, I can only think of one way to fix this. I'll need to make the database cluster references within the Driver static. Here's the fix:
    https://github.com/ha-jdbc/ha-jdbc/commit/980cc731f30b35fd4e46efb14f73ec95babf72d1

     

Log in to post a comment.