Recently we upgraded to spring boot 2.2.5
We used to use the property spring.datasource.data to load SQL after "create" or "update" of the database.
Our Yaml looked like this:
--
spring:
profiles: default
application:
name:"EMBEDDED"
datasource:
url: "jdbc:postgresql://localhost/somedb"
driverClassName: org.postgresql.Driver
username: postgres
password: "some password"
data: "classpath*:data.sql"
jpa:
database: POSTGRESQL
show-sql: false
hibernate.ddl-auto: update
The important part is the "spring.datasource.data" property where we used to define a name of a SQL file that hibernate would run after updating the DB.
Note that we use the hibernate.ddl-auto to make that happen.
Now we have upgraded the spring boot and we also started using 2 data-sources as follows:
spring:
profiles: dev
application:
name:"EMBEDDED-DEV"
datasource:
src1:
jdbcUrl: "jdbc:sqlserver://localhost;databaseName=DB1"
username: user
password: password
initializationFailTimeout: 0
data: "classpath*:data.sql"
src2:
jdbcUrl: "jdbc:sqlserver://localhost;databaseName=db2"
username: user
password: password
initializationFailTimeout: 0
data: "classpath*:data.sql"
jpa:
database-platform: org.hibernate.dialect.SQLServer2012Dialect
show-sql: false
hibernate.format_sql: true
hibernate.ddl-auto: update
As you can see we have now 2 data sources, but when i try to add the "data" property as i used to do before i get this exception:
java.lang.RuntimeException: Property data does not exist on target class com.zaxxer.hikari.HikariConfig
Where should i put now the data property in order for it to work?
Related
In my spring boot application, I have multiple profiles as below:
application.yml
application-dev.yml
application-local.yml
application-qa.yml
application-prod.yml
Each profile will have its own datasource configured. For testing purpose, same datasource configuration are passed in all profiles.
In application.yml if I mention spring.profiles.active=dev it is connecting to DB successfully. But if I mention spring.profiles.active=local then on server startup local profile is getting active with below error
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (the profiles lcl are currently active).
Below configuration passed in all profiles excluding application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
password: root
url: jdbc:mysql://localhost:3306/local_db?useSSL=false
username: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
liquibase:
change-log: classpath:db/changelog/db.changelog-master.json
When trying to start a Spring Boot application for tests (with H2 database) with R2DBC and Liquibase configured, I get the following error:
2022-10-04 12:50:18.893 INFO 57774 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 67 ms. Found 2 R2DBC repository interfaces.
org.h2.message.DbException: Log file error: "/.testdb.trace.db", cause: "java.nio.file.FileSystemException: /.testdb.trace.db: Read-only file system" [90034-214]
Here's my config:
spring:
liquibase:
change-log: classpath:liquibase/db.changelog.xml
contexts: production
url: jdbc:h2:file:///./.testdb;MODE=MySQL
r2dbc:
url: r2dbc:h2:file:///./.testdb
h2:
console:
enabled: false
Is there anything I can do to fix that error?
The problem was caused by the URL, R2DBC requires it to look like this:
r2dbc:
url: r2dbc:h2:file:///./.testdb
But then if you try to copy it over to Liquibase part, the error appears. To fix that, remove forward slashes from Liquibase URL:
spring:
liquibase:
change-log: classpath:liquibase/db.changelog.xml
contexts: production
url: jdbc:h2:file:./.testdb;MODE=MySQL
Final version:
spring:
liquibase:
change-log: classpath:liquibase/db.changelog.xml
contexts: production
url: jdbc:h2:file:./.testdb;MODE=MySQL
r2dbc:
url: r2dbc:h2:file:///./.testdb
h2:
console:
enabled: false
Even though my application.properties looks fine when I go to
http://localhost:8080/h2-console I still get the whitelabel error page
My application.properties :
server:
port: 8080
Spring:
h2:
console:
enabled: true
path: /h2-console
onsole.settings.trace: false
scpring.h2.console.settings.web-allow-others: false
datasource:
url: jdbc:h2:mem:testdb
username: sa
password:
driverClassName: org.h2.Driver
jpa:
show-sql: true
defer-datasource-initialization: true
database-platform: org.hibernate.dialect.H2Dialect
White label errors occur when you don't have a mapping for /error. Check if you have the /h2-console route defined in one of your controller methods. If you have ensure all your classes are on the same folder level or deeper as your main class to ensure component scanning actually finds your classes.
I'm building a spring boot application. I'm trying to enable SQL logs in application.yml. How can I do it?
We can use any one of these:
spring:
jpa:
show_sql: true
or
logging:
level:
org:
hibernate:
sql=debug
type:
descriptor:
sql:trace
I'm doing Spring Security and I'm trying to connecte my API with database to get user Authentication.
I don't know the error.
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:
Property: driverclassname
Value: com.mysql.cj.jdbc.Driver
Origin: "driverClassName" from property source "source"
Reason: com.mysql.cj.jdbc.Driver
Action:
Update your application's configuration
Process finished with exit code 1
It looks like HikariCP configuration issue. If you are using yml file for spring boot, the application.yml file should have the following contents.
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: <jdbc url specific to your database>
driver-class-name: <database url>
username: <username>
password: <password>
hikari:
idle-timeout: 10000
Please change the place holders as I have mention with angles <> as per the database.