Spring boot: Oracle RAC DB connection configuration - java

In Spring boot application, Need to configure Oracle RAC DB URL. Can someone explain how to configure the Oracle RAC URL in application.properties?
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL={PROTOCOL})(HOST={{URL})(PORT={PORT})))(CONNECT_DATA=(SERVICE_NAME={SERVICE_NAME})))
Have verified the Spring boot official doc and didn't find anything related. Even verified in the Common Properties and can't find any references.
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
Thanks for your help in advance!

Try with below.
jdbc:oracle:thin:#(DESCRIPTION=
# (LOAD_BALANCE=on)
# (ADDRESS=(PROTOCOL=TCP)(HOST=host1) (PORT=1521))
# (ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=1521))
# (CONNECT_DATA=(SERVICE_NAME=service_name)))
OR
# Oracle settings
spring.datasource.url=jdbc:oracle:thin:#localhost:1522:orcl
spring.datasource.username=HIBERNATE_TEST
spring.datasource.password=HIBERNATE_TEST
spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver
OR
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=OFF)(FAILOVER=ON)
(ADDRESS=(PROTOCOL=TCP)(HOST=tst-db1.myco.com)(PORT=1604))
(ADDRESS=(PROTOCOL=TCP)(HOST=tst-db2.myco.com)(PORT=1604)))
(CONNECT_DATA=(SERVICE_NAME=mydb1.myco.com)(SERVER=DEDICATED)))
Sources :
https://docs.oracle.com/cd/E57185_01/EPMIS/apbs01s01.html
https://dzone.com/articles/configuring-spring-boot-for-oracle

This is what i did to connect to postgres in my project and it is in production now. For Oracle it is exactly same. In fact for any other RDBMS.
Add the properties in the application.yml or the application.properties in the spring boot project.
The below is yml configuration.
spring:
jpa:
database: POSTGRESQL
show-sql: false
datasource:
platform: postgres
url: jdbc:postgresql://123.3.4.89.com:1234/DatabaseName
username: user123
password: pass123
driver-class-name: org.postgresql.Driver
testWhileIdle: true
validationQuery: SELECT 1
Then add the driver in the pom or the gradle build file which build tool you are using.
And the jpa jar of spring boot.
This was entry in the build.gradle file.
compile ('org.springframework.boot:spring-boot-starter-data-jpa')
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.2'
Thats it, now you can create your repositories and start pushing and fetching data in the Db.
Hope this helps, cheers !!!

Related

Profile Groups in Spring Boot 3 not working

After the migration to Spring Boot 3 it's not possible to activate multiple profiles in profile specific files. Instead Profile Groups should be used.
Unfortunately I can't get them to work, here is the snippet from my yml config:
spring:
profiles:
group:
local: debuglogin, profile_a, profile_b, profile_c
I have tried this in application-local.yml. Did anyone had the same experience?
I've just found the issue. I was adding this code block in the profile specific file like application-local.yml instead it should be added in the application.yml:
spring:
profiles:
group:
local: debuglogin, profile_a
integration-test: debuglogin, profile_b
this should work if added in the application.yml but not in profile specific files.

Spring Boot 2.2.2 Datasource URL not working for SQL Server 2019

I'm trying to doing a test development with Spring on SQL Server 2019, but all I get is the Datasource url attribute issue.
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
As for my yml configration I don't think I set something wrong
spring:
datasource:
url: jdbc:sqlserver://localhost:1433;databaseName=yap_test
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: 'yap_user'
password: 'P#ssw0rd'
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
show_sql: false
format_sql: false
dialect: org.hibernate.dialect.SQLServer2012Dialect
use_nationalized_character_data: true
org:
hibernate:
envers:
audit_table_prefix: AUD_
audit_table_suffix: ''
revision_field_name: REVISION_ID
revision_type_field_name: REVISION_TYPE
store_data_at_delete: true
for the JDBC Driver i'm using mssql-jdbc-8.2.2.jre8
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
</dependency>
I hope someone can help me figure out what's my configuration problem
Everything looks fine. there is no problem with property file.
1st of all try to check whether "mssql" jar is there or not in your local maven repository.
if it's there then update your property file as below:--
spring.datasource.driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
In your Case it should be:--(yml file)
spring:
datasource:
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
if it's not there:--
solution :- give the mssql JAR path in the pom then run
I actually added exclude any YML file during jar repackage, so that's the reason why everytime I tried to run it locally it doesn't find the url attribute.

Spring boot 2.1.1, use postgres config with jpa

I build some simple spring boot project, with jpa/postgres.
But when i debug this project, error say
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
But in my application.properties that have url.
spring.datasource.url=jdbc:postgresql://localhost:5432/oauth?currentSchema=oauth
spring.datasource.username=SOME_USER_NAME
spring.datasource.password=SOME_PASSWORD
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=validate
#hibernate
spring.jpa.database = postgresql
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
Does anyone know about this?
if validation is trouble than debug don't say like 'url attribute is not specified'.
And My application.properties name 'application-test.properties', and in debug configuration set 'test', and also log say 'the profiles test are currently active'.
It looks like properties is not matching, but log said profile is matched.
It makes me very confuse..
I found problem. Jonathan Johx and TinyOS is right, Thanks for them.
If project built by spring boot 2.1.1, Intellij with postgres, then Override build.gradle and pom.xml.
In Spring-boot 2.1.1 (I still don't know why default value is it)
default setting is
runtime('org.postgresql:postgresql')
but If you want to run right, than must write 'compile' like this.
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
version is not important. just find in maven repo what you want version.

Managing dependencies with spring boot profiles and Gradle

I created a java project that will use spring boot and Gradle. I would like to configure profiles, for the different environment (development on my local machine, systemtest for integration test on server farm machine etc). I would use h2 in memory database for development environment and SqlServer for systemtest environment. In build.gradle I defined the following dependencies
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web-services")
compile('org.springframework.boot:spring-boot-starter-actuator')
runtime('com.h2database:h2:1.4.195')
runtime('com.microsoft.sqlserver:mssql-jdbc')
}
I created a application.yml file, application-development.yml and application-systemtest.yml where I would put common properties and environment specific properties. The file application-systemtest.yml defines the connecction parameters for sql server
spring:
datasource:
url: jdbc:sqlserver://<host>,1433;databaseName=MYDB
username: myuser
password: mypass
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
jpa:
show-sql: true
hibernate:
dialect: org.hibernate.dialect.SQLServer2012Dialect
I would also create an uber-jar and select the profile as a launch parameter, ie
java -Dspring.profiles.active=systemtest -jar <my uber jar>
The development profiles starts fine and I am running on h2 in memory database. When trying systemtest profile, spring boot fails to load contexts and fails. This is caused by spring boot finding h2 dependency and trying to configure datasource defined in application-systemtest.yml
So I modified the build.gradle dependencies closure
def profile = project.findProperty('spring.profiles.active')
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web-services")
compile('org.springframework.boot:spring-boot-starter-actuator')
if (profile == 'development') {
runtime('com.h2database:h2:1.4.195')
} else {
runtime('com.microsoft.sqlserver:mssql-jdbc')
}
}
This time spring boot start correctly. Don't like very much this solution as I have to handle the profile configuration partly with Gradle. I would like to know if there is a way to configure spring boot so that profile is completely managed within itself, resolving h2 in development environment and sqlserver in systemtest environment, leaving Gradle unaware of spring profiles.
How to solve this problem ?
It wouldn't be advisable to have a different artifact / binary depending on the DB product. Try to configure the datasources / Spring profiles as suggested by #M. Deinum to prevent the datasource to be configured as H2 when using a different DB.

Flyway-maven-plugin how to read the settings from spring boot application.yml

In my Spring Boot project I want to use flyway-maven-plugin.
My pom:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.1</version>
<configuration>
<url>jdbc:mysql://localhost:3306/my_database?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&connectionCollation=utf8_unicode_ci&characterSetResults=UTF-8</url>
<user>root</user>
<password>${spring.datasource.password}</password>
</configuration>
</plugin>
And here is my application.yml
spring:
profiles.active: default
---
spring:
profiles: default
spring.datasource:
password: root
As I understood to use mvn flyway:info I need some plugin which will read my application.yml. Or maybe there is another way?
I have the following in my src/main/resources/application.properties
flyway.url=jdbc:sqlserver://localhost:1433
flyway.user=james_hetfield
flyway.password=MetaLLic#
spring.datasource.url=${flyway.url}
spring.datasource.user=${flyway.user}
spring.datasource.password=${flyway.password}
And then I run the migrations from the command-line as follows
mvn -Dflyway.configFiles=src/main/resources/application.properties flyway:migrate
My understanding is that if you are using Flyway with Spring Boot you do not use the flyway-maven plugin.
EDIT: Using Flyway CLI has a place when you need to work with the DB directly to make modifications to Flyway's schema table. An example would be to init an existing DB or clean a test project.
These are the current settings that can be performed out of the box by setting properties within the application.properties for Flyway.
# FLYWAY (FlywayProperties)
flyway.check-location=false # check that migration scripts location exists
flyway.locations=classpath:db/migration # locations of migrations scripts
flyway.schemas= # schemas to update
flyway.init-version= 1 # version to start migration
flyway.init-sqls= # SQL statements to execute to initialize a connection immediately after obtaining it
flyway.sql-migration-prefix=V
flyway.sql-migration-suffix=.sql
flyway.enabled=true
flyway.url= # JDBC url if you want Flyway to create its own DataSource
flyway.user= # JDBC username if you want Flyway to create its own DataSource
flyway.password= # JDBC password if you want Flyway to create its own DataSource
YAML version:
flyway:
check-location: false
locations: classpath:db/migration
....
If you need further customization you may need to write a #Bean to further customize how Flyway interacts with your application.
To read *.yml properties from pom you should use https://github.com/aadnk/yaml-properties-plugin

Categories

Resources