I have a Spring web app, with 3 different profiles for prod dev and test Each of these uses a different database, local for test and dev and a heroku postgres for prod.
I have set up my application.yaml file as below,
spring:
profiles:
active:
- dev
datasource:
url: jdbc:postgresql://ec2-35-168-65-132.compute-1.amazonaws.com:5432/*******
username: username
password: ******************
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
javax:
persistence:
validation:
mode: none
show-sql: true
data:
web:
pageable:
default-page-size: 10
max-page-size: 100
---
spring:
config:
activate:
on-profile: prod
datasource:
url: jdbc:postgresql://ec2-35-168-65-132.compute-1.amazonaws.com:5432/********
username: username
password: **********
jpa:
hibernate:
ddl-auto: update
syftgolf:
upload-path: uploads-prod
---
spring:
config:
activate:
on-profile: dev
datasource:
url: jdbc:postgresql://localhost:5432/testdb
username: postgres
password: admin
jpa:
hibernate:
ddl-auto: create-drop
syftgolf:
upload-path: uploads-dev
---
spring:
config:
activate:
on-profile: test
datasource:
url: jdbc:postgresql://localhost:5432/syftgolf
username: postgres
password: admin
jpa:
hibernate:
ddl-auto: create-drop
syftgolf:
upload-path: uploads-test
I have slowly been updating this app over the months from a barebones project I did. I cant for the life of me, remember what this first part does.
spring:
profiles:
active:
- dev
datasource:
url: jdbc:postgresql://ec2-35-168-65-132.compute-1.amazonaws.com:5432/*********
username: username
password: *********
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
javax:
persistence:
validation:
mode: none
show-sql: true
data:
web:
pageable:
default-page-size: 10
max-page-size: 100
I think spring:profiles:active: -dev sets the active profile to dev, so it should then use the database under dev? But I cant remember why the rest of the lines are placed here and not under a specific profile,
datasource:
url: jdbc:postgresql://ec2-35-168-65-132.compute-1.amazonaws.com:5432/***********
username: username
password: *************************
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
javax:
persistence:
validation:
mode: none
show-sql: true
data:
web:
pageable:
default-page-size: 10
max-page-size: 100
if I remove this and just leave the active profile part, I get an error that the app cant start.
Please could someone help with the best way of laying out this application.yaml file and also is setting active profile in this file the correct way of doing it?
So if I am working on the app, i set this to dev do I then need to change it to prod before I push it to heroku using the heroku cli?
The best way would be to save an environment variable in your heroku application which will be using by your application to set the profile.
For example, this is a configuration for a simple spring boot thymeleaf application.
server:
port: ${PORT:4000}
spring:
application:
name: PROFILE-TEST
---
spring:
config:
activate:
on-profile: dev
custom:
message: "This is development environment"
---
spring:
config:
activate:
on-profile: prod
custom:
message: "This is production environment"
---
spring:
profiles:
active: ${PROFILE:dev}
Here I'm using spring.config.activate.on-profile to set an environment to have different custom.message.
By Default I'm setting the active profile with spring.profiles.active=${PROFILE:dev}. PROFILE is the environment variable we're gonna use to set different value during deployment. If there is no environment variable set for PROFILE, it'll default back to dev.
Now if I've written a controller which will only act on profile dev with the #Profile annotation like this
#Profile("dev")
#Controller
#RequestMapping("/")
#RequiredArgsConstructor
public class DevHomeController {
private final CustomConfigurationProperties customConfigurationProperties;
#GetMapping
public ModelAndView home(ModelAndView modelAndView) {
modelAndView.addObject("message", customConfigurationProperties.getMessage());
modelAndView.setViewName("dev-home.html");
return modelAndView;
}
}
It'll show something like
I've written another controller which will have a profile value #Profile("prod")
In the console type the below command to set an environment variable in heroku.
heroku config:set PROFILE=prod
And deploy the application. I'll look like
The message you see on the screen is coming from custom.message property you have set in your application.yml, and as we have set the environment variable for PROFILE, heroku is using that to set the running profile.
Related
This is my config:
#Bean
#QuartzDataSource
#ConfigurationProperties(prefix = "spring.datasource")
public DataSource quartzDataSource() {
return DataSourceBuilder.create().build();
}
and this is my app.yml:
datasource:
url: my-url
jdbcUrl: ${spring.datasource.url}
username: 'root'
password: 'root'
...
quartz:
job-store-type: jdbc
jdbc:
initialize-schema: always
wait-for-jobs-to-complete-on-shutdown: true
properties:
org:
quartz:
dataSource:
quartz-data-source:
provider: hikaricp
driver: com.mysql.cj.jdbc.Driver
URL: ${spring.datasource.url}
user: ${spring.datasource.username}
password: ${spring.datasource.password}
maximumPoolSize: 5
connectionTestQuery: SELECT 1
validationTimeout: 5000
idleTimeout: 1
scheduler:
instanceId: AUTO
instanceName: my-project-scheduler
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
useProperties: false
misfireThreshold: 60000
clusterCheckinInterval: 30000
isClustered: true
dataSource: quartz-data-source
threadPool:
class: org.quartz.simpl.SimpleThreadPool
threadCount: 1
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
My question:
If I set initialize-schema: always then the qrtz tables are created on each application startup.
On the other side, if I set initialize-schema: never then I get an error on the first startup that the qrt tables are missing.
Is there a way to configure it to initialize the qrtz tables only if they do not exist?
You are gonna need a migration tool to handle the database creation.
Spring Boot provides two options: Flyway and LiquidBase.
Choose one, create migration scripts and you are up and running.
I personally like the Flyway approach.
You just add implementation 'org.flywaydb:flyway-core' to your build.gradle file (or the maven alternative).
Then add this to your application.yml
spring:
flyway:
enabled: true
baseline-on-migrate: true
Then create db/migration folder in resources folder and put in your migration scripts eg. V1_0_0__db_init.sql (flyway has its own naming convention).
To get the create SQL scripts I recommend that you export them from running database.
Also do not forget to change the spring.jpa.hibernate.ddl-auto to validate.
I have a project with two properties.
application.yml (major)
spring:
config:
activate:
on-profile: local
application:
name: test-app
datasource:
cluster:
enabled: true
driverClassName: org.postgresql.Driver
hikari:
maximum-pool-size: 30
minimum-idle: 3
autoCommit: false
jpa:
database-platform: org.hibernate.dialect.PostgreSQL95Dialect
properties:
hibernate:
connection.provider_disables_autocommit: true
jdbc.lob.non_contextual_creation: true
open-in-view: false
application-local.yml
spring:
datasource:
read:
url: jdbc:postgresql://localhost:5432/test-app
write:
url:jdbc:postgresql://localhost:5432/test-app
username:
password:
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
properties:
open-in-view: false
I also have added the setting in IntelliJ IDEA
VM options also has used.
-Dspring.profiles.active=local
But during the launch I get an error:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and
no embedded datasource could be configured.
Reason: Failed to determine suitable jdbc url
I assume that the specified profile did not start
Who has any ideas how to fix this ?
I tried to set hibernate dialect for jpa in YML,
checked many topics, but it does not set:
spring:
datasource:
hikari:
allow-pool-suspension: true
connection-timeout: 1000
name: testDb
jpa:
database: h2
generate-ddl: false
database-platform: h2
package-to-scan: com.x.model
properties:
hibernate:
dialect: com.x.data.core.hibernate.dialect.ExtendedH2Dialect
h2:
console:
enabled: true
path: /h2
How to fix this?
what is com.x.data.core.hibernate.dialect.ExtendedH2Dialect ? You have to use dialect as org.hibernate.dialect.H2Dialect
below is the sample
server:
port: 8096
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:~/test
username: sa
password:
h2:
console:
enabled: true
jpa:
hibernate.ddl-auto: update
generate-ddl: false
show-sql: false
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
Note:
I'm using spring-boot-starter-data-jpa 2.0.5
The issue is when I execute my integrations tests I get the exception :
Caused by: org.h2.jdbc.JdbcSQLException: Schema "myschema" already exists; SQL statement:
CREATE SCHEMA myschema [90078-196]
I have this application-test.yml :
debug: true
spring:
data:
jpa:
repositories:
enabled: true
jpa:
database: h2
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: create-drop
properties:
hibernate.hbm2ddl.auto: create-drop
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE;DATABASE_TO_UPPER=false;INIT=CREATE SCHEMA IF NOT EXISTS myschema
username: sa
password:
driver-class-name: org.h2.Driver
What am I missing here ?
hbm2ddl is set as create-drop changing it to update would solve your issue.
I can't seem to get the Liquibase running against oracle.
My application-dev.yml has:
spring:
profiles: dev
datasource:
dataSourceClassName: oracle.jdbc.pool.OracleDataSource
url: jdbc:oracle:thin:#localhost:1521:orcl
username: rest_test
password: rest_test
jpa:
database-platform: org.hibernate.dialect.OracleDialect
database: ORACLE
openInView: false
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
But I recieved:
[DEBUG] com.steve.config.DatabaseConfiguration - Configuring Datasource
[ERROR] com.zaxxer.hikari.util.PropertyBeanSetter - Property url is does not exist on target class class oracle.jdbc.pool.OracleDataSource
Which is odd as the class seems to have a url property:
[btuser#localhost pool]$ strings OracleDataSource.class | grep -i seturl
setURL
Any thoughts?
Thanks
fixed this now, this config now works:
spring:
profiles: dev
datasource:
driverClassName: oracle.jdbc.OracleDriver
dataSourceClassName: oracle.jdbc.pool.OracleDataSource
url: jdbc:oracle:thin:#localhost:1521:orcl
username: rest_test
password: rest_test
jpa:
database-platform: org.hibernate.dialect.Oracle10gDialect
database: ORACLE
openInView: false
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory