Spring Boot datasorurce configuration - spring

Below is the *.yml file for one of our application
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
username: user1
password: pass1
url: jdbc:oracle:thin:#//host1:port1:sid1
tomcat:
driver-class-name: oracle.jdbc.driver.OracleDriver
username: user2
password: pass2
url: jdbc:oracle:thin:#//host2:port2:sid2
max-active: 10
default-transaction-isolation: 2
main:
banner-mode: log
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.Oracle12cDialect
I am just wondering which database will Spring pick by JdbcTemplate and what is the difference between these two?

Sring will get these configuration
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
username: user1
password: pass1
url: jdbc:oracle:thin:#//host1:port1:sid1
Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-use-yaml-for-external-properties

Related

How to set Spring profiles for deploying to Heroku

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.

There is not connect another profile in Spring boot

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 ?

Set hibernate dialect for jpa in YML

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

org.h2.jdbc.JdbcSQLException: Schema "myschema" already exists

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.

jhipster Oracle database connectivity

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

Resources