spring boot - SpringBoot JNDI datasource throws java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory -


similar questions have been asked before , went through of not able solve problem. related questions - q1,q2,q3, q4, q5, q6

i have spring batch project spring boot , trying use db connection pools. using embedded tomcat container version 8.5.x.

everything works fine if use application.properties specify data source , pool settings.

but when try use jndi, exception -

caused by: java.lang.classnotfoundexception: org.apache.tomcat.dbcp.dbcp2.basicdatasourcefactory 

i don't see jar names tomcat-dbcp-** in maven jars not sure if need include new dependency or need set default data source factory , how go it.

below jndi beans set up, question. have blanked out values.

@bean     public tomcatembeddedservletcontainerfactory embeddedservletcontainerfactory(){         return new tomcatembeddedservletcontainerfactory() {              @override             protected tomcatembeddedservletcontainer gettomcatembeddedservletcontainer(                     tomcat tomcat) {                 tomcat.enablenaming();                 return super.gettomcatembeddedservletcontainer(tomcat);             }              @override             protected void postprocesscontext(context context) {                 contextresource resource = new contextresource();                 resource.setname("jdbc/mydatasource");                 resource.settype(datasource.class.getname());                 resource.setproperty("driverclassname", "com.ibm.db2.jcc.db2driver");                 resource.setproperty("url", "url");                 resource.setproperty("username", "user");                 resource.setproperty("password", "*****");                 context.getnamingresources().addresource(resource);             }         };     }      @lazy     @bean(destroymethod="")     public datasource jndidatasource() throws illegalargumentexception, namingexception {         jndiobjectfactorybean bean = new jndiobjectfactorybean();         bean.setjndiname("java:comp/env/jdbc/mydatasource");         bean.setproxyinterface(datasource.class);         bean.setlookuponstartup(false);         bean.afterpropertiesset();         return (datasource)bean.getobject();     } 

my pom.xml

<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelversion>4.0.0</modelversion>      <packaging>war</packaging>      <groupid>***</groupid>     <artifactid>***</artifactid>     <version>1.0.0</version>      <parent>         <groupid>org.springframework.boot</groupid>         <artifactid>spring-boot-starter-parent</artifactid>         <version>1.4.0.release</version>     </parent>      <dependencies>         <dependency>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-starter-batch</artifactid>             <exclusions>                 <exclusion>                     <groupid>org.springframework.boot</groupid>                     <artifactid>spring-boot-starter-logging</artifactid>                 </exclusion>             </exclusions>         </dependency>          <dependency>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-starter-log4j</artifactid>             <version>1.2.1.release</version>         </dependency>             <dependency>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-starter-web</artifactid>         </dependency>         <dependency>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-starter-tomcat</artifactid>             <scope>provided</scope>         </dependency>          <dependency>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-starter-jdbc</artifactid>         </dependency>          <dependency>             <groupid>db2</groupid>             <artifactid>db2jcc</artifactid>             <version>4.0</version>         </dependency>          <dependency>             <groupid>db2</groupid>             <artifactid>db2jcc_license_cu</artifactid>             <version>4.0</version>         </dependency>     </dependencies>      <build>          <plugins>              <plugin>                 <groupid>org.springframework.boot</groupid>                 <artifactid>spring-boot-maven-plugin</artifactid>                 <version>1.4.0.release</version>                 <executions>                     <execution>                         <goals>                             <goal>repackage</goal>                         </goals>                     </execution>                 </executions>             </plugin>             <plugin>                 <groupid>org.apache.maven.plugins</groupid>                 <artifactid>maven-compiler-plugin</artifactid>                 <version>3.1</version>                 <configuration>                 <source>1.7</source>                 <target>1.7</target>                 </configuration>             </plugin>         </plugins>     </build>  </project> 

i solved problem setting factory attribute in resource definition. resource.setproperty("factory", "org.apache.tomcat.jdbc.pool.datasourcefactory");

@bean public tomcatembeddedservletcontainerfactory embeddedservletcontainerfactory(){     return new tomcatembeddedservletcontainerfactory() {          @override         protected tomcatembeddedservletcontainer gettomcatembeddedservletcontainer(                 tomcat tomcat) {             tomcat.enablenaming();             return super.gettomcatembeddedservletcontainer(tomcat);         }          @override         protected void postprocesscontext(context context) {             contextresource resource = new contextresource();             resource.setname("jdbc/mydatasource");             resource.settype(datasource.class.getname());             resource.setproperty("factory", "org.apache.tomcat.jdbc.pool.datasourcefactory");             resource.setproperty("driverclassname", "com.ibm.db2.jcc.db2driver");             resource.setproperty("url", "url");             resource.setproperty("username", "user");             resource.setproperty("password", "*****");             context.getnamingresources().addresource(resource);         }     }; } 

as per tomcat 8 documentation, supposed automatically infer db pool factory type looking @ datasource type , somehow defaults dbcp factory , class not there in class path.

i guess issue can solved making tomcat-dbcp-** jars available not sure how spring boot or if possible spring boot.

what find weird spring boot not including tomcat-dbcp dependencies part of starter pom using dbcp datasource factory default factory.


Comments