How to connect Spring and Oracle db (oracle cloud) 19c - spring

if i try to connect this error show
oracle.net.ns.NetException: Invalid connection string format, a valid format is: "host:port:sid" (CONNECTION_ID=plSfz1GBTiKKWbZqZbjUrA==)
i think yml datasource or jpa is wrong...
but I checked out gogle anywhere but,
oracle cloud(wallet) <-> spring is I can't found it
plz help me!
this is my build.gradle
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'war'
}
group = ''
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.20'
implementation 'junit:junit:4.13.1'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
implementation 'com.oracle.ojdbc:ucp'
implementation 'com.oracle.database.security:oraclepki'
implementation 'com.oracle.database.security:osdt_core'
implementation 'com.oracle.database.security:osdt_cert'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
this is my application.yml
# local, dev, prod 공통 설정
server:
port: 8080
tomcat:
uri-encoding: UTF-8
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:#[TNSNAME]?TNS_ADMIN=./src/main/resources/Wallet
username:
password:
jpa:
database-platform: org.hibernate.dialect.Oracle12cDialect
open-in-view: true
hibernate:
ddl-auto: none
# ddl-auto: create-drop
# ddl-auto: validate

If you use TNS_ADMIN, it has to be path to TNS_ADMIN file, and your connection has to be declared in tnsnames.ora.
Related reading: https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/connect-jdbc-thin-wallet.html#GUID-BE543CFD-6FB4-4C5B-A2EA-9638EC30900D

Refer to this blog for code sample and also instructions.

i resolved this way,
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:#<***TLSNAME>?TNS_ADMIN=D:/Wallet
username: username
password: password

Related

Hibernate envers, liquibase and postgres. entity_aud is not found

I have a spring boot v2.7.1 project running with liquibase and hibernate.
When i execute the command gradle diffChangeLog liquibase is generating tables with _AUD suffix and then hibernate is blowing an error
ERROR: relation "comment_aud" does not exist.
here are the relative information from my configuration files:
build.gradle
plugins {
id 'org.springframework.boot' version "${springBootVersion}" #2.7.1
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id "jacoco"
id 'org.liquibase.gradle' version '2.1.1'
}
apply plugin: 'org.liquibase.gradle'
depedencies {
implementation "org.liquibase:liquibase-core"
liquibaseRuntime 'org.liquibase:liquibase-gradle-plugin:2.1.1'
liquibaseRuntime 'org.liquibase:liquibase-core:4.8.0'
liquibaseRuntime 'info.picocli:picocli:4.6.3'
liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:4.13.0'
liquibaseRuntime "org.postgresql:postgresql"
liquibaseRuntime 'org.springframework.boot:spring-boot-starter-data-jpa'
}
liquibase {
activities {
main {
driver 'org.postgresql.Driver'
url 'jdbc:postgresql://localhost:5432/database'
username 'admin'
password 'admin'
changeLogFile "src/main/resources/liquibase/changelogs/" + new Date().format("yyyy_MM_dd-HH_mm_ss") + "-changelog.xml"
referenceUrl 'hibernate:spring:mypackagedir.entity' +
'?dialect=org.hibernate.dialect.PostgreSQLDialect' +
'&implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy' +
'&?physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy'
defaultSchemaName ""
classpath "build/classes/java/main"
logLevel "debug"
}
}
runList = 'main'
}
application.yml
spring:
datasource:
driver-class-name: org.postgresql.Driver
password: ${database.password}
url: ${database.host}
username: ${database.username}
jpa:
hibernate:
ddl-auto: none
properties:
org:
hibernate:
envers:
global_with_modified_flag: true
store_data_at_delete: true
audit_table_suffix: _aud
database-platform: org.hibernate.dialect.PostgreSQL92Dialect
show-sql: true
liquibase:
enabled: true
part of the generated changelog after i do diffChangeLog
...
<createTable tableName="comment_AUD">
<column name="REV" type="INTEGER">
<constraints nullable="false" primaryKey="true" primaryKeyName="comment_AUDPK"/>
</column>
<column name="REVTYPE" type="SMALLINT"/>
...
as you can see liquibase is generating the audit table with _AUD although i set it to _aud in hibernate properties
i tried the following:
adding and removing all or one of naming_strategies in build.gradle file
&implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy&?physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
adding removing naming strategies in application.yml properties:
jpa:
hibernate:
naming:
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
changing the suffix to audit_table_suffix:_AUD
but nothing helps! i am running in circles now and can't find a solution for this. can someone tell me how to fix this please?
Thanks in advance

Why does liquibase not start when the spring boot application starts?

I am trying to create a basic database structure when starting a spring boot application. I need that when starting the task :bootRun was run by liquibase.
It starts without errors, a connection to the database is created, but liquibase does not start. It does not give errors, there is no information in the log about it.
Why doesn't it start automatically with spring boot?
build.gradle
plugins {
id 'org.springframework.boot' version '3.0.0-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'spring.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5:3.1.0.M1'
implementation group: 'org.liquibase', name: 'liquibase-core', version: '4.12.0'
runtimeOnly'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
compileOnly 'org.projectlombok:lombok'
}
tasks.named('test') {
useJUnitPlatform()
}
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/db
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:db/changelog/db.changelog-root.yaml
src/db/changelog/db.changelog-root.yaml
databaseChangeLog:
- logicalFilePath: db/changelog/db.changelog-root.yaml
- changeSet:
id: create-a-structure
author: your_liquibase_username
changes:
- createTable:
tableName: app_user
columns:
- column:
name: id
type: BIGINT
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: username
type: varchar(254)
constraints:
unique: true
nullable: false
- column:
name: password
type: varchar(64)
constraints:
nullable: false
- column:
name: first_name
type: varchar(50)
nullable: false
- column:
name: last_name
type: varchar(50)
nullable: false
I believe the spring property for the changelog file should not have the classpath reference:
spring.liquibase.change-log=db/...
instead of
spring.liquibase.change-log=classpath:db/...
I had a similar problem. This solved it for me https://stackoverflow.com/a/41960712/3999476
I have a multi module maven project, I had to move the dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
to the same module where I added the dependency for liquibase.
Just noticed that you've put the liquibase yaml in:
src/db/changelog/db.changelog-root.yaml.
Since the specified value is:
spring.liquibase.change-log=classpath:db/changelog/db.changelog-root.yaml
Could you put the file into:
src/main/resources/db/changelog/db.changelog-root.yaml?

Session not created in Spring Session

Currently, I am testing Spring Session using Spring Boot
When I add Spring Security to a dependency, Session is normally created and stored in the spring_session table, but if I remove the Spring Security dependency, Session is not created and is not saved in the spring_session table.
My current settings are as below.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.session:spring-session-jdbc'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
compileOnly 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
spring:
profiles:
active: local
---
server:
servlet:
session:
timeout: 3600
spring:
config:
activate:
on-profile: local
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost/dummy
username: post
password: post
jpa:
hibernate:
ddl-auto: create-drop
session:
store-type: jdbc
jdbc:
initialize-schema: always

Cloud client config application do not get properties from Config Server Spring boot

I have a web application and I want to use Server Configuration from Spring Boot.
Both applications are located on localhost machine. I have made first two applications from scratch and they worked together, but when i use the client that has many dependencies in it (not just the cloud-config and web dependency) it is not working anymore. How do I know? I have a variable in properties file in server and i try to use it in my client with #Value("${atena}")
and error appears java.lang.IllegalArgumentException: Could not resolve placeholder 'atena' in value "${atena}".
The following image is my server config application.
The main class from server has the following annotation #EnableConfigServer
In atena-config.yml I have only the variable name:
atena: 'Hellllloooooo'
bootstrap.yml content
server:
port: 9000
spring:
profiles:
active: native
and build.gradle dependencies:
dependencies {
implementation 'org.springframework.cloud:spring-cloud-config-server'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
I am sure the server it is correct, something is wrong with my client.
--------------------> Client side
I have a restcontroller:
#RestController
#RequestMapping("/songs")
public class SongController {
#Value("${atena}")
String variable;
#GetMapping(value="/check-from")
public String viewVariable(){
return variable;
}
}
in which I am trying to get the variable from server config.
bootstrap.yml from client
spring:
application:
name: atena-config
cloud:
config:
uri: http://localhost:9000
And lastly the build.gradle from client:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.apis:google-api-services-youtube:v3-rev206-1.25.0'
implementation 'org.springframework.boot:spring-boot-starter'
implementation('org.apache.tomcat:tomcat-jdbc:9.0.10')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.mybatis:mybatis:3.4.6')
implementation('org.mybatis:mybatis-spring:1.3.2')
implementation('org.springframework.boot:spring-boot-starter-jdbc')
implementation('org.springframework.cloud:spring-cloud-starter-config')
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
I really don't know what happen, I am pretty sure that these dependencies are the problem, but I have not figured out which one, i can not exclude any of them, because I am using them in project.
Never mind. I have fixed it. Indeed the problem was from my dependencies, my gradle.build was with problem. I have created a new project with spring initializer having all the dependencies and copied the new gradle.build from there and now is it working.
This is the new build.gradle from client
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
ext {
set('springCloudVersion', 'Greenwich.RELEASE')
}
dependencies {
compile 'com.google.apis:google-api-services-youtube:v3-rev206-1.25.0'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

Spring Boot + Hibernate + Grails ignores ddlAuto in yml file

I don't want Hibernate to recreate or update the database schema.
According to the documentation, you need to have this in your application.yml file:
spring.jpa.hibernate.ddl-auto: none
It doesn't work.
Here's my application.yml file:
spring:
profiles.active: default
spring.jpa.hibernate.ddl-auto: none
---
spring:
profiles: default
spring.datasource:
driverClassName: net.sourceforge.jtds.jdbc.Driver
url: jdbc:jtds:sqlserver://IP/DB
username: user
password: password
spring.jpa:
hibernate:
ddlAuto: validate
showSql: true
hibernate:
ddl-auto: none
---
spring:
profiles: unit-test
spring.datasource:
pooled: true
jmxExport: true
driverClassName: org.h2.Driver
username: sa
password:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=FALSE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=TRUE
Documentation link:
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
Note I've also changed ddl-auto with ddlAuto, still doesn't work.
build.gradle:
buildscript {
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/libs-release" }
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.+"
}
}
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
apply plugin: 'spring-boot'
dependencies {
//Spring
compile "org.springframework.boot:spring-boot-configuration-processor"
compile "org.springframework.boot:spring-boot-starter"
compile "org.grails:gorm-hibernate4-spring-boot:5.0.+"
compile 'javax.el:javax.el-api:2.2.5'
//DB
testCompile 'com.h2database:h2:1.4.190'
compile 'net.sourceforge.jtds:jtds:1.3.1'
compile 'commons-dbcp:commons-dbcp:1.4'
}

Resources