Spring boot 2.1.1, use postgres config with jpa - java

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.

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.

SpringBoot 2 with Flyway: spring.flyway.locations is ignored

I am using Spring Boot 2.2.2 with Flyway 5.2.4 and I tried to configure flyway to use a differente location for the scripts, but spring.flyway.locations=filesystem:db_other/migration/{vendor} neither flyway.locations=filesystem:db_other/migration/{vendor} configurations on application.properties worked.
When running the program, the following exception appear in the log:
FlywayMigrationScriptMissingException: Cannot find migration scripts in: [classpath:db/migration]
I already tried using Spring Boot 2.2.1, 2.2.0, 2.1.11 and Flyway 6.1.0 and 6.1.3, but the result is the same.
The default value for that property is classpath:db/migration as shown here (search for flyway).
Since you're using a different folder in the resources directory you should only need to change "filesystem" to "classpath" in your application.properties value.
Actually if was my fault: as I used to work with just spring (not spring boot) I configured my test class with the annotations #ExtendWith(SpringExtension.class) AND #ContextConfiguration(classes = { MyConfiguration.class }) instead of just use #SpringBootTest. When making this change the test worked.

Spring boot: Oracle RAC DB connection configuration

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 !!!

Gradle test dependencies not loaded in Spring Boot test

I am trying to work with p6spy in Spring Boot tests. I have a test class annotated with
#RunWith(SpringRunner.class)
#SpringBootTest
My gradle.build looks like this
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
testCompile 'p6spy:p6spy:3.0.0'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
As for the application itself (which runs fine), I added the new datasource to the test-application-context.
spring:
application:
name: persistence
datasource:
url: jdbc:p6spy:h2:mem:persistence;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
jpa:
database: H2
But when I run my test I get this error
java.lang.IllegalStateException: Cannot load driver class: com.p6spy.engine.spy.P6SpyDriver
To me this looks like my dependencies are not loaded. At first, I was using the #DataJpaTest annotation, but this one ignored even my new test-application-context.
Any help appreciated.
EDIT: I got it working bei adding the p6spy dependency manually to the test using IntelliJ. Now I'm sure that my classpath is wrong, but I don't know how to fix it to make it work in Gradle.
The problem is located in my version of IntelliJ. I will file a bug report.
If anyone should have this problem, I added the missing dependencies manually in the project settings. Then it works also from the IDE.

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.

Categories

Resources