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>