2

when I am trying to get data source using JNDI, from tomcat location conf/Catalina/localhost/{warname}.xml the following error I have facing. please help me, guys.

Error I Facing

    org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource [com/dummycoding/users/config/UsersDBConfig.class]: 
Bean instantiation via factory method failed;
 nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [javax.sql.DataSource]:
 Factory method 'dataSource' threw exception; 
nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException:
 Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/usersprofile'; 
nested exception is javax.naming.NoInitialContextException:
 Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  
java.naming.factory.initial

MyApplicatin.properties

spring.datasource.jndiName=java:comp/env/jdbc/usersprofile
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update

UserDBConfig.class

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;

@Configuration
public class UsersDBConfig {

    @Bean
    public DataSource dataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/usersprofile");
        return dataSource;
    }
}

My application class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UsersApplication {

    public static void main(String[] args) {
        SpringApplication.run(UsersApplication.class, args);
    }

}

my Servelet Initializer class

@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class})
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(UsersApplication.class);
    }

MyTomcat external data source xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/usersprofile">
 <Resource acquireIncrement="0" auth="Container" driverClass="com.mysql.cj.jdbc.Driver" factory="org.apache.naming.factory.BeanFactory"
 idleConnectionTestPeriod="60" initialPoolSize="0" jdbcUrl="jdbc:mysql://localhost:3306/dummy_users?autoReconnect=true"
 maxIdleTime="100" maxPoolSize="100" minPoolSize="1" name="jdbc/usersprofile" password="MySQL123$$" type="com.mchange.v2.c3p0.ComboPooledDataSource"
 user="root"/>
</Context>
Karthick Raj
  • 191
  • 3
  • 11
  • Possible duplicate of https://stackoverflow.com/questions/27479263/spring-boot-jndi-datasource-lookup-failure-name-comp-env-jdbc-not-found-in-con –  Dec 15 '19 at 06:16
  • Are you deploying your application as a war file to an external Tomcat or using embedded Tomcat? Also, you don’t need the `dataSource` bean method as you have configured `spring.datasource.jndiName`. – Andy Wilkinson Dec 15 '19 at 08:05
  • @AndyWilkinson, yes i am using external tomcat, please help me this out. – Karthick Raj Dec 15 '19 at 08:55
  • If you are deploying to external Tomcat, you’d need a [`SpringBootServletInitializer`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.html). I can’t see one in your question. Can you provide a [minimal, complete, and verifiable example](/help/mcve) of your problem? – Andy Wilkinson Dec 15 '19 at 08:59
  • can i share my project in githup, then you can verify my complete sample project. – Karthick Raj Dec 15 '19 at 09:00
  • @AndyWilkinson now i attached my servelet initializer class – Karthick Raj Dec 15 '19 at 09:06
  • Yes, please do share your whole project on GitHub. – Andy Wilkinson Dec 15 '19 at 09:52
  • @AndyWilkinson please find my sample project here https://github.com/TamanaKarthick/SpringBoot.git – Karthick Raj Dec 15 '19 at 10:00
  • HI @AndyWilkinson are you seen my Project, please tell me any idea, if you are free. – Karthick Raj Dec 15 '19 at 11:42
  • Why have you tried to exclude the DataSource and JPA auto-configurations? For the exclusion to take effect it needs to be on `UsersApplication`. You could use the `exclude` attribute of `@SpringBootApplication`. There's quite a bit in your app that doesn't relate to looking up a DataSource from JNDI and, unfortunately, I don't have time to work through the unrelated pieces to get to the problem you are asking about. I'd try stripping your problem down to the absolute bare minimum that is required to use a JNDI-based `DataSource` and see if you can get that working. – Andy Wilkinson Dec 15 '19 at 12:30
  • thanks for your response, @AndyWilkinson can you please share some related documents to complete this project or any paid course or free? that will helpful for me. – Karthick Raj Dec 15 '19 at 12:51

0 Answers0