My Spring Boot version is 2.3.4-RELEASE. I configure DataSource this way in application-dev.yml:
spring:
profiles: dev
datasource:
master:
driver-class-name: com.mysql.jdbc.Driver
url: myUrl
slave:
driver-class-name: com.mysql.jdbc.Driver
url: myUrl
When I activate the 'dev' profile, I can start my Spring Boot container successfully, but when I restart it, I get this error:
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).
I tried to add the config: spring.datasource.url. It works, but I need to distinguish master and slave. How can I do that?
You must activate your profile using property spring.profiles.active
If you have database settings to be loaded from a particular profile
you may need to activate it (no profiles are currently active).
Read set the active Spring profiles to see how to do it
Related
In my spring boot application, I have multiple profiles as below:
application.yml
application-dev.yml
application-local.yml
application-qa.yml
application-prod.yml
Each profile will have its own datasource configured. For testing purpose, same datasource configuration are passed in all profiles.
In application.yml if I mention spring.profiles.active=dev it is connecting to DB successfully. But if I mention spring.profiles.active=local then on server startup local profile is getting active with below error
***************************
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 (the profiles lcl are currently active).
Below configuration passed in all profiles excluding application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
password: root
url: jdbc:mysql://localhost:3306/local_db?useSSL=false
username: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
liquibase:
change-log: classpath:db/changelog/db.changelog-master.json
This is application properties code,please check and tell me how to fix the error.
debug=true
server.port=9090
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
#create, update, create-drop, validate
spring.jpa.hibernate.ddl-auto=update
#file related all configurations
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
project.image=images/
#here we are allowing debug so that we can get every log related to security on console
logging.level.org.springframework.security=DEBUG
#spring.profiles.active will tell us which profile we are using
spring.profiles.active= prod
This is application-developer properties code
spring.datasource.url=jdbc:mysql://localhost:3306/blog_app_apis
spring.datasource.username =
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
This is application-production properties code
spring.datasource.url= jdbc:mysql://blog-db.cx8gwdujroiq.ap-south-1.rds.amazonaws.com:3306/blog_app_apis
spring.datasource.username = pankaj
spring.datasource.password=kicker12345
This is the error message:
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 (the profiles prod are currently active).
Your production configuration is missing the database driver property, which you should clearly understand from the reason section in the exception message:
Reason: Failed to determine a suitable driver class
Since you have the application.properties file with the default configuration properties, you can use the fix suggested to you by the Spring framework:
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 (the profiles prod are currently active).
In order to fix the error for the production profile you need to add the following property to your application.properties file: spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
The resulting file should look like this:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
debug=true
server.port=9090
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
#create, update, create-drop, validate
spring.jpa.hibernate.ddl-auto=update
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
project.image=images/
#here we are allowing debug so that we can get every log related to security on console
logging.level.org.springframework.security=DEBUG
#spring.profiles.active will tell us which profile we are using
spring.profiles.active=prod
This will add the default database driver for all your profile. So be mindful to change this if some specific profile will use a database that differs from MySQL.
Updated:
You also have the incorrect name of the active profile. In your application.properties file you have a such line that sets the active profile of your application:
spring.profiles.active=prod
But your profile-based file is called application-production.properties. The Spring framework has a such naming format for configuration properties files:
application-[PROFILE_NAME].properties
So you should also rename your application-production.properties file to application-prod.properties to match the value from the spring.profiles.active property.
I am setting up an RestApiApplication.java and want to connect to a sqlite DB via URL
Now I have trouble setting up the resources via
application.properties file:
spring=
datasource=
driver-class-name=com.sqlite.jdbc.Driver
url=jdbc:sqlite:restApi.sqlite
username=root
password=
spring.jpa.hibernate.ddl-auto=create-drop
The terminal error in the IDE is:
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
I am working in a Mac Environment with Mac OS Ver 12.5
IDE I am using is IntelliJ IDEA CE
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
For setting up the database I am using DataGrip and the Database itself with it setting seems to be fine.
LOOK at DataGrip setup here
Any hints are welcome!
Thank you very much!
we have three environment as (dev, test and prod) and we have database configuration as below
spring:
jpa:
hibernate:
ddl-auto: update
datasource:
url: ${URL}
username: ${USERNAME}
password: ${PASSWORD}
What i am trying is that i create the jar and then build the image and when deploying in to the kubernetes , i will be using the dev , test and prod respective deployment.yaml in which i will be loading the url,username and password to env so the application will read it during prod start up
So when i am trying to build jar application try to connect to the database and it failed to create the jar.
Please let me know my understanding is wrong or right if wrong then how to correct it and just one thing is that i can't change the process i.e jar+ image + kubernetes
In Kubernetes you can put your configuration in Configmap or Secret. You can package the spring boot application and provide the Configmap entry as env variable of your container as exposed here
Using Spring Cloud Kubernetes you can also read these properties without any configuration on the container as explained in this article
I am using Spring Boot 1.4.3 and have a whole bunch of tests that are annotated with #DataJpaTest. By default, they run against an in-memory database. I would like to be able to run all of them against a local MySQL temporarily. How can I do this in an easy way?
I have found that I can make it work for one by adding #ActiveProfiles("local") where I have an application-local.properties that points to my local MySQL, but it is just too much work to add that everywhere, run the tests and then remove it again (since I only want to run this manually against MySQL, the CI environment will run against the in memory db).
I am using Maven if that would matter.
UPDATE:
So I have an application-local.properties which contains the db properties to connect to my local MySQL database (Which I use already to run my application against the local MySQL)
Then I right-click in IntelliJ on a package and select "Run all tests in package". In the settings of that run configuration, I add -Dspring.profiles.active=local to the "VM options" field.
I would have thought that this would activate the local profile during the tests, but it does not. If I stop the local MySQL, the tests still run fine.
In the docs it states that you are able to remove the autoconfigured h2 datasource with #AutoConfigureTestDatabase(replace= Replace.NONE) on the test class https://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test.
Also you then need to provide your db setup in properties, so that it does not use your app properties e.g.:
# Database
spring.datasource.url=jdbc:mariadb://localhost:3303/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
i put this in application.properties in the test package
You can add the profile with the MySQL datasource properties in the same application.properties (or .yml) as:
application.yml
# Existing properties
---
spring:
profiles: h2
# More h2-related properties
---
spring:
profiles: postgres
database:
driverClassName: org.postgresql.Driver
datasource:
url: jdbc:postgresql://localhost:5432/db_dvdrental
username: user_dvdrental
password: changeit
jpa:
database: POSTGRESQL
generate-ddl: false
# More postgres-related properties
and either use #ActiveProfiles("postgres") in an integration test class or start teh container using VM argument as:
java -Dspring.profiles.active=h2 ...
Add application.yml(properties) with jdbc connection into src/test/resources
Run your JPA test with #AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE) - it disables using embedded database (h2), otherwise :
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource': Invocation of init method
failed; nested exception is java.lang.IllegalStateException: Failed to
replace DataSource with an embedded database for tests. If you want an
embedded database please put a supported one on the classpath or tune
the replace attribute of #AutoConfigureTestDatabase.