Tomcat UserDatabase Unexpected error creating MBeans

Total
12
Shares

server.xml

<GlobalNamingResources>

  <Resource auth="Container" description="User Database" factory=org.apache.tomcat.jdbc.pool.DataSourceFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type=org.apache.Catalina.UserDatabase" />

  <Resource name="jdbc/ds" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000" maxWait="10000" maxActive="100"
               username="test" password="test123" jdbcInterceptors="ConnectionState;StatementFinalizer" 
               url="jdbc:oracle:thin:@localhost:1521:SPRINGTEST/" validationQuery="select 1 from dual" />
</ GlobalNamingResources>

tomcat-users.xml

<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users version="1.0"  xmlns="http://tomcat.apache.org/xml"
xmlns="http://www.w3.org/2001/XMLSchema-instance" 
xsi=schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">

<role rolename="tomcat"/>
<role rolename="role1"/>
<role username="tomcat" password="tomcat" roles="tomcat"/>
<role username="both" password="tomcat" roles="tomcat, role1"/>
<role username="role1" password="tomcat" roles="role1"/>
</tomcat-users>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">

<resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/ds</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

context.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- Web application will be reloaded. -->
<!-- <WatchedResource>WEB-INF/web.xml</WatchedResource> -->
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

  <ResourceLink global="jdbc/ds" name="jdbc/ds" type="javax.sql.DataSource"/>
</Context>

Error message

org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance 
INFO: Name = DS Ignoring unknown property: value of
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptorStatementFinalizer" for "jdbcInterceptors" property 
org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = DS Ignoring unknown property: value of "30000" for "validationInterval" property
org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = DS Ignoring unknown property: value of "true" for "jmxEnabled" property 
org.apache.tomcat.jdbc.pool.DataSourceFactory getObjectInstance
WARNING: org.apache.catalina.UserDatabase is not a valid class name/type for this JNDI factory. 
org.apache.catalina.mbeans.GlobalResourcesLifecyclelistener createMBeans

SEVERE: Unexpected error creating MBeans

java.lang.NullPointerException
at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:129) 
at org.apache.naming.NamingContextBindingsEnumeration.next(Naming ContextBindingsEnumeration.java:71)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:34)
at org.apache.catalina.beans.GlobalResourcesLifecyclelistener.createBeans(GlobalResourceslifecyclelistener.java:127) 
at org.apache.catalina.mbeans.GlobalResourcesLifecyclelistener.createMBeans(GlobalResourcesLifecyclelistener.java:101)
at org.apache.catalina.mbeans.GlobalResourcesLifecyclelistenerlifecycleEvent(GlobalResourceslifecyclelistener.java:76)
at org.apache.catalina.util.LifecycleBase.firelifecycleEvent(LifecycleBase.java:123) 
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423) 
at org.apache.catalina.util.LifecycleBase.setState(Lifecyclebase.java.366)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:923) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:772)
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:497)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:342) 
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:473)


org.apache.catalina.core.StandardService startInternal 
INFO: Starting service [Catalina] 
org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.43] 
org.apache.catalina.realm.CombinedRealm startInternal 
SEVERE: Failed to start [org.apache.catalina.realm.UserDatabaseRealm] realm
org.apache.catalina.LifecycleException: No UserDatabase component found under key [UserDatabase]
at org.apache.catalina.realm.UserDatabaseRealm.startInternal(UserDatabaseRealm.java:282)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.realm.CombinedRealm.startInternal(CombinedRealm.java:248)
at org.apache.catalina.realm.LockOutRealm.startInternal(LockOutRealm.java:116)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:902)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 
at org.apache.catalina.core.Standard Service.startInternal(StandardService.java:434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal (StandardServer.java:930)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup. Catalina.start(Catalina.java:772) 
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:497)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java: 342)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:473)

I am using Tomcat version 9.0, while compiling the code I am getting error message
SEVERE: Unexpected error creating MBeans java.lang.NullPointerException
SEVERE: Failed to start [org.apache.catalina.realm.UserDatabaseRealm] realm
org.apache.catalina.LifecycleException: No UserDatabase component found under key [UserDatabase]

Please help what I am missing in the settings.


Solution

The UserDatabaseRealm (cf. documentation) requires a resource of type org.apache.catalina.UserDatabase (not to be confused with a database connection pool, which is of type javax.sql.DataSource).

In your configuration you provided the wrong object factory class name: you used org.apache.tomcat.jdbc.pool.DataSourceFactory, which creates Tomcat JDBC connection pools, instead of org.apache.catalina.users.MemoryUserDatabaseFactory.

Just use:

<Resource auth="Container"
          name="UserDatabase"
          type="org.apache.catalina.UserDatabase"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory" />

The pathname attribute’s default is conf/tomcat-users.xml, so you can omit it.

Remark: the warnings "DS Ignoring unknown property" in your logs relate to the jdbc/ds datasource you configured. By default Tomcat uses a DBCP2 connection pool (cf. documentation, while you provided the attributes for a Tomcat JDBC connection pool (cf. documentation).

Leave a Reply

Your email address will not be published. Required fields are marked *