1

I am trying to add postgres database connection to java spring application through JNDI. But while building the application, it shown an error "Error creating bean with name 'dataSource' defined in class path resource".

This is my application.properties file.

spring.datasource.jndi-name=java:/TestDb
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
#spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = none

NOTE: The application is build succesfully if change application.properties like below

spring.datasource.url=jdbc:postgresql://localhost:5444/test
spring.datasource.username=test
spring.datasource.password=test

The stacktrace is given.

 o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': 
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.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:/Test'; 
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

2021-04-01 13:14:12.383  INFO 14408 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-04-01 13:14:12.398 ERROR 14408 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': 
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.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:/Test'; 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
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:799) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:409) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
    
    ... 114 common frames omitted
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
user2765386
  • 27
  • 1
  • 6
  • 1
    When asking about errors you might want to post the stacktrace as well so we can look for the causes. Right now we only know that the bean could not be created... Also watch for errors or warnings that are logged before the one you're working on - those often are related and might be your actual root cause. – Thomas Apr 01 '21 at 07:40
  • That being said, who creates the datasource and puts it into JNDI? Note that postgres won't put itself there. – Thomas Apr 01 '21 at 07:42
  • The JNDI connection is already established with Jboss server. And when I test the connection, it is successfull – user2765386 Apr 01 '21 at 07:50
  • Note: when posting stacktraces please format them so that they are readable and not just a wall of text. Post them as a "code" block so it gets srollable. – Thomas Apr 01 '21 at 07:55
  • OK, so your JBoss exposes its JNDI but how does your Spring application know how to access it? From your application's perspective it is a _remote_ JNDI so you at least need to add configuration on where to find it (even if it is localhost). Also see here: https://stackoverflow.com/questions/45923971/spring-datasource-jndi-name-from-different-ip-server-with-jndi – Thomas Apr 01 '21 at 08:03
  • Also look here: [How do I lookup a JNDI Datasource from outside a web container?](https://stackoverflow.com/questions/2521421/how-do-i-lookup-a-jndi-datasource-from-outside-a-web-container/6296375) – Thomas Apr 01 '21 at 09:12

1 Answers1

0

Normally, you dont define a JNDI datasource inside your application, but in your application container. Spring uses an embedded Tomcat Server though, where JNDI is disabled by default.

The easiest way would be to use an external Application Server and modifying its configuration by adding a JNDI configuration. With embedded tomcat, datasource configuration should be done using the spring.datasource properties.

Here you can see how to configure an external Application Server (Tomcat): https://www.baeldung.com/spring-persistence-jpa-jndi-datasource

If you still want to use JNDI with embedded tomcat, you can see here how to do it: How to create JNDI context in Spring Boot with Embedded Tomcat Container

Manu
  • 284
  • 2
  • 20