I am trying to use necessary fields and remove unnecessary or redundant fields in my application.yml, but there are some properties that seem to be duplicate or same features.
Here is an example features below.
1. I think I should use dialect property, but which one should I use (jpa.properties.dialect or jpa.database-platform) or both?
2. As far as I see from Common Application Properties, it seems optional to use jpa.database property. But not sure if I can omit jpa.database and jpa.properties.dialect properties both. Any idea for this?
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:${DB_NAME};DATABASE_TO_UPPER=false
username: ${DB_USERNAME}
password:
jpa:
properties:
dialect: org.hibernate.dialect.H2Dialect // ???
database: h2 // ???
database-platform: org.hibernate.dialect.H2Dialect // ???
Related
What I am looking for is some suggestions on this behavior: Spring boot app(Considered as a microservice) should come up, irrespective of Db status.
Why I am doing this?
Based on my understanding of the microservice all services should be independent of each other.
I am using Spring boot with JPA (org.springframework.boot' version '2.5.7'). I am able to achieve this using the below configuration
spring:
datasource:
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost:5433/xxx?createDatabaseIfNotExist=true&characterEncoding=utf8&enabledTLSProtocols=TLSv1.2&useSSL=false
username: xx
password: xx
continueOnError: true
initialize: false
initialSize: 0
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 5000
minIdle: 0
jpa:
show-sql: true
hibernate:
naming_strategy: org.hibernate.cfg.DefaultNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
hbm2ddl:
auto: none
temp:
use_jdbc_metadata_defaults: false
But now the issue is I have to make hbm2ddl.auto none. Due to this, I am losing update schema functionalists which is one of the very essential functionality.
Requirement:
case 1. Service should be up and running irrespective of DB status
case 2. Jpa/Hibernate should update the database schema by comparing the existing schema with the entity mappings and generate the appropriate schema migration scripts (hbm2ddl.auto: update)
Can we achieve both? If yes how? or Do I have to compromise with one?
If I am going with only "case 1" do I have to rely on running schema updates manually or is there any other way?
thanks in advance
As mentioned in the comment you can exclude org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationif you wish for spring boot to ignore the status of the data source (link on how to do that). In order to still have the schema auto-generated/controlled outside of your actual db you could use something like liquibase, which is used a lot in production environments.
I am trying to set up in-memory H2 tables for a test class in my Spring boot application.
My config looks something like:
spring:
jpa:
show-sql: true
generate-ddl: true
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: create-drop
datasource:
# not sure which one to use so added both just in case
initialization-mode: always
initialize: true
platform: h2
# casting a wide net here, but no cookie - completely ignored
data: data-h2.sql,classpath*:data-h2.sql, classpath:data-h2.sql
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
driver-class-name: org.h2.Driver
username: sa
password:
As you can see, I'm trying to load a data-h2.sql script upon db initialization.
Unfortunately, the property is ignored no matter the value.
I am certain the configuration file is being picked up properly (e.g. among others, I desperately added a #Value("${spring.datasource.data}" -annotated property in my test class and the value was indeed populated correctly).
As an alternative, I could annotate the test class with #Sql("classpath:data-h2.sql") which did run the script - however it did so for every test, while I wanted the script to be run once before any test execution.
I also tried removing that and using a blank schema.sql and moving the population to data.sql (as suggested here), but Spring would complain about the empty schema file - which is useless to me, because my schema is auto-generated and I certainly don't want to re-create it (NB: probably a conflict with a hibernate property if memory serves).
I've browsed some of the answers here, but the only one I could use, is not working.
The only solution I can see is to keep the #Sql annotation, but try and clear the tables after every test with another #Sql annotation launching another script on #After.
This seems insane to me - there must be a better solution.
Am I missing something more esoteric than it already is in my configuration?
Simply put your file in the /main/resources directory (what you already did)
Spring Boot 2:
The correct property is:
spring.datasource.initialization-mode=always
Read more about this topic in the Spring Boot Documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-initialize-a-database-using-spring-jdbc
Spring Boot 1
You only have to place data-h2.sql in the classpath
Read more about this topic in the Spring Boot Documentation:
https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/htmlsingle/#howto-initialize-a-database-using-spring-jdbc
I'm creating a spring boot application using Flyway to migration and want to use a memory database for development profile, but the problem is that data is lost every time I restart application. So I need to insert some data when my application start in development profile. I tried to put a file called data.sql on src/main/resource to spring load it when application starts but it doesnt work (It didnt run the script). I tried to put INIT=runscript from 'classpath:data.sql' in the h2 url but it tries to run it before Flyway migration execution so the tables doesnt exist yet. Can anyone give me an other way to do it?
My application.yml:
spring:
datasource:
url: jdbc:h2:mem:testdb;IFEXISTS=FALSE
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
show-sql: true
hibernate:
ddl-auto: none
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
flyway:
enabled: true
Per documentation , a profile-specific customised flyway spring.flyway.locations can be configured. The profile-specific scripts runs when that profile is active . So a dev profile configured will work on this requirement.
The initialisation script can be placed as part of migration folder which will run and populate the db.
An example can be found here
spring:
profiles: dev
spring.datasource:
driver-class-name:
password: ~
url: ~
username: ~
---
secdb:
profiles: dev
spring.datasource:
driver-class-name: ~
password: ~
url: ~
username: ~
---
I have above two properties declared as shown in the application.yml file but when I use it in implementation class as follows.
#Value("${spring.datasource.url}")
private String URL;
it works and picks up the url from YML file.
but when I do as follows
#Value("${secdb.spring.datasource.url}")
private String URL;
it fails at spring boot start saying
Could not resolve placeholder 'secdb.spring.datasource.url' in value...
As, I am at beginner level. YML may be wrong but my intention is to have two data sources in the YML file and use the second one for one JDBC connection other one is default. Please, guide me through the mistake
You have made two mistakes in your yaml file.
Don't use space before ---.
Before your first spring.datasource:, there is a space. It indicates spring.datasource: is a subproperty of spring:.
#Value("${secdb.spring.datasource.url}") is absolutely not the right way. Even you active secdb, you also need to get the value like #Value("${spring.datasource.url}").
I do't suggest you to use Spring profiles like secdb: profiles: dev. It's not a familiar way. You can use it like spring: profiles: secdb and active it just like spring.profiles.active=secdb. Or if you insist to use it that way, you need to active it like spring.profiles.active=secdb.
After all, if you want to use Spring profiles properties, you need to active it just like
$ java -jar -Dspring.profiles.active=production
or
add spring.profiles.active=production in application.properties.
I suggest you to read this document in detail.
I will be glad if it helps.
How do I specify database schema used by Spring Boot? I am using default hibernate (=default) and postgres (but i hoping for a generic solution). I know how to specify JDBC URL:
spring.datasource.url=jdbc:postgresql:db_name
But unfortunately postgresql does not allow to specify schema in JDBC URL. I know that there is hibernate property hibernate.default_schema, so I was hoping that one of the following properties will work:
hibernate.default_schema=schema
spring.hibernate.default_schema=schema
spring.jpa.hibernate.default_schema=raw_page
But unfortunately neither of them seems to have any result.
Use for application.properties:
spring.jpa.properties.hibernate.default_schema=your_scheme
OR for application.yaml:
spring:
jpa:
properties:
hibernate.default_schema: your_scheme
From the Spring Boot reference guide:
all properties in spring.jpa.properties.* are passed through as normal JPA properties (with the prefix stripped) when the local EntityManagerFactory is created
See http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties
For a full list of available properties see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties
It depends on the DataSource implementation which property has to be used to set the default schema (reference). With HikariDataSource for example spring.jpa.properties.hibernate.default_schema is ignored and you have to set
spring.datasource.hikari.schema=schema
See the complete list of HikariCP configuration parameters here.
spring.jpa.properties.hibernate.default_schema=your_scheme
OR
spring:
jpa:
properties:
hibernate.default_schema: your_scheme
With HikariDataSource for example spring.jpa.properties.hibernate.default_schema is ignored and you have to set too
spring.datasource.hikari.schema=your_scheme
spring:
jpa:
properties:
hibernate:
default_schema: your_schema_name
I was hitting error:
Cannot acquire connection from data source
org.postgresql.util.PSQLException: ERROR: unsupported startup parameter: search_path
Solution:
application-xyz_dev.yml
url: jdbc:postgresql://localhost:8080/your_database?search_path=your_scheme&stringtype=unspecified
spring:
jpa:
properties:
hibernate.default_schema: your_scheme