HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required

11,028

This should work for you.

# DataSource settings: Database configurations
spring.datasource.url = jdbc:mysql://localhost:3306/db_example
spring.datasource.username = springuser
spring.datasource.password = password

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

Have you try the Spring Data Repositories? If you use the Spring Data Repositories, you don't have to specify a Datasource object. If you want to implement repositories, you can follow this example:

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends CrudRepository<Product, Long> {

}

To call the service on a MVC @Service.

@Service
public class ProductService {

  @Autowired
  ProductRepository productRepository;

  public List<Product> findAll() {
    List<Product> results = null;

    results = productRepository.findAll();

    return results;

  }
} 
Share:
11,028
MDP
Author by

MDP

Updated on December 06, 2022

Comments

  • MDP
    MDP over 1 year

    I'm trying to following this guide to create a bean that gives me a DataSource Object, but when I try to access the Datasource, for example this way:

    Connection connection  = datasource.getConnection();
                Statement stmt=connection.createStatement();  
                ResultSet rs=stmt.executeQuery("select * from products");   
    

    I get this error:

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

    I edited my code many times, since I read various examples, that are always slightly different.

    This is the last version of my code

    @Configuration
    @ComponentScan("com.packagename.webstore")
    public class RootApplicationContextConfig {
    
        @Bean 
        @ConfigurationProperties(prefix = "spring.datasource")
        public HikariDataSource  dataSource() {     
            return DataSourceBuilder.create().type(HikariDataSource.class).build();
    
        } 
    }
    

    This is the application.properties file inside src/main/resources folder:

    spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/db_example
    spring.datasource.username=springuser
    spring.datasource.password=password
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    

    This are my dependencies:

    enter image description here

    Does anybody understand what is my error?? Thank you

    • pvpkiran
      pvpkiran over 5 years
      i think it shud be like this spring.datasource.driver-class-name
    • MDP
      MDP over 5 years
      I already tried in a previous attempt, but I got the same error :(
    • M. Deinum
      M. Deinum over 5 years
      spring.datasource.url instead of spring.datasource.jdbc-url. You don't need the spring.datasource.driverClassName.
    • M. Deinum
      M. Deinum over 5 years
      Also you are using Spring Boot so why configure the datasource manually? You don't need the datasource bean as Spring Boot already configures one for you. The same applies to the @ComponentScan. So basically you should be able to remove your RootApplicationContextConfig and have the same (or better result).
    • MDP
      MDP over 5 years
      I think I'm making a lot of confusion. Actually I'm using a Spring mvc since I'm Building a web application. That's why I have RootApplicationContextConfig . But I dont' understand how to create bean that gives me a DataSource object
    • user7294900
      user7294900 over 5 years