Failed to look up JNDI DataSource - java

This problem may seems redundant but I can't find a suitable solution for my specific problem.
So, I have been using basic JDBC template to make connection to the DB tables in my spring boot application. I have few more features to add and I wanted to follow JPA route. I added spring.datasource.jndi-name and spring.jpa.database-platform to the application.properties file correctly(same as once that are working in my other applications).
Although I'm able to run the current application(with both JPA and JDBC connection) locally, I'm getting following error while building it on weblogic remote server.
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 'jdbc.myDataSource'; 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
Can anyone help me with this ?

Related

Spring Boot & ActiveMQ Artemis: NoClassDefFoundError during start of application

When trying to create a Spring Boot Java application with ActiveMQ Artemis I get the following exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'artemisConfiguration' defined in class path resource [org/springframework/boot/autoconfigure/jms/artemis/ArtemisEmbeddedServerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.activemq.artemis.core.config.Configuration]: Factory method 'artemisConfiguration' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/activemq/artemis/api/core/QueueConfiguration
From the above exception I have difficult to understand what config might be missing or is it something else? I am using Spring Boot 2.7.3.
It is implemented to the letter as described in the this guide (native and embedded).
The fundamental problem is this:
java.lang.NoClassDefFoundError: org/apache/activemq/artemis/api/core/QueueConfiguration
This means that the JVM can't find this class. This class is in the artemis-commons module. You need to put this on your application's classpath.

JNDI #Resource in Spring

I have a Java EE application with WildFly, and I use:
#Resource(lookup = "java:/jms/queue/MyQueue")
but now I am migrating part of this application to Spring one, and I have no idea how to look up for my JNDI resource.
In application.yaml I added:
spring:
datasource:
jndi-name: "java:jms/queue/MyQueue"
but it throws an error during startup:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRoute': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'java:/jms/queue/MyQueue' defined in JNDI environment: JNDI lookup failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial
MyRoute.java:
#Resource(lookup = "java:/jms/queue/MyQueue")
private Queue queue1;
Is it possible to easily inject the resource in Spring?
I wish it can work like #Autowired does. Inject the Queue with lookup into JNDI.

Error creating bean with name 'dataSource' [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]

i'm getting the following error when i try to debug a Spring boot with Gradle project in Eclipse
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/DataSourceConfiguration$Hikari.class]
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
i've searched a lot, and most of the solutions came from something that is wrong in the application.properties file, but i can not figure what is wrong in mine
server.port=8080
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/dices?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
Any suggestion?
thanks
Can you try adding compile("mysql:mysql-connector-java") to your build.gradle and check again?
The problem was with eclipse which was not downloading the dependencies. Gradle configuration files were ok

Unable to deploy spring boot jar application

I'm developping a spring boot application and it works fine when I run the command:
mvn spring-boot:run
But when deploying jar file with the command:
java -jar target\jarfile.jar
I get the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeResource': Unsatisfied dependency expressed through field 'hrServicesSilo'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hrServicesSilo': Unsatisfied dependency expressed through field 'employeeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeService': Unsatisfied dependency expressed through field 'employeeRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.intranet.si.model.hr.Employee
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]
What am I doing wrong?
I can see a repository so I assume you use spring-data. It is strange that it works in maven and not working after packaging so you can try the following:
Check that you use spring-maven-plugin to package your application.
Try to add #EntityScan("com.intranet.si.model.hr") (can also be com.intranet.si.model to recursivly catch all models in the package and sub-packages) to one of your configuration classes or to the application class (the one annotated with #SpringBootApplication). This will notify spring + hibernate where to scan for entities. Also make sure your entity is annotated with #Entity.
I guess that you already have #EnableJpaRespotories in one of your configuration files but make sure it is configured as well.

Spring Boot with REST & JPA, building WAR

I am attempting to this tutorial: http://spring.io/guides/tutorials/bookmarks/
I have successfully created a Rest Controller and run it through eclipse, and I'm able to get a JSON response in the browser. I have used an Apache Derby embedded database.
What I'd like to do now is run it on my separate Tomcat instance (not embedded), with a JNDI lookup to a MySQL database.
The tutorial mentioned above does not mention JNDI, so I looked here instead, which suggested adding
spring.datasource.jndi-name=java:comp/env/jdbc/mydb
to the application.properties file.
I'm pretty sure I have Tomcat set up correctly, as I've used the JNDI lookup before in another app, but attempting to build the WAR using Maven fails:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; 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:comp/env/jdbc/finance'; 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
....
It seems the tutorials are not complete, or have I missed something? I'm at a loss as to how to proceed.

Categories