我正在编写一个春季启动应用程序,该应用程序与雪花数据仓库连接并在其上执行SQL查询。我写了一个配置类,用于配置数据源以连接到雪花数据仓库,如下所示:
@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);
}
}
请参阅用hikari设置雪花果:
HikariConfig config = new HikariConfig(); config.setDriverClassName("com.snowflake.client.jdbc.SnowflakeDriver"); config.setDataSourceProperties(properties); config.setJdbcUrl(connectStr); HikariDataSource ds = new HikariDataSource(config);
如果包括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;
}
}
hikari是Spring-Boot 2
中的默认连接池如果我们想在基于应用程序中使用hikari,我们无事可做 在春季靴子上2.x。
您可以通过应用程序设置连接池的不同属性。以下是应用程序的示例。
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的有用链接。