Spring Boot Datasource annotated with ConfigurationProperties but the properties are not loaded - java

I am trying to wire datasource to get properties from application(yml) file but the Datasourcebuilder is not reading those properties. I referred Stackoverflow as well as Spring Boot docs but could not see anything missing in my code.
I am pasting the code below that uses Spring Boot 1.4.3.RELEASE
#SpringBootApplication
#EnableConfigurationProperties
#ComponentScan
public class MyApplication {
#Bean(name="dmDs")
#Primary
#ConfigurationProperties("spring.datasource")
public DataSource dmDataSource(){
return DataSourceBuilder.create().build();
}
#Bean
public String aBean(){
DataSource ds = dmDataSource(); // creates a datasource with URL, username and password empty.
return new String("");
}
The application config file is as shown below:
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
- org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
profiles:
active: test
---
spring:
profiles: test
datasource:
url: jdbc:oracle:thin:SOME_URL
driver-class-name: oracle.jdbc.OracleDriver
password: test
username: test
datacollector:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:#SOME_URL
username: user
password: pass
I see in the logs that the properties are read from the application.yml file
[main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.url' in [applicationConfig: [classpath:/application.yml]] with type [String]
[main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.driver-class-name' in [applicationConfig: [classpath:/application.yml]] with type [String]
[main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.password' in [applicationConfig: [classpath:/application.yml]] with type [String]
[main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.username' in [applicationConfig: [classpath:/application.yml]] with type [String]
JdbcTemplateAutoConfiguration matched:
- #ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.core.JdbcTemplate' (OnClassCondition)
- #ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) found a primary bean from beans 'cipDs', 'dmDs' (OnBeanCondition)
I am running the application as shown below:
public static void main(String[] args){
SpringApplication.run(new Object[]{DecisionManagementApplication.class,ApplicationConfig.class}, args);
}

If you want to use the bean Spring has created within its container you need to inject it, you can not use "new".
Try:
#Bean
#Autowired
public String aBean(final DataSource myDS)
{
return new String("Check myDS properties now");
}

Related

Configuring hikari connection pool

I have following settings for my database (I have multiple databases, so they are configured in spring.datasource hierarchy.
spring:
datasource:
db-write:
url: jdbc:sqlserver://whatever.database.windows.net:1433;database=dbname;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
username: 'myusername'
password: 'mynotsosecretpassword'
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
Then I am configuring my datasource here
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(basePackages = "com.datasources.dbwrite.repository",
entityManagerFactoryRef = "entityManagerFactoryDbWrite",
transactionManagerRef= "transactionManagerDbWrite"
)
public class DataSourceConfigurationDbWrite {
#Bean
#Primary
#ConfigurationProperties("spring.datasource.db-write")
public DataSourceProperties dataSourcePropertiesDbWrite() {
return new DataSourceProperties();
}
#Bean
#Primary
#ConfigurationProperties("spring.datasource.db-write.configuration")
public DataSource dataSourceDbWrite() {
return dataSourcePropertiesDbWrite().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
#Primary
#Bean(name = "entityManagerFactoryDbWrite")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryDbWrite(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(dataSourceDbWrite())
.packages("com.datasources.dbwrite.models")
.build();
}
#Primary
#Bean
public PlatformTransactionManager transactionManagerDbWrite(
final #Qualifier("entityManagerFactoryDbWrite") LocalContainerEntityManagerFactoryBean entityManagerFactoryDbWrite) {
return new JpaTransactionManager(Objects.requireNonNull(entityManagerFactoryDbWrite.getObject()));
}
}
I am configuring my hikari datasource in dataSourceDbWrite method based on the properties i read in dataSourcePropertiesDbWrite method. I believe i need to configure properties in specific hierarchy so that dataSourceDbWrite method can easily detect which properties are needed for hikari. Is that correct?
What that hierarchy would be?
Moreover, how can and where can i find what properties i can configure for hikari? connection-timeout? connection pool size etc?
Me personally prefer application.yml than code to configurate Hikari:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: {JDBC URL}
username: {USERNAME}
password: {PASSWORD}
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 5
idle-timeout: 600000
maximum-pool-size: 10
auto-commit: true
pool-name: HikariCorePool
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: select * from information_schema.tables limit 1
(BTW, that piece of configuration was originally writen by a colleague years ago. We didn't change it and just copy-and-paste into any new projects those years.😆)
If you want to check out all configurable fields, those spring.datasource.hikari.* keys inorg.springframework.boot:spring-boot-autoconfigure:{VERSION}/META-INF/spring/spring-configuration-metadata.json may could help.
And javadoc in com.zaxxer.hikari.HikariConfigMXBean could help too.
See example in article, the properties hierarchy are according to #ConfigurationProperties's value
If we want to configure Hikari, we just need to add a #ConfigurationProperties to the data source definition:
#Bean
#ConfigurationProperties("spring.datasource.todos.hikari")
public DataSource todosDataSource() {
return todosDataSourceProperties()
.initializeDataSourceBuilder()
.build();
}
Then, we can insert the following lines into the application.properties file:
spring.datasource.todos.hikari.connectionTimeout=30000
spring.datasource.todos.hikari.idleTimeout=600000
spring.datasource.todos.hikari.maxLifetime=1800000
See relevant hikari's spring properties
spring.datasource.hikari.connection-timeout
spring.datasource.hikari.data-source-class-name
spring.datasource.hikari.data-source-properties
spring.datasource.hikari.driver-class-name
spring.datasource.hikari.idle-timeout
spring.datasource.hikari.initialization-fail-timeout
spring.datasource.hikari.jdbc-url
spring.datasource.hikari.leak-detection-threshold
spring.datasource.hikari.login-timeout
spring.datasource.hikari.max-lifetime
spring.datasource.hikari.maximum-pool-size
spring.datasource.hikari.minimum-idle
spring.datasource.hikari.validation-timeout
And explanation on each property in HikariCP, for example
connectionTimeout
This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. Lowest acceptable connection timeout is 250 ms. Default: 30000 (30 seconds)
Notice that camelCase hikari properties (connectionTimeout) is shown as snake-case in spring (connection-timeout)

setup alternate datasource in spring boot application causes liquibase to fail

Application has a default spring data source specified in application.yml
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:oracle:thin:#localhost:1521:xe
username: system
password: oracle
hikari:
poolName: Hikari
auto-commit: false
I have added configuration options for a second data source, used for a completely difference (JDBCTemplate purpose).
faas20:
ds:
url: jdbc:oracle:thin:#tldb0147vm.group.net:1760:tdb
username: ...
password: ...
Then, I add two data sources, one named, and the other default. Without the default one, liquibase fails to start.
#Configuration
public class LegacyConfiguration {
#Bean(name = "faas20")
#ConfigurationProperties(prefix = "faas20.ds")
public DataSource legacyDataSource() {
return DataSourceBuilder
.create()
.build();
}
#Bean
public DataSource defaultDataSource() {
return DataSourceBuilder
.create()
.build();
}
}
Startup of the application fails though.
The application now cannot build the default EntityManagerFactory.
Why would that be affected?
Parameter 0 of constructor in impl.OrderServiceImpl required a bean named 'entityManagerFactory' that could not be found.
Consider defining a bean named 'entityManagerFactory' in your configuration.
Without the two data sources present the application and liquibase start up as they should.
edit
I am not clear on how to configure two separate data sources,
Default Data Source for JPA
Additional Data Source for use in JDBC (and potentially other JPA classes)

Spring boot doesn't read DB configuration from yml file

I have this config file:
liquibase:
enabled: true
change-log: classpath:/db/changelog/db.changelog-master.yml
management:
security:
enabled: false
server:
port: 8080
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:file:./target/h2db/db/develop;DB_CLOSE_DELAY=-1
username: sa
password:
h2:
console:
enabled: true
When I try to check datasorce it connects to "jdbc:h2:mem:testdb" anyway:
#Component
public class AfterInit implements CommandLineRunner {
#Autowired
DataSource dataSource;
#Override
public void run(String... args) throws Exception {
out.println(dataSource);
}
}
Why spring boot can't find proper database config?
When I changed dependency from runtime("com.h2database:h2") to compile("com.h2database:h2") Datasource became work.
Can anybody explain me what's happend?

spring boot not using consul properties

I have an application annotated like this
#SpringBootApplication(exclude = {DefaultNotificationServiceConfig.class})
#EnableDiscoveryClient
#EnableSwagger2
#EnableSpringDataWebSupport
#EnableJpaRepositories(basePackages = {"com.repositories.jpa"})
#EnableMongoRepositories(basePackages = {"com.repositories.mongo"})
public class DemoApplication {
private static ApplicationContext ctx;
public static void main(String[] args) {
ctx = SpringApplication.run(new Object[]{DemoApplication.class}, args);
System.out.println(ctx.getEnvironment());
}
}
I want it to use consul, and it is getting the active profiles from consul because I see this line in the logs when starting it
2016-09-19 20:38:29.947 INFO 9556 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='consul', propertySources=[ConsulPropertySource [name='com/app/'], ConsulPropertySource [name='com/application/']]]
2016-09-19 20:38:29.975 INFO 9556 --- [ main] com.Application : The following profiles are active: dev,im3
However its not using the mongo and mysql databases I've specified in the dev profile. Its trying to connect to a mongo db at localhost which is not the one in my properties
Caused by: com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017
and liquibase is trying to use a default database not listed in my properties
INFO 9/19/16 8:38 PM: liquibase: Creating database history table with name: PUBLIC.DATABASECHANGELOG
Here is the dev profile from consul
server:
port: 8090
spring:
redis:
host: ubuntu
port: 6379
data:
mongodb:
uri: mongodb://ubuntu:27017/test
datasource:
url: jdbc:mysql://ubuntu:3309/test
username: uesr
password: password
driverClassName: com.mysql.jdbc.Driver
testOnBorrow: true
validationQuery: select 1
multipart:
maxFileSize: 250MB
maxRequestSize: 250MB
I have another spring boot application pointing to the same consul instance and using the same profiles and that one is working. I also have bootstrap.yml in my classpath
spring:
application:
name: ${APPLICATION_NAME:app}
cloud:
consul:
host: ${CONSUL_HOST:localhost}
port: ${CONSUL_PORT:8500}
config:
format: YAML
watch:
delay: 10
prefix: app
I'm using eclipse and in the run configurations I'm using a main class from a dependent project when starting the app that looks like below, but the project I am running from is the project containing the application class above.
#SpringBootApplication(exclude = {DefaultNotificationServiceConfig.class})
#EnableSpringDataWebSupport
#EnableDiscoveryClient
#EnableSwagger2
#EnableJpaRepositories(basePackages = {"com.repositories.jpa"})
#EnableMongoRepositories(basePackages = {"com.repositories.mongo"})
public class Application {
private static ApplicationContext ctx;
public static void main(String[] args) throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
ctx = SpringApplication.run(new Object[]{Application.class}, args);
System.out.println(ctx);
}
}
Update
The app that's working states the profiles being used in the log
2016-09-19 23:53:55.265 INFO 9744 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='consul', propertySources=[ConsulPropertySource [name='com/app,im3/'], ConsulPropertySource [name='com/app,dev/'], ConsulPropertySource [name='com/app/'], ConsulPropertySource [name='com/application,im3/'], ConsulPropertySource [name='com/application,dev/'], ConsulPropertySource [name='com/application/']]]
I have 3 config files in consul, com/app and com/app,dev and com/app,im3. com/app looks like this
server:
session:
timeout: 600
spring:
profiles:
active: dev,im3
jpa:
hibernate:
ddl-auto: none
naming-strategy: com.config.OracleNamingStrategy
show-sql: false
thymeleaf:
mode: LEGACYHTML5
logging:
level:
org:
springframework: ERROR
thymeleaf: ERROR
hibernate: ERROR
springfox: ERROR
com:
bosch:
mercurio: DEBUG
Stupid mistake, I defined the environment variables but not the VM argument
-Dspring.profiles.active=dev,im3
it doesn't work if I remove
spring:
profiles:
active: dev,im3
from com/app, it seems redundant that I need both

Spring Boot with two datasources: How to setup unique resource names?

As pointed out here, you can define two Datasources in Spring Boot in the following way:
#first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver
#second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver
#Bean
#Primary
#ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
#Bean
#ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
This seems to work fine. But when you are doing Atomikos XA-Transactions, each datasource has to has a unique resource name for the case that a recovery is necessary.
The Boot documentation defines a property for this:
spring.jta.atomikos.datasource.unique-resource-name=dataSource # The unique name used to identify the resource during recovery.
How do I provide a unique-resource-name for the primary and a different one for the secondary resource in Spring Boot?
Looks to me like XA-Transactions are supported in Boot, but only between JMS and DB ressources...

Categories

Resources