Connect to MSSQL v18 from Spring boot application using windows authentication - java

I am currently using the below properties to connect to localhost from Java spring boot application:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:sqlserver://localhost:1433;database=vlad;integratedSecurity=true
spring.jpa.properties.hibernate.format_sql=true
springdoc.api-docs.path=/api-docs
From Java spring boot this is not working. The error I get is:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

You will most likely need to configure your Dialect and Include a driver:
application.properties:
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2016Dialect
You will also most likely need to add driver and auth dependency to your pom.xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc_auth</artifactId>
<version>10.2.1.x86</version>
<type>dll</type>
</dependency>
If that doesn't do the trick, you might have to manually download the Driver:
https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-2017
Hope it helps

Related

Springboot application start up fails and missing logs when using azure.keyvault.secret.property-sources

I am trying to migrate my springboot application written in kotlin to azure.
I added spring-cloud-azure-starter-keyvault-secrets:4.5.0 dependency in my app, and added below configurations to application.properties.
my.secret=${my-azure-secret}
spring.cloud.azure.keyvault.secret.property-source-enabled=true
spring.cloud.azure.keyvault.secret.property-sources[0].endpoint=<my-vault>.vault.azure.net/
After I add this, the spring boot initialization doesn't work any more and i can't find any logs.
log.info { "Log before springboot initialize" }
SpringApplication(MyApplication::class.java).run(*args)
log.info { "Log after springboot initialize" }
So, Log before springboot initialize is logged, and after that nothing happens (or I can't find any logs afterwards)
I already verified it is not related to any logback settings because if I remove the property spring.cloud.azure.keyvault.secret.property-sources[0].endpoint from application.properties, it boots up properly.
(I also tried to add the additional dependency spring-cloud-azure-dependencies:4.5.0 )
Any clues/hints what is happening and how to resolve it ?
Thanks.
Here I was able to boot the spring application using the same dependencies.
But here I used the latest version of spring-cloud-azure-starter-keyvault-secrets i.e. 5.0.0 also my spring boot version is 3.0.2
my dependencies:
<dependencies>
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/com.azure.spring/spring-cloud-azure-starter-keyvault-secrets -->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-keyvault-secrets</artifactId>
<version>5.0.0</version>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency></dependencies>
Also I use windows so instead of my_azure_secret instead of the my-azure-secret
output:

MySQL57Dialect has been deprecated; use org.hibernate.dialect.MySQLDialect instead

In my Java Spring Boot project I try to migrate User entity into mysql database.
When I mvn spring-boot:run, I see following error:
HHH90000026: MySQL57Dialect has been deprecated; use org.hibernate.dialect.MySQLDialect instead
Here's my application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?createDatabaseIfNotExist=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database=mysql
Here's the dependency for mysql:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
What am I missing?
With the help of Raushan Kumar I solved the connection problem with:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

Flyway - EmbeddedMysql failing - SpringBoot Junits

When I am running JUnits, those are executing fine in local but failing in Jenkins Pipeline with below error:
SQL State : 08001
Error Code : 0
Message : Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Below are pom.xml dependencies:
<!-- https://mvnrepository.com/artifact/com.wix/wix-embedded-mysql -->
<dependency>
<groupId>com.wix</groupId>
<artifactId>wix-embedded-mysql</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.flywaydb/flyway-core -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
Properties of MySQL:
spring.jpa.database=mysql
spring.datasource.url=jdbc:mysql://localhost:2205/?autoReconnect=true&useSSL=false
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.main.allow-bean-definition-overriding=true
# HIBERNATE
spring.jpa.properties.hibernate.proc.param_null_passing=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# FLYWAY
spring.flyway.enabled=true
spring.flyway.schemas=test
Can anyone please suggest why it failing with above error message when running Junits in Jenkins pipeline?

How to integrate Keycloak 7 with Spring Boot 2

I want to setup the Keycloak adapter (v7.0.0) for a Spring Boot 2.1.x backend. Somehow this doesn't work as expected and mvn clean spring:boot-run shows errors.
To get a playground i cloned this working repo. After a successful build with the old versions of Keycloak and Spring Boot i changed the dependencies (and versions).
For the POM i included:
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
<version>7.0.0</version>
</dependency>
and
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.keycloak.bom</groupId>
<artifactId>keycloak-adapter-bom</artifactId>
<version>7.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
and provide properties via the application.yml file:
keycloak:
auth-server-url: http://127.0.0.1:8081/auth/
resource: persons-app
realm: PersonRealm
public-client: true
principal-attribute: preferred_username
This follows Keycloaks docs.
The result is an error showing:
Description:
Parameter 1 of method setKeycloakSpringBootProperties in org.keycloak.adapters.springboot.KeycloakBaseSpringBootConfiguration required a bean of type 'org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver' that could not be found.
Action:
Consider defining a bean of type 'org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver' in your configuration.
This is already defined. So thats the problem. I don't know if there are certain restriction of what versions of Keycloak and Spring Boot work together. Maybe someone could explain what i missed here or what versions to set. Thanks!
The same question has been asked here: Issues running example keycloak spring-boot app I'll point you to the other one for more information about the issue and a temporary workaround.
At the moment there is no ultimate solution, it's an issue with the Keycloak Spring Boot Adapter 7.0.0. If you use the previous version of the adapter (6.0.1) then it will work fine, even if the Keycloak server is running version 7.0.0 (you just don't get the new features introduced in the new adapter).

Spring Boot Hikari can't find DriverClassName

I have a spring boot project,It run just fine when I execute via eclipse Project > Run as > spring boot app
but when I build the project and execute it using java -jar myproject.jar or run it using mvn spring-boot:run it throw this error
Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:
Property: driverclassname
Value: com.microsoft.sqlserver.jdbc.SQLServerDriver
Origin: "driverClassName" from property source "source"
Reason: Failed to load driver class com.microsoft.sqlserver.jdbc
.SQLServerDriver in either of HikariConfig class loader or Thread context classloader
Action:
Update your application's configuration
my sql server connector dependency
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
<scope>test</scope>
</dependency>
and here my application.properties
spring.datasource.url=jdbc:sqlserver://mydb;databaseName=HTSdb
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
it looks my app can't find the sqlserver driver but it is already in project classpath,any suggestion? thanks in advance
I think the issue is with dependency scope which is set as test.
Scope test indicates that dependency isn't required at standard runtime of application and should only be used for purpose of test runs only!
Usually database connectors dependency are set with runtime scope.
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
<scope>runtime</scope>
</dependency>
Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:
Property: driver-class-name
Value: org.postgresql.Driver
Origin: "driverClassName" from property source "source"
Reason: Failed to load driver class org.postgresql.Driver in either of HikariConfig
class loader or Thread context classloader
Action:
Update your application's configuration
Just had the same error, in my case with "org.postgresql.Driver".
Gues what was the issue? an empty space after "org.postgresql.Driver " by mistake.
So, be aware about that ' ' invisible empty space :).
Add dependency
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
check if Jar is loaded in lib if not then you need to add version 8.2.1.jre8 and rebuild the project .
You can validate by checking if mssql-jdbc-8.2.1.jre8 jar is there loaded in project and it has SQLServerDriver.class file in it .

Categories

Resources