Spring Boot & PostgreSQL- HikariCP always returns null

user2868900 :

I am trying to create a spring boot application without JPA / Hibernate (due to a complex DB structure, so I need more control over the queries)

I am having some trouble with getting the DataSource to work, it is only returning Null instead of a DataSource.

These are my Pom.xml dependencies:

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.5</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

This is the application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username= postgres
spring.datasource.password = postgres
spring.datasource.driverClassName = org.postgresql.ds.PGSimpleDataSource
spring.datasource.dataSourceClassName = org.postgresql.ds.PGSimpleDataSource

And here is my Connection class returning the datasource:

@Configuration
@PropertySource({"classpath:application.properties"})
public class Conn {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }

}

This is my RequestHandler where I am trying to create the connection (right now logging, and it always returns null).

@RestController
public class Test implements ErrorController {

    private DataSource ds;
    private static final String PATH = "/error";


    @RequestMapping("/connectToDb")
    public void doSomething() {
        ds = new Conn().dataSource();
        System.out.println(ds);
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }

}

Whenever I try to use the actual DataSource for prepared statements I receive the error:

HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.

Been trying to change the application.properties as well as trying different methods, but nothing has been working so far. Similar posts I have found have had the same error message, but I have yet to find a solution for the problem.

Any input on this? Thanks.

Michael :

The problem is here

@RestController
public class Test implements ErrorController {

    @RequestMapping("/connectToDb")
    public void doSomething() {
        ds = new Conn().dataSource(); // <<<<<
        System.out.println(ds);
    }
}

You can't simply create a new instance of the configuration. If you do that then you are essentially ignoring all of the annotations. You specifically need the instance that Spring has already created, which you could do via autowiring.

You don't need to pass the whole configuration, though, you can just autowire that bean.

@RestController
public class Test implements ErrorController {

    private final DataSource ds;
    private static final String PATH = "/error";

    public Test(DataSource ds) {
        this.ds = ds;
    }

    @RequestMapping("/connectToDb")
    public void doSomething() {
        System.out.println(ds);
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=159946&siteId=1