How to load datasource from hibernate.cfg.xml file when launching spring boot app - spring-boot

Intro :
My app contains 2 datasource, i used tomcat context.xml to configure them and it's working fine,
but now i need to launch the app as a spring-boot application,
one of datasource which is declared in hibernate.cfg.xml file doesn't seem to be loaded properly
What i tried:
i created a bean for the second data source which is in hibernat.cfg.xml file (same as i did for the first datasource) in a class annotated #configuration but i got this error:
Error parsing JNDI name [java:/comp/env/jdbc/mySecondDB]
i tried also to modify the jndi value to : java:comp/env/jdbc/mySecondDB
but it's not working
how i can load this 2nd datasource from the hibernate.cfg.xml file ?

Related

When I define a datasource ,what's the different between server.xml datesource and application.properties

My application runs on liberty application server, using springboot .And I define a datasource in server.xml file , and also I define the datasource in application.properties.
What's the different between the two file?

Spring boot provided "spring.datasource.password" can change to "spring.ds.pwd" without creating new bean for DataSource

My team is using latest spring boot for one of the project.
Spring provides database configuration in application.properties file and for password this is the key "spring.datasource.password"
Now, we want to change this to "spring.ds.pwd", but, the change should not required to create new Bean for datasource is it possible?
I followed below link, but, here new beans are creating to handle the session, which should not be expected. Other search information is also similar.
https://dzone.com/articles/spring-boot-jpa-mysql-sample-app-code-example
I was able to achieve what I want by overriding the DataSourceProperties java class with same package name.

Using #ConfigurationProperties in Spring Boot Application doesn't work

I am using Spring Boot V 1.4.1 for a new application.
My app requires two JDBC data sources and I was following the example at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources how to set it up.
My Spring beans configuration class is annotated with #EnableConfigurationProperties and my first bean is defined as
#Primary
#Bean
#ConfigurationProperties(prefix = "first.database")
DataSource qivsDB() {
return DataSourceBuilder.create().build();
}
, the second one accordingly. My application.properties file has properties defined like
first.database.url=jdbc:[redacted]
first.database.username=[redacted]
first.database.password=[redacted]
For reasons I not transparent to me during debugging this is failing to initialize: Cannot determine embedded database driver class for database type NONE - debug showed me that the builder does not have any properties set when calling build().
What did I miss here?
Before you do all the debugging part, you should have a look to the auto-configuration report. If you define your own DataSource there's no reason for Spring Boot to start looking at what it can do for your app. So, for some reasons, that definition of yours is not applied in your app and the default in Spring Boot still applies, doesn't find any JDBC url in the default namespace and attempt to start an embedded database. You should see in the auto-config report that the DataSourceAutoConfiguration still matches.
I am not sure the public keyword has anything to do with it, though you won't get custom meta-data for that key since we only scan for public methods.

SpringBoot DataSource configuration

I'm trying to use the application.properties file to configure the datasource Spring Boot will have to use.
I've put the following properties inside :
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.user=test
spring.datasource.password=test
spring.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/test
The application.properties file is well used by other systems. But I can't get it to work for the automatic datasource configuration.
I'm still getting this Exception :
org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database url for database type NONE.
The postgresql driver is included and loaded. And I can configure the datasource using a Configuration class, and the same parameters as above.
I've also added the #EnableAutoConfiguration and #EnableJpaRepositories to my Application.class.
Any clues?
You should use spring.datasource.url to configure the JDBC URL rather than spring.datasource.jdbcUrl.
spring.datasource.jdbcUrl will work if the specific DataSource implementation that you're using has a setJdbcUrl method (HikariCP, for example) where as spring.datasource.url will work with any of the supported datasources.
Using spring.datasource.url also has the added benefit that you don't need to specify spring.datasource.driverClassName as it will be inferred from the url.

IllegalStateException No LoadTimeWeaver available

I'm trying to get load time working in a Tomcat server defined inside STS.
I've added the load time weaver to the application context:
context:load-time-weaver/>
(I've tried as well)
context:load-time-weaver weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
The project contains the required jars (spring-aop.jar, aspectjweaver.jar) and the instrument jar has been copied to the tomcat/lib (spring-instrument-tomcat-4.0.2.RELEASE.jar).
The context.xml inside the Servers project contains:
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
(I've tried in the tomcat/conf/context.xml and in the META-INF/context.xml as well)
When I start the server I get the error "java.lang.IllegalStateException: No LoadTimeWeaver available"
Stepping in AspectJWeavingEnabler:enableAspectJWeaving, the method that throws the exception, the bean class loader has been correctly instantiated to TomcatInstrumentableClassLoader, but the LoadTimeWeaver is null. I believe the LoadTimeWeaver should have been injected by the application context "context:load-time-weaver". The instantiated TomcatInstrumentableClassLoader seems to suggest that both the spring-instrument-tomcat-4.0.2.RELEASE.jar and the loaderClass entry in the context.xml have been correctly picked up.

Resources