Spring Boot app: Failed to determine a suitable driver class - spring-boot

I tried to connect springboot;
When I run, I get an error "Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."
My pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
spring:
datasource:
dbcp2:
driver-class-name: com.mysql.cj.jdbc.driver
url: jdbc:mysql://localhost:3306/dbName
username: root
password: ********(pass)
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
show-sql: true
mysql config
(https://i.stack.imgur.com/RjdYl.png)
Output
***************************
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).
Iam unable to run it

I believe dbcp2 stands in your way. Remove it from YAML
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbName
username: root
password: ********(pass)
or follow this:
https://javadeveloperzone.com/spring-boot/spring-boot-common-dbcp2-connection-pool/

Related

Why is my Spring Consul Retry configuration not working

I am trying to use retry functionality in spring consul so that the application fail-fast after certain number of retries.
I am using following bootstrap.yml
spring:
profiles:
active: dev
application:
name: consultest
cloud:
consul:
token: ${token}
enabled: true
host: ${host}
port: 8500
retry:
initial-interval: 5000
max-attempts: 6
max-interval: 600000
multiplier: 3
profiles: dev
Also my pom.xml has following dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
I am using springboot version 2.2.2.RELEASE.
The problem is that whenever i start the application and it is not able to connect to consul, it only tries one time and then the application fails
Following is the log,
2023-01-25 09:08:16.245 ERROR 1166405 --- [ main] o.s.c.c.c.ConsulPropertySourceLocator : Fail fast is set and there was an error reading configuration from consul.
2023-01-25 09:08:16.252 ERROR 1166405 --- [ main] o.s.boot.SpringApplication : Application run failed
As you can see the application does not retry.
Am i missing some configuration? Can anyone help with this issue?

Liveness probe failed when using lettuce instead of jedis in spring-starter-data-redis

I have a spring boot application using spring cloud bom : Hoxton.SR1
The app is deployed in a kubernetes cluster.
To connect the app to the redis database I tested 2 different configuration :
Jedis connection pool configuration :
pom.xml :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
application.yml :
spring:
redis:
host: localhost
port: 6379
password: password
database: 0
jedis:
pool:
min-idle: 0
max-idle: 12
max-active: 12
max-wait: 500ms
Deployment in the K8S cluster works well.
Lettuce connection pool configuration :
pom.xml :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--lettuce connection pool-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
application.yml :
spring:
redis:
host: localhost
port: 6379
password: password
database: 0
lettuce:
pool:
min-idle: 0
max-idle: 12
max-active: 12
max-wait: 500ms
Deployment in the K8S cluster DOES NOT WORK, error is : Liveness probe failed: Get http://10.68.13.246:8080/actuator/health: net/http: request canceled (Client.Timeout exceeded while awaiting headers).
What am I missing ?
What could be the root cause of this ?
Is it related to spring actuator or lettuce connector ?

java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.isValid(I)Z Spring Boot with Intellij

I'm trying to run my spring boot application with IntelliJ, and I'm using Oracle JDBC
I got always this error :
java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.isValid(I)Z
I thought it's a dependency problem, I deleted all .m2 repository and I reimported the project but I get the same error
this my application.properties :
server:
port: 9000
spring:
profiles: dev
datasource:
url: jdbc:oracle:thin:#localhost:1521:xe
username: me
password: me
driver-class-name: oracle.jdbc.OracleDriver
jpa:
database-platform: org.hibernate.dialect.Oracle10gDialect
hibernate:
ddl-auto: update
show-sql: true
autoconfigure:
exclude: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
data:
rest:
base-path: /api
liquibase:
enabled: false
change-log: classpath:db/changelog/changelog-master.yaml
this is oracle dependency in my pom.xml :
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
You need to specify the JDBC version that you want to use. Refer to the blog for more details.
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version> 19.3.0.0 </version>
<scope>runtime</scope>
</dependency>

Trouble when changing Spring Boot version from 2.0.3.RELEASE to 2.1.0.BUILD-SNAPSHOT

I have a working code that stops to work when I change Spring Boot version from 2.0.3.RELEASE to 2.1.0.BUILD-SNAPSHOT.
Sometimes the error is:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'dataSource', defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
or ...
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-16 14:38:18.509 ERROR 604 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'scopedTarget.oauth2ClientContext', defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2RestOperationsConfiguration$SessionScopedConfiguration$ClientContextConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/security/oauth2/config/annotation/web/configuration/OAuth2ClientConfiguration$OAuth2ClientContextConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Both cases it is related to duplicated bean os something else.
My POM dependencies are:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
<maven.test.skip>true</maven.test.skip>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
and the config:
server:
error:
include-stacktrace: always
whitelabel:
enabled: false
servlet:
session:
cookie:
name: HYDRASSESSION
port: 36205
management:
endpoints:
web:
exposure:
include: "*"
security:
basic:
enabled: false
oauth2:
client:
clientId: atlas
clientSecret: secret
accessTokenUri: http://myserverip:36202/oauth/token
userAuthorizationUri: http://myserverip:36202/oauth/authorize
resource:
userInfoUri: http://myserverip:36202/user/me
spring:
jpa:
properties:
hibernate:
temp:
use_jdbc_metadata_defaults: false
hibernate:
ddl-auto: validate
application:
name: atlas
datasource:
password: admin
username: postgres
url: jdbc:postgresql://myserverip:36211/atlas?ApplicationName=Atlas
guardiao:
logout:
path: http://myserverip:36202/exit
It run fine when using 2.0.3.RELEASE but I'm receiving an error when try to login myserver:36202/oauth/authorize?client_id=: by browser complains ERR_TOO_MANY_REDIRECTS but I have no errors in console. Anyway... my question is: how can I update my SpringBoot to 2.1.0.BUILD-SNAPSHOT ?
So as the exception reports, there are two beans of the same type. Historically Spring would override one bean with the other. That has long been an annoyance since you could get hard to find bugs where a second bean with a completely different type, but with the same bean ID would make your first bean vanish.
Spring Boot 2 now disables that sort of bean overriding by default. You can re-enable it by setting the following property in your application.yml:
spring.main.allow-bean-definition-overriding: true
This re-enables the previous behavior. It doesn't address the root cause that beans are being overridden though and also mean you won't get the benefit of bean override errors. Upgrading the underlying libraries will hopefully clean this up over time.
As noted on other comments, upgrading the spring-security-oauth2-autoconfigure dependency to org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.RELEASE may solve it for you.
This answer might be helpful for those who have setup a new project and adding the spring.main.allow-bean-definition-overriding: true property in application.properties file is not fixing the issue:
Check the folder where your application.properties file is present -- it needs to be available directly under the directory: src/main/resources/ and not anywhere else.

How to externalise spring application configuration using config server?

I have written my own config server to centralise configuration management and exposing application configuration using APIs. Now i want to consume configuration properties from spring and spring-boot applications. But i am not able to figure out the correct way for that. I tried placing my config server client code to listen for application context start event and reading configuration from config server. But i am not able to inject this configuration into other beans.
How can i read application configuration from config server (using rest client) and inject this read config to application environment for further processing ?
create an application that for config and your application
#SpringBootApplication
#EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ConfigApplication.class, args);
}
}
application.yml in config server
spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native
security:
user:
password: pass
server:
port: 8888
and create shared folder in src/main/resources/shared and your-service.yml like this
spring:
security:
basic:
enabled: false
server:
port: 8083
maven pom.xml in config server
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>config</finalName>
</configuration>
</plugin>
</plugins>
</build>
in your-service spring boot application add /src/main/resources/bootstrap.yml
like this
spring:
application:
name: your-service
cloud:
config:
uri: http://localhost:8888
fail-fast: true
username: user
password: pass
and add this dependency to your service
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
first run config server and then your spring boot service

Resources