Example usage for org.springframework.boot.jdbc DataSourceBuilder create

List of usage examples for org.springframework.boot.jdbc DataSourceBuilder create

Introduction

In this page you can find the example usage for org.springframework.boot.jdbc DataSourceBuilder create.

Prototype

public static DataSourceBuilder<?> create() 

Source Link

Usage

From source file:org.apache.nifi.registry.db.DataSourceFactory.java

private DataSource createDataSource() {
    final String databaseUrl = properties.getDatabaseUrl();
    if (StringUtils.isBlank(databaseUrl)) {
        throw new IllegalStateException(NiFiRegistryProperties.DATABASE_URL + " is required");
    }//from  w  ww  .jav  a 2  s .c om

    final String databaseDriver = properties.getDatabaseDriverClassName();
    if (StringUtils.isBlank(databaseDriver)) {
        throw new IllegalStateException(NiFiRegistryProperties.DATABASE_DRIVER_CLASS_NAME + " is required");
    }

    final String databaseUsername = properties.getDatabaseUsername();
    if (StringUtils.isBlank(databaseUsername)) {
        throw new IllegalStateException(NiFiRegistryProperties.DATABASE_USERNAME + " is required");
    }

    String databasePassword = properties.getDatabasePassword();
    if (StringUtils.isBlank(databasePassword)) {
        throw new IllegalStateException(NiFiRegistryProperties.DATABASE_PASSWORD + " is required");
    }

    final DataSource dataSource = DataSourceBuilder.create().url(databaseUrl).driverClassName(databaseDriver)
            .username(databaseUsername).password(databasePassword).build();

    if (dataSource instanceof HikariDataSource) {
        LOGGER.info("Setting maximum pool size on HikariDataSource to {}",
                new Object[] { properties.getDatabaseMaxConnections() });
        ((HikariDataSource) dataSource).setMaximumPoolSize(properties.getDatabaseMaxConnections());
    }

    return dataSource;
}

From source file:org.springframework.boot.jdbc.DataSourceBuilderTests.java

@Test
public void defaultToHikari() {
    this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").build();
    assertThat(this.dataSource).isInstanceOf(HikariDataSource.class);
}

From source file:org.springframework.boot.jdbc.DataSourceBuilderTests.java

@Test
public void specificTypeOfDataSource() {
    HikariDataSource hikariDataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();
    assertThat(hikariDataSource).isInstanceOf(HikariDataSource.class);
}