SpringBoot service application port 1433 problem with external datasource config values

34 Views Asked by At

I am developing a simple web service application in Spring boot.I am using HikariCP, MSSQL server 2012 My application passes all tests and runs successfully on server, when my datasource settings are in application.yml or application.properties which are in the classpath. but if i use an external file from an external path in the computer (file is not located in war/classpath) @PropertySource("file:path/to/the/file), app can't pass test results and throws an exception indicates that HikariCP can't be initialized because of this error:

The TCP/IP connection to the host "Host_X/DB_X, port 1433 has failed. Error: "Host_ X/DBX". Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

App has access to that file and folder, I checked the values from the file, everything is read well, evertyhing is correct.

I don't understand why same datasource config values are placed in application.properties or application.yml, no exceptions, no errors, everything runs fine. but when I remove the data source config values from application.yml or application,properties and load them from external file, it throws the ecxeption about, so the HikariCP can't be initiated and application cannot start.

can anyone tell me why?

I read all the documentation in spring boots official site about externalizing configuration settings https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/boot-features-external-config.html https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/howto-data-access.html

here is my application.properties:

server.port=9119
server.compression.enabled=true
server.compression.min-response-size=64
spring.profiles.active=local
# Logging
logging.level.org.springframework.security=info
logging.level.org.springframework.web=info
logging.level.com.zaxxer.hikari.HikariConfig=info
logging.level.com.zaxxer.hikari.HikariDataSource=debug

here is my DataSourceConfiguration class:

@Configuration
@Profile("local")
@PropertySource("file:C/path/to/database.properties")
@Primary
public class DataSourceConfiguration {

    @Value("${spring.datasource.url}")
    private String jdbcUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.cachePrepStmts}")
    private boolean cachePrepStmts;

    @Value("${spring.datasource.prepStmtCacheSize}")
    private int prepStmtCacheSize;

    @Value("${spring.datasource.prepStmtCacheSqlLimit}")
    private int prepStmtCacheSqlLimit;

    @Value("${spring.datasource.hikari.connection-timeout}")
    private long connectionTimeout;

    @Value("${spring.datasource.hikari.idleTimeout}")
    private long idleTimeout;

    @Value("${spring.datasource.hikari.minimum-idle}")
    private int minimumIdle;

    @Value("${spring.datasource.hikari.pool-name}")
    private String poolName;

    @Value("${spring.datasource.hikari.leak-detection-threshold}")
    private long leakDetectionThreshold;

    @Bean
    public HikariDataSource dataSource() {
        System.out.println(new Date().toString() + ": creating dataSource");
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(username);
        config.setPassword(password);
        config.setDriverClassName(driverClassName);
        config.addDataSourceProperty("cachePrepStmts", cachePrepStmts);
        config.addDataSourceProperty("prepStmtCacheSize", prepStmtCacheSize);
        config.addDataSourceProperty("prepStmtCacheSqlLimit", prepStmtCacheSqlLimit);
        config.setConnectionTimeout(connectionTimeout);
        config.setIdleTimeout(idleTimeout);
        config.setMinimumIdle(minimumIdle);
        config.setPoolName(poolName);
        config.setLeakDetectionThreshold(leakDetectionThreshold);
        return new HikariDataSource(config);
    }
}

datasource.properties

spring.datasource.url=jdbc:sqlserver://host_srv\sql_inst;databaseName=XXX
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.cachePrepStmts=true
spring.datasource.prepStmtCacheSize=250
spring.datasource.prepStmtCacheSqlLimit=2048
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.pool-name=fido
spring.datasource.hikari.leak-detection-threshold=60000

i will be appreciated if anyone can help me about this.

hikari 3.2.0 java 1.8 spring boot 2.1.4

My HikariDataSource is @autowired in my Controller

0

There are 0 best solutions below