I have a local SQL Server instance. If I try to connect with Management Studio (SQL Server authentication, with username and password) everything works fine.
However, when I try to create a connection from a Spring Boot application I get some errors.
This is the code for the Datasource (dbUsername and dbpassword are correct)
@Bean
public DataSource getDataSource() throws SQLServerException {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser(dbUsername);
ds.setPassword(dbPassword);
ds.setServerName("localhost");
ds.setPortNumber(1433);
ds.setDatabaseName("mydb");
return ds;
}
When I try retrieve a connection with
Connection connection = dataSource.getConnection();
I get this error:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '"sa"'.
If I add ds.setIntegratedSecurity(true); I have a different error:
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.
This is the JDBC driver that I'm using:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre8</version>
</dependency>
Any suggestions?
SQLServerDataSource? In a web application it is better to use a data source that provides connection pooling. Spring Boot out of the box uses HikariCP for that, where you can define the datasource entirely in the config, without having to define a bean like this.