I am trying to get run application.yml instead of application.properties. But it fails. Please help. The same thing works in application.properties.
My application.yml looks like this:
spring:
profiles: container
jpa:
show-sql: true
database: MYSQL
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: update
datasource:
url: jdbc:mysql://mysql57:3308/hello_java
username: demo_java
password: 1234
driver-class-name: com.mysql.jdbc.Driver
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-03 10:40:26.671 ERROR 12600 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
APPLICATION FAILED TO START
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
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
Related
I want to write unit test in my Spring project.
There is only one application config in the whole project.
The application run well, but I get a "Caused by: java.sql.SQLException: No suitable driver" exception in unit test.
How can I setup a driver into the test?
The only annotation used in unit test:
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
The db connection settings in application.yml:
spring:
main:
allow-bean-definition-overriding: false
application:
name: application
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.sybase.jdbc4.jdbc.SybDriver
password: xxx
url: jdbc:sybase:Tds:localhost:3268/my_db
username: admin
hikari:
connection-test-query: SELECT 1
liquibase:
change-log: liquibase/changelog.sql
user: admin
password: xxx
jpa:
database: SYBASE
database-platform: org.hibernate.dialect.SybaseAnywhereDialect
hibernate:
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
I finally find out the reason.
Since I had added a test configuration class into test folder, the extra configuration is also loaded into the single annotation "#SpringBootTest" test and the db connection setup fail.
When I comment the #TestConfiguration in the extra test configuration class file, the test with single #SpringBootTest can work normally.
I'm able to connect to postgres using spring cloud config server with only bootstrap.yml file.
But I have multiple environments like dev,test and prod. So I want to create separate profiles for each environment(like bootstap-dev.properties) and change the url datasource url accordingly.
Can anyone please suggest me regarding the same ?
bootstrap.yml:
server:
port: 8081
spring:
application:
name: myapp
profiles:
active: jdbc
datasource:
url: jdbc:postgresql://localhost:5432/config_db
username: XXXX
password: XXXX
driverClassName: org.postgresql.Driver
cloud:
config:
server:
jdbc:
sql: SELECT key, value FROM properties WHERE application=? AND profile=? AND label=?;
order: 0
default-label: default
bus:
trace:
enabled: true
security:
user:
name: XXX
password: XXX
management:
endpoints:
web:
exposure:
include: bus-refresh,health
endpoint:
health:
show-details: always
It is easy to use in the spring cloud.
First, create an application.properties that content only follow like this spring.profiles.active=dev
Second, to modify your dev application.properties name to application-dev.properties.
Ok, then put them into your resources directory together.
Others, you can create another file of environments with different suffixes, such as application-test.properties or application-pro.properties..., and only change the active value to the suffix name. like this spring.profiles.active=test
My best to you.
Error I am Getting after running sudo ./gradlew run :
Task :run FAILED
12:03:13.440 [main] ERROR com.zaxxer.hikari.HikariConfig - Failed to load driver class com.mysql.jdbc.Driver from HikariConfig class classloader jdk.internal.loader.ClassLoaders$AppClassLoader#3d4eac69
12:03:13.445 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type [javax.sql.DataSource]
How my Application.yml looks like :
micronaut:
application:
name: freshdb2
#datasources.default: {}
datasources:
default:
url: jdbc:mysql://localhost:3306/mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: root
password: ""
driverClassName: com.mysql.jdbc.Driver
You are missing driver, you should add dependency mysql-connector-java, for gradle add:
runtime group: 'mysql', name: 'mysql-connector-java', version: '8.0.13'
I was also getting the message:
io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type [javax.sql.DataSource]
I'm not using MySQL, just trying to unpick how to use JPA from Micronaut Data Guide and others using H2.
It turns out I had incorrectly copied the datasource properties into application.yml. The above message is all you get to tell you this.
In my case the back quotes in the following had become something else.
datasources:
default:
url: ${JDBC_URL:`jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE`}
username: ${JDBC_USER:sa}
password: ${JDBC_PASSWORD:""}
driverClassName: ${JDBC_DRIVER:org.h2.Driver}
dialect: ${JDBC_DIALECT:H2}
I have spring boot server with 2 property files: application-local.properties and application-test.properties
In each file I have configs for dev machine and for test. Start it like this:
-Dspring.profiles.active=local
But in new spring boot project I use .yaml config file. And I do not understand how can I use profiles with .yaml. I tried read documentation but understood nothing. Can you explain what to do, step by step?
I need have two files?
application-local.yaml and application-test.yaml
Or I need write all in one application.yaml file? If in one file how can I separate configs? It is my config:
server:
path: ***
port: ***
cxf:
path: ***
spring.datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: oracle.jdbc.OracleDriver
url: ***
username: ***
password: ***
hikari:
minimumIdle: 5
maximumPoolSize: 20
idleTimeout: 30000
poolName: SpringBootJPAHikariCP
maxLifetime: 2000000
connectionTimeout: 30000
connection-test-query: SELECT 1 FROM DUAL
spring.jpa:
show-sql: false
database-platform: org.hibernate.dialect.Oracle10gDialect
properties.hibernate.jdbc.batch_size: 30
properties.hibernate.cache.use_second_level_cache: false
hibernate:
ddl-auto: validate
spring.cache:
ehcache:
config: classpath:ehcache.xml
#app configs
my:
messages-max-count: 5
messages-delay: 100
schedulers-charge-delay: 100
client:
first-server-address: ***
second-server-address: ***
last-server-address: ***
enabled-client: FirstClient
I want create test profile and change database url (or change to postgreSql), change maximumPoolSize property
Create application.yaml and define all default properties there.
Create application-local.yaml and override properties needed for the local profile.
Create application-test.yaml and override properties needed for the test profile.
Set spring.profiles.active by either passing it as a system property (-D for java) or defining it within application.yaml.
When you are running an app with a {PROFILE}, Spring will parse application-{PROFILE}.yaml after application.yaml.
Yes, you can create multiple profiles even with single file
Profile are separated with 3 DASH (---)
logging:
level:
.: error
org.springframework: ERROR
spring:
profiles:
active: "dev"
main:
banner-mode: "off"
server:
port: 8085
---
spring:
profiles: dev
---
spring:
profiles: prod
As I currently see it I have 5 possible database profiles
CI testing -> h2 mem
developer environment (could be test or app run) -> h2 mem, or h2 file, or postgres
production -> postgres (ideally credentials not stored in the git/war)
currently I have postgres configured for running the application, and h2 configured for testing via having a different application.properties in java/resources vs test/resources
what's the simplest way to have the database connection information change for these scenarios?
As M. Deinum mentions in his comment, the simplest way to do this is to use profile specific configuration.
Spring Boot allows you to have one common configuration file (application.properties) and then multiple other files, each specific to a profile (application-${profile}.properties).
For instance:
application.properties - Common configuration
application-dev.properties - Configuration for dev profile
application-ci.properties - Configuration for ci profiles
If your application runs with "ci" profile for instance, the default configuration file as well as the ci configuration file (which would contain the datasource configuration properties for ci profile) will be loaded.
To switch profiles you can use one of the following options:
JVM property: -Dspring.profiles.active=ci
Command line switch: --spring.profiles.active=dev
For unit tests you can use #ActiveProfiles("test") annotation on your test classes to tell Spring that unit tests should be run with test profile.
Also if you don't want to store production database credentials along with your source code, you can specify external configuration file when you deploy your app in production:
Using command line switch: --spring.config.location=/srv/myapp/config.properties
Using a JVM property: -Dspring.config.location=/srv/myapp/config.properties
Compact answer for the above scenario would be by creating a single application.yml file and creating different profiles based on the requirement, in your case -dev, -ci and -prod and providing the DB information accordingly.
Sample example is:
spring:
profiles.active: development
---
spring:
profiles: development
datasource:
db-person:
url: jdbc:oracle:thin:#db_person_dev
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:#db_contract_dev
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
spring:
profiles: test
datasource:
db-person:
url: jdbc:oracle:thin:#db_person_test
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:#db_contract_test
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
spring:
profiles: production
datasource:
db-person:
url: jdbc:oracle:thin:#db_person_prod
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:#db_contract_prod
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
For further understanding and simple example you can refer this link.