Spring 使用 pgBouncer 池启动 2
Spring Boot 2 with pgBouncer pooling
我有一个设置并启用了 pgBouncer 池的 PostgreSQL 11 服务器。
我想使用它的池化机制,而不是默认的 Hikari 和 Spring Boot 的 spring-boot-starter-data-jpa
附带的 Tomcat 池化。我已经从项目中禁用 HikariCP
和 tomcat-jdbc
,但是我不确定我需要进一步设置什么才能成功启动 Spring 应用程序。
我想我的问题是如何设置一个 Spring 应用程序,以便它不使用任何池机制与数据库通信,因为它将由数据库上的 pgBouncer
处理边?
我查看了各种类似问题的问题和答案,这导致我开始禁用 HikariCP
。但是,我找不到简明的 tutorial/example 如何在我的案例场景中完成这项工作。
任何帮助将不胜感激。
您可以通过将 maximumPoolSize 设置为 1
来禁用 Hikari 的连接池
This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections. Basically this value will determine the maximum number of actual connections to the database backend
spring.datasource.hikari.maximumPoolSize=1
原来我需要定义一个 DataSource
,所以我创建了一个这样的配置 class:
@Configuration
@ConfigurationProperties("spring.datasource")
public class DatabaseConfig {
@Value("${spring.datasource.url}")
private String uri;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Bean
public DataSource dataSource() throws SQLException {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(DriverManager.getDriver(uri));
dataSource.setUrl(uri);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
必须将其从依赖项中排除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
我遇到了类似的问题,在查看 spring 源代码后,我发现了一种更简单的方法,不需要任何代码或 pom 更改。
spring:
datasource:
type: org.springframework.jdbc.datasource.DriverManagerDataSource
driver-class-name: org.postgresql.Driver
此数据源将简单地替换 hikari 并每次都创建一个新连接而不是池化。
我有一个设置并启用了 pgBouncer 池的 PostgreSQL 11 服务器。
我想使用它的池化机制,而不是默认的 Hikari 和 Spring Boot 的 spring-boot-starter-data-jpa
附带的 Tomcat 池化。我已经从项目中禁用 HikariCP
和 tomcat-jdbc
,但是我不确定我需要进一步设置什么才能成功启动 Spring 应用程序。
我想我的问题是如何设置一个 Spring 应用程序,以便它不使用任何池机制与数据库通信,因为它将由数据库上的 pgBouncer
处理边?
我查看了各种类似问题的问题和答案,这导致我开始禁用 HikariCP
。但是,我找不到简明的 tutorial/example 如何在我的案例场景中完成这项工作。
任何帮助将不胜感激。
您可以通过将 maximumPoolSize 设置为 1
来禁用 Hikari 的连接池This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections. Basically this value will determine the maximum number of actual connections to the database backend
spring.datasource.hikari.maximumPoolSize=1
原来我需要定义一个 DataSource
,所以我创建了一个这样的配置 class:
@Configuration
@ConfigurationProperties("spring.datasource")
public class DatabaseConfig {
@Value("${spring.datasource.url}")
private String uri;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Bean
public DataSource dataSource() throws SQLException {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(DriverManager.getDriver(uri));
dataSource.setUrl(uri);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
必须将其从依赖项中排除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
我遇到了类似的问题,在查看 spring 源代码后,我发现了一种更简单的方法,不需要任何代码或 pom 更改。
spring:
datasource:
type: org.springframework.jdbc.datasource.DriverManagerDataSource
driver-class-name: org.postgresql.Driver
此数据源将简单地替换 hikari 并每次都创建一个新连接而不是池化。