Spring data jpa repository integration test using JNDI datasource - spring

We are using JNDI datasource for spring data JPA repository datasource.
While writing integration tests, tried to create the JNDI datasource as below.
But still the
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
DriverAdapterCPDS cpds = new DriverAdapterCPDS();
cpds.setDriver("com.mysql.jdbc.Driver");
cpds.setUrl("jdbc:mysql://localhost:3306/test?autoReconnect=true");
cpds.setUser("");
cpds.setPassword("");
SharedPoolDataSource dataSource = new SharedPoolDataSource();
dataSource.setConnectionPoolDataSource(cpds);
dataSource.setMaxActive(10);
dataSource.setMaxWait(50);
builder.bind("jdbc/testDB", dataSource);
builder.activate();
Getting the below exception when running the maven JUnit Tests
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

Related

Unable to create retry-able datasource for spring boot jdbc

I would like to add the retry feature for database connection certain number of times until the app acquires it. For the I have used the spring-retry on the DataSource but it is not working. It is throwing the following error
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcTemplate' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.class]: Unsatisfied dependency expressed through method 'jdbcTemplate' 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]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ExistingValue must be an instance of com.zaxxer.hikari.HikariDataSource
I have seen the debug logs but those are not helpful. Here is the sample source code . kindly help
Note: the dependencies mentioned in build.gradle is required for my app. I have only extracted the retry part.
Your use-case is delaying the start of Spring Boot until your database is up. Spring actually ships with a component that does that. The DatabaseStartupValidator is that component and has existed since about Spring 1.x.
You can add it as a bean and it will wait for further bootstrapping until the database is up.
#Bean
public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) {
var dsv = new DatabaseStartupValidator();
dsv.setDataSource(dataSource);
return dsv;
}
For a more detailed explanation see this blog post of mine.

How to auto wire spring data repository in another spring based project (without spring data context)

I have two spring based projects, project-a with spring data and project-b simple spring based application with out spring data, project a is referenced in project b. In project b application context, i couldn't able to scan the spring data repositories present in project a using the annotation <jpa:repositories base-package="com.projecta"></jpa:repositories> it throws me below error.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.DatabaseException
[junit] Internal Exception: java.sql.SQLRecoverableException: IO Error: Connect identifier was empty.
Let me know if you have ever come across this.

Issue with spring cloud task mutiple datasources

I configured two data sources in my app following the guide of spring example:https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/multiple-datasources.
The spring boot version I use is :2.0.0.RELEASE
The spring.cloud.task.version I use is :1.2.2.RELEASE.
This application works fine in my local computer, But when deploy to the to AWS, I got the following error with the defination of class:CustomTaskConfigurer.java.
which defined the same as here:https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/multiple-datasources/src/main/java/io/spring/configuration/CustomTaskConfigurer.java
The error message is like below:
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.task.configuration.SimpleTaskConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customTaskConfigurer' defined in file [/home/vcap/app/BOOT-INF/classes/com/xxx/configuration/CustomTaskConfigurer.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xxx.configuration.CustomTaskConfigurer$$EnhancerBySpringCGLIB$$bc80cd46]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Unable to create a TaskExecutionDao.
The root cause for this error is that when I developed the application locally I configure a local datasource bean for postgresql like below:
#Bean
#Primary
#ConfigurationProperties("spring.datasource")
public HikariDataSource sourceDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
This bean read the properties in the application.properties file which identify the username and password url for the local postgres.
When This application deloyed to cloud,It will connect to the cloud data base instead of the local databases, which means the url,username,and password are no longer correct.
After add the configuration for cloud, this error disppeared.
But this exception stack trace only tell you that it is unable to create taskExecutionDao, It is really hard for the user to fix the issue when see such a error message
If it is a multiple datasource issue, you can try marking it as #Primary. Providing a better stack trace is helpful.

ClassCastException on #Resource injection of MQQueueConnectionFactory

I am trying to inject the MQQueueConnectionFactory defined as a JNDI resource into my spring configuration using #Resource. I am getting a ClassCastException while doing this.
I am really confused on how to solve this. I am using JDK7 and spring 4.1.6.RELEASE. The MQ client is installed in a standard way and exported to tomcat classpath.
[ERROR] [TokenId=] [2015-05-29 21:33:53.496] [DispatcherServlet] - [Context initialization failed]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messagingConfig': Injection of resource dependencies failed; nested exception is org.
springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'java:comp/env/jms/SSAJMSQueueConnectionFactory' must be of type [javax.jms.ConnectionFactory], but
was actually of type [com.ibm.mq.jms.MQQueueConnectionFactory]
When you see a ClassCastException which doesn't make sense, where you know for example that MQQueueConnectionFactory is an extension of ConnectionFactory, then it usually points to a classloader problem.
In most cases MQQueueConnectionFactory has been loaded with a different classloader to ConnectionFactory - this will cause a ClassCastException if you attempt to cast one to another even though you'd expect it to work.

Spring TestContext Framework

i tried to create a test for loading applicationContext.xml using Spring TestContext Framework,the code is pretty simple:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"/applicationContext.xml"})
public class ApplicationContextLoadingTest {
#Autowired
private ApplicationContext applicationContext;
#Ignore
#Test
public void testContext() {
}
}
when i run the test i got the error message:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pollInitializer' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
...
any one knows what that means? Thanks
It means exactly what it says: applicationContext.xml contains a bean named pollInitializer, this bean has an initialization method, that method tried to execute some Hibernate query, and the query failed with SQLGrammarException.
So, check Hibernate queries issued by that initialization method.
It means that spring is loading the file applicationContect.xml and is it is failing to setup the bean named pollInitalizer which is defined in that file.
With your test you are attemptinhpg to auto wire an ApplicationContext. That is not really normal. Normally you would auto wire some beans defined in the XML that you caused to be loaded. You don't have to work with the application context directly.

Resources