0

I have a Springboot application using MSSQL which I am deploying on Azure. It uses ActiveDirectoryMSI for Authentication. The Data Source Config class looks as follows

@Configuration
@Slf4j
public class DataSourceConfig {

    @Value("${sql.databaseName}")
    private String sqlDbName;

    @Value("${sql.serverName}")
    private String sqlServer;

    @Value("${sql.msiClientId}")
    private String sqlIdentity;

    @Bean
    public void connectToDb() {

        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setServerName(sqlServer + ".database.windows.net");
        ds.setDatabaseName(sqlDbName);
        ds.setMSIClientId(sqlIdentity);
        ds.setAuthentication("ActiveDirectoryMSI");

        try (Connection connection = ds.getConnection()) {
            log.info("Connected to database using my MSI");
        } catch (SQLServerException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

All the values for these variables are stored in KeyVault. The issues is after building, when I try and deploy the app, it expects a url in application.yml, which I don't have as all that information is supposed to come from Keyvault on azure. So it gives me following error. I can't give it a url because this MSI way does not require a url

***************************
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).

Here is my pom.xml. I have not put anything related to Spring.datasource in application.yml

<dependencies>
        <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.8</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
    </dependencies>

Any idea how I can rectify this issue? Thanks

BrownTownCoder
  • 1,312
  • 5
  • 19
  • 25
  • hi, interesting, perhaps this might be of interest https://stackoverflow.com/questions/51221777/failed-to-configure-a-datasource-url-attribute-is-not-specified-and-no-embedd – IronMan Jan 04 '21 at 19:49
  • @IronMan I think the issue is it expects a url in application.yml but if I give it one, it will try to connect to it instead of used datacourse config. I need to somehow tell the app not to worry about datasource variables – BrownTownCoder Jan 04 '21 at 19:52

1 Answers1

0

Figured it out. Had to change scope for h2 dependency from test to runtime

BrownTownCoder
  • 1,312
  • 5
  • 19
  • 25