Springboot + JSP + Spring Security: Failed to configure a DataSource - mysql

I'm new using springboot and I've been triying to configure a project with it and spring security, but unfortunately I'm not able to run it. I'm getting the next error :
nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
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
I've been looking for a solution but I can't find anything that fit in my project.
Please, any idea?
You can see the project here :
Proyect
Thanks

In application.properties, Change
app.datasource.jdbc-url
app.datasource.username
app.datasource.password
app.datasource.driver-class-name
To
spring.datasource.url
spring.datasource.username
spring.datasource.password
spring.datasource.driver-class-name

Related

Spring Boot Application not configuring datasource through CircleCi

I have a Spring Boot Rest Api (v2.2.6.RELEASE) with a mysql database
When I ssh into the remote box and run java -jar it builds and runs perfectly but when I run that same command through CircleCi I get the following.
2021-12-25 17:53:30.112 ERROR 30084 --- [ 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
Below is an extract of the properties file:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=****
spring.datasource.username=*****
spring.datasource.password=*****
spring.datasource.initialization-mode=always
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Also, Im using the same user for both CircleCi and when I ssh and deploy manually.
Any help and guidance would be appreciated.

Spring Cloud Data Flow Kubernetes Deployment fails for external database - Cannot load driver class: com.mysql.cj.jdbc.Driver

I am following the developer guide to deploy SCDF to a minikube cluster on my local machine. Used the helm chart approach. Was able to get it working with the defaults. The default deploys a mariadb in the cluster. I wanted to change it to use an external mysql db that is running in a docker container in my machine (outside the cluster). Followed the recommendations to change the values.yaml to enable the external DB and attributes for external DB connection (URI, dbname, user/pwd etc.).
Then deployed using "helm install my-release -f values.yaml bitnami/spring-cloud-dataflow"
The SCDF pod (& skipper pod) errors out because it can't find the mysql jdbc driver. kubectl logs on the pods show the following error: "java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver"
How do I include the mysql jdbc driver in the image for SCDF that gets deployed (or resolve this problem). I read SCDF already includes the drivers for std databases (true ?). New to helm/k8s so apologies if solution is obvious... Other posts on similar error all talk about including this in the pom.xml . But this is not a dependency issue with my (task) app but SCDF itself.
thanks
-------------------- more detail on the exception stack ---------------
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657)
...
Looks like I just need to specify the mariadb jdbc driver in the values.yaml file for mysql as well. That got past the load class error.
But still would like to know how to prevent class load error for a different driver specified when SCDF is deployed to k8s via the helm chart.

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured with Spring, MySql and Gradle

I am working with Spring Boot and MySql. When I launch the application, I get the following 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 (no profiles are currently active).
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/*****
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
build.gradle
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "mysql:mysql-connector-java:$sqlClientVersion"
}
dependencies {
implementation 'mysql:mysql-connector-java:8.0.20'
}
Any help would be really appreciated

Spring Boot DataSource: 'url' attribute is not specified

I am trying to follow this guide
https://spring.io/guides/gs/accessing-data-mysql/
but this guide is for the maven and i am trying the gradle
and getting this error
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
in application.properties i have these things only.
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
I tried all the possible things there is on the SO.
the only deps i have are these
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
Or You will have to prevent Spring boot from automatically configuring the data source by adding this line to the file application.properties.
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
This is kind of an old question but for anyone else coming across this, IF by any chance you are using IntelliJ (as OP does here based on his/her comments) make sure that our beloved IDE recognises your application.properties/application.yml as such by going to File -> Project Structure -> Modules then select your resources file and click on "Resources" from the Mark as: header thing (based on IntelliJ Community Edition 2019.1). Also keep in mind, as no doubt the IDE will certainly notify you, that by reimporting any Maven changes you will need to do this procedure again.
Check the target classpath directory. If the application.properties file didn't exist, then delete the target and rebuild.
I would search the reason somewhere else.
Your error log tells you Reason: Failed to determine a suitable driver class. Please build your project, and check if mysql driver is included in a built jar file. If jar is missing try to change your mysql dependency scope from runtimeOnly to implementation.
The same problem can occur (at least I think so) if you try to run this project from IDE and solution should also help in that case.
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. error is unfortunately very generic and can happen in many different situations. For example last week I had received this error while using oracle Oracle when there where special signs in password which was specified in properties.yaml without double-quotes.
I was having the same problem and solved it by migrating my project that was in version 2.4.x, to 2.3.x.
in IDE / STS (spring tool suit), every thing was working fine
but when made a "project.jar" file,
this was thrown.
unnecessary spaces " " in the "application.yml" file can cause this.
server:
port: 8085
spring:
datasource:
url: jdbc:mysql://localhost:3306/studentdb
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
application:
name: STUDENT-SERVICE
instead of tweaking my "application.yml" file
i simply moved all my statements in "application.yml" file to
"application.properties" file and formatted the statements like required in ".properties".
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format.sql=true
spring.application.name=student-service
server.port=8085
and voilĂ 
(you can add params at the end of url)
(spring.datasource.url=jdbc:mysql://localhost:3306/studentdb?allowPublicKeyRetrieval=true&useSSL=false)
Make sure your pom.xml has the war packaging. It worked for me.
<packaging>war</packaging>
Spring boot has chanegd the url to jdbc-url.
You need to use as below
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/db_example
Document Link:
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-properties
Stressing that, of course, we would search the documentation for "spring.cloud.config.jdbc-url", but our search should be directly through spring.datasource.hikari.jdbc-url.

Hibernate Reverse Engineering with Eclipse and MySql

I'm having the hardest time getting Eclipse to connect and reverse engineer from a MySQL5 database. I can see Eclipse connecting to my MySQL database and can even see the tables through the "Data Source Explorer" view but when I try it after creating Hibernate Console and Configuration files, I get the error:
org.hibernate.console.HibernateConsoleRuntimeException:
Problems while loading database
driverclass (com.mysql.jdbc.Driver)
Problems while loading database
driverclass (com.mysql.jdbc.Driver)
java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
From my simple java project, I start by creating a Hibernate Configuration File (cfg.xml).
Name of file: hibernate.cfg.xml
Session Factory name: org.hibernate.SessionFactory
Database Dialect: org.hibernate.dialect.MySQL5Dialect
Driver Class: com.mysql.jdbc.Driver
Connection Url: jdbc:mysql://localhost:3306/<myDatabaseName>
Default Schema: <myDatabaseName>
Username: correct username
Password: correct password
I also have selected the option to "Create a console Configuration"
At this point, I can see the new configuration listed in my "Hibernate Configuration" perspective/workbench pane. If I try to expand it, I get the earlier listed error.
I don't understand why I can see the database through the 'Data Source Explorer" and even though I'm using the DB connection profile listed in there as part of my configuration, I still get this error.
I also tried to create a new database profile using a manual mysql connector jar (mysql-connector-java-5.1.13-bin.jar) and same end result.
Versions of what I have:
Eclipse version: 3.6.0 aka Helios, Build 20100617 - 1415
MySQL: 5.1.34
Hibernate Tools (from JBoss): HibernateTools-3.3.1.v201006011046R-H111-GA
(placed into Eclipse's 'dropins' folder)
What am I doing wrong in my hibernate configuration setup?
Help!
Your Hibernate Console Configuration doesn't have the MySQL JDBC driver on its classpath, hence the java.lang.ClassNotFoundException:com.mysql.jdbc.Driver. From the reference guide of the Hibernate Tools:
3.4. Creating a Hibernate Console Configuration
...
alt text http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/images/plugins/plugins_3.png
...
Classpath: The classpath for loading POJO and JDBC drivers; only needed if
the default classpath of the Project
does not contain the required classes.
Do not add Hibernate core libraries or
dependencies, they are already
included. If you get ClassNotFound
errors then check this list for
possible missing or redundant
directories/jars.
Include default classpath from project: When enabled the project
classpath will be appended to the
classpath specified above
Does the "associated project" have the MySQL JDBC driver declared as library? If not, then you must add it (either as a project library or in the above tab).