Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HibernateSpec does not parse spring boot externalized configuration. #81

Closed
abcfy2 opened this issue Apr 2, 2018 · 1 comment
Closed
Assignees

Comments

@abcfy2
Copy link

abcfy2 commented Apr 2, 2018

Spring boot support Externalized Configuration

Like this:

dataSource:
    type: com.zaxxer.hikari.HikariDataSource
    pooled: true
    driverClassName: org.postgresql.Driver
    url: ${JDBC_URL:'jdbc:postgresql://127.0.0.1:5432/db?useUnicode=true&characterEncoding=utf8'}
    username: ${DB_USER:myuser}
    password: ${DB_PASS:mypass}
    dbCreate: none
    properties:
        maximumPoolSize: 100
        minimumIdle: 10

But HibernateSpec will throw this exception:

java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, ${JDBC_URL:'jdbc:postgresql://127.0.0.1:5432/db?useUnicode=true&characterEncoding=utf8'}
	at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:106)
	at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:342)
	at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:117)
	at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:107)
	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:97)
	at org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy.afterPropertiesSet(LazyConnectionDataSourceProxy.java:162)
	at org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy.<init>(LazyConnectionDataSourceProxy.java:106)
	at org.grails.datastore.gorm.jdbc.connections.DataSourceConnectionSourceFactory.proxy(DataSourceConnectionSourceFactory.java:95)
	at org.grails.datastore.gorm.jdbc.connections.DataSourceConnectionSourceFactory.create(DataSourceConnectionSourceFactory.java:88)
	at org.grails.datastore.gorm.jdbc.connections.CachedDataSourceConnectionSourceFactory.create(CachedDataSourceConnectionSourceFactory.java:37)
	at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:38)
	at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:23)
	at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:64)
	at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:52)
	at org.grails.datastore.mapping.core.connections.ConnectionSourcesInitializer.create(ConnectionSourcesInitializer.groovy:24)
	at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:206)
	at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:260)
	at grails.test.hibernate.HibernateSpec.setupSpec(HibernateSpec.groovy:58)

Seems that HibernateSpec does not resolve spring boot externalized configuration.

@benrhine
Copy link
Contributor

benrhine commented Jun 5, 2018

You are correct that currently the HibernateSpec does not resolve SpringBoot external configs as application.yml is intended to take the place of application.properties. The current HibernateSpec only reads application.yml and application.groovy.

With that being said I don't believe the error you posted is related. I believe that error is related to a syntax issue in your dataSource. You currently have the following

url: ${JDBC_URL:'jdbc:postgresql://127.0.0.1:5432/db?useUnicode=true&characterEncoding=utf8'}

but refering to the documentation here. You should have the following instead.

url: '${JDBC_URL:jdbc:postgresql://127.0.0.1:5432/db?useUnicode=true&characterEncoding=utf8}'

NOTE: The quote locations are different.

The above syntax is specifically for picking up command line arguments or system properties (ie. environment variables) NOT values from a properties file.

NOTE: It will only use the secondary string in the event JDBC_URL is null.

Now I think what you are actually try to get at is you want your application.yml to be capable of reading from application.properties. I have added an enhancement with the following pull request #90 that will allow you to use the older style of application.properties instead of either application.yml or application.groovy.

With HibernateSpec now reading application.properties this now allows when both application.propertes and say application.yml are present you can read values from the .properties in the .yml.

environments:
    test:
        dataSource:
            dbCreate: update
            url: '${test.dataSource.url}'

NOTE: application.properties must be located alongside other config files in the conf folder for this to work.

WARNING: Key names must be different in application.properties for this to work. If in application.yml you reference url: '${environments.test.dataSource.url}' it will view that as self referencing and will not retrieve the value from application.properties.

@benrhine benrhine closed this as completed Jun 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants