Bug 54194 - NPE in DataSource.registerJmx() although DataSource is created with setJmxEnabled(false)
NPE in DataSource.registerJmx() although DataSource is created with setJmxEna...
Status: RESOLVED FIXED
Product: Tomcat Modules
Classification: Unclassified
Component: jdbc-pool
unspecified
PC Linux
: P2 minor (vote)
: ---
Assigned To: Tomcat Developers Mailing List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2012-11-23 14:35 UTC by tobias.gierke
Modified: 2012-12-04 06:37 UTC (History)
1 user (show)



Attachments
Patch against DataSource.java (rev 1412899) from /tomcat/tc7.0.x/trunk (719 bytes, text/plain)
2012-11-23 14:35 UTC, tobias.gierke
Details

Note You need to log in before you can comment on or make changes to this bug.
Description tobias.gierke 2012-11-23 14:35:58 UTC
Created attachment 29626 [details]
Patch against DataSource.java (rev 1412899) from /tomcat/tc7.0.x/trunk

The exception does not seem to have any impact (at least not for my application) on the connection pools functionality , it's merely a minor annoyance. 

The attached patch fixed the problem for me.

OS: Ubuntu 12.04 64-bit

JDK: 

java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

Version of tomcat-jdbc.jar: 7.0.33

I create the datasource inside a custom ObjectFactory (because I need to retrieve JDBC URL/username/password from a config file on the classpath):

Resources are configured inside tomcat/conf/context.xml as follows:
------------------------------------ 
<Resource name="jdbc/db0"
          database="db0"
          auth="Container"
          type="javax.sql.DataSource"          factory="my.DatasourceResourceFactory" />

<Resource name="jdbc/db1"
          database="db1"
          auth="Container"
          type="javax.sql.DataSource"
          factory="my.DatasourceResourceFactory" />

------------------------------------

I'm creating the datasource like so:

------------------------------------
    private DataSource createDataSource(String jdbcUrl,String username,String password) 
    {
        final PoolProperties p = new PoolProperties();
        p.setUrl( jdbcUrl );
        p.setDriverClassName("org.postgresql.Driver");
        p.setUsername( username );
        p.setPassword( password );      

        p.setJmxEnabled(false); // JMX disabled

        p.setTestWhileIdle(false);
        p.setTestOnBorrow(true);
        p.setValidationQuery("SELECT 1");
        p.setTestOnReturn(false);
        p.setValidationInterval(30000);
        p.setInitialSize(5); 
        p.setMaxActive(25);      
        p.setMinIdle(0); 
        p.setMaxIdle(8); 
        p.setTimeBetweenEvictionRunsMillis(30000);
        p.setMinEvictableIdleTimeMillis(30000);         
        p.setRemoveAbandonedTimeout(60);
        p.setLogAbandoned(false);
        p.setRemoveAbandoned(false);   p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

        final DataSource datasource = new DataSource();
        datasource.setPoolProperties(p);
        return datasource;        
    }
------------------------------------

This is the exception showing up in the log file:

------------------------------------

2012-11-23 14:53:48,109 [pool-2-thread-1] ERROR org.apache.tomcat.jdbc.pool.DataSource  - Unable to register JDBC pool with JMX
java.lang.NullPointerException
        at org.apache.tomcat.jdbc.pool.DataSource.registerJmx(DataSource.java:135)
        at org.apache.tomcat.jdbc.pool.DataSource.preRegister(DataSource.java:102)
        at org.apache.tomcat.util.modeler.BaseModelMBean.preRegister(BaseModelMBean.java:1132)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.preRegister(DefaultMBeanServerInterceptor.java:1007)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:919)
        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 org.apache.tomcat.util.modeler.Registry.registerComponent(Registry.java:742)
        at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1062)
        at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:671)
        at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:270)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:958)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1599)
        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)
Comment 1 Keiichi Fujino 2012-12-04 06:37:37 UTC
Thanks for the report.
The cause of this NPE is not initializing connection pool.
You can avoid this problem by calling dataSource.createPool().

Anyway, I am going to apply your patch. 
And it will be included in 7.0.34 onwards.