在Spring Boot应用程序中创建自定义连接池

时间:2018-12-19 06:25:27

标签: java spring spring-boot hikaricp snowflake

我正在编写一个与Snowflake Data Warehouse连接并在其上执行SQL查询的Spring Boot应用程序。 我编写了一个Configuration类,用于配置数据源以连接到Snowflake数据仓库,如下所示:

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class DBConfig {

    Logger logger = LoggerFactory.getLogger(DBConfig.class);
    @Bean
    JdbcTemplate jdbcTemplate() throws IllegalAccessException, InvocationTargetException, InstantiationException {
        logger.info("-----Configuring JDBCTemplate------");
        SnowflakeBasicDataSource dataSource = new SnowflakeBasicDataSource();
        dataSource.setServerName("<myserver>.snowflakecomputing.com");
        dataSource.setUser("<my_username>");
        dataSource.setPassword("<my_password>");
        dataSource.setWarehouse("DEMO_WH");
        dataSource.setDatabaseName("DEMO_DB");
        dataSource.setSchema("PUBLIC");
        return new JdbcTemplate(dataSource);
    }
}

我的pom.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.vaibhav</groupId>
    <artifactId>snowflake-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>snowflake-1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.snowflake</groupId>
            <artifactId>snowflake-jdbc</artifactId>
            <version>3.6.21</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我必须在Spring Boot应用程序中为此数据源使用连接池。

如何在应用程序中使用可以与自定义数据源完美配合的HikariCP连接池?

------编辑---感谢您提供解决方案,最后我的工作代码如下

@Configuration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public class DBConfig {

    Logger logger = LoggerFactory.getLogger(DBConfig.class);

    @Bean
    JdbcTemplate jdbcTemplate() throws IllegalAccessException, InvocationTargetException, InstantiationException {
        logger.info("-----Configuring JDBCTemplate------");

        HikariConfig config = new HikariConfig();
        config.setDriverClassName("net.snowflake.client.jdbc.SnowflakeDriver");
        // config.setDataSourceProperties(properties);
        config.setJdbcUrl("jdbc:snowflake://<myserver>.snowflakecomputing.com/?warehouse=DEMO_WH&db=DEMO_DB&schema=PUBLIC");
        config.setUsername("<my_username>");
        config.setPassword("<my_password>");
        HikariDataSource ds = new HikariDataSource(config);

        return new JdbcTemplate(ds);
    }
}

3 个答案:

答案 0 :(得分:1)

请参阅设置SnowflakeDriver with Hikari

  HikariConfig config = new HikariConfig();    
  config.setDriverClassName("com.snowflake.client.jdbc.SnowflakeDriver");    
  config.setDataSourceProperties(properties);    
  config.setJdbcUrl(connectStr);    
  HikariDataSource ds = new HikariDataSource(config);

答案 1 :(得分:0)

Hikari是Spring-boot 2+中的默认连接池

  

如果要在基于应用程序的应用程序中使用Hikari,则无需执行任何操作   在Spring Boot 2.x上。

您可以通过application.yml / application.properties设置连接池的不同属性。以下是application.yml的示例:

spring:
  datasource
    hikari:
      maximumPoolSize: 4 # Specify maximum pool size
      minimumIdle: 1 # Specify minimum pool size
      driver-class-name: com.snowflake.client.jdbc.SnowflakeDriver

这是用于配置Hikari CP的useful link

答案 2 :(得分:0)

如果包含spring-jdbc,Spring将根据可用的数据源自动创建JdbcTemplate。因此,如果上述答案不能满足您的要求,您可以尝试:

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class DBConfig {

    Logger logger = LoggerFactory.getLogger(DBConfig.class);

    // TAKE NOTE THAT THIS MIGHT ALREADY BE DONE BY SPRING
    @Bean
    protected JdbcTemplate jdbcTemplate( DataSource dataSource )
    {
        return new JdbcTemplate( dataSource );
    }    

    @Bean
    protected DataSource makeDataSource() throws IllegalAccessException, InvocationTargetException, InstantiationException {
        logger.info("-----Configuring JDBCTemplate------");
        SnowflakeBasicDataSource dataSource = new SnowflakeBasicDataSource();
        dataSource.setServerName("<myserver>.snowflakecomputing.com");
        dataSource.setUser("<my_username>");
        dataSource.setPassword("<my_password>");
        dataSource.setWarehouse("DEMO_WH");
        dataSource.setDatabaseName("DEMO_DB");
        dataSource.setSchema("PUBLIC");
        return dataSource;
    }
}