英文:
Set maxConnectionLimit to Manually created connection using Spring DataSourceBuilder.create()
问题
使用application.properties文件,我们可以提供以下配置:
spring.datasource.hikari.maximum-pool-size=10
因为Spring默认使用Hikari连接池。但是,当我们根据特定需求手动创建数据源时,无法使用application.properties文件。
情境:
- 用户将动态配置数据源,并且此后应从该连接对象中获取连接。
- 为此,我们正在创建DatasourceBuilder.create().build()方法,以创建数据源连接对象并将其设置到Bean工厂中。
- 但是,在使用DataSourceBuilder.create().build()方法创建数据源连接对象时,它会同时创建连接池并创建10个与数据库的连接。
- 我们希望避免使用连接池,只保留一个连接。
我该如何实现这一点?
英文:
Using application.properties we can provide the
spring.datasource.hikari.maximum-pool-size=10
as spring using the Hikari connection pooling by default.
But when we are creating datasource manually as a specific requirement we cannot use the application.properties.
Scenario.
- User will configure the datasource dynamically and that connection object should be available from thereafter.
- For this we are creating DatasourceBuilder.create().build() method to create a datasource connection object and set it into the bean factory.
- But while creating a datasource connection object using DataSourceBuilder.create().build() method it is creating connection pooling and 10 connection to the database at the same time.
- We wanted to avoid that connection pooing and have only one connection.
How do I do that?
答案1
得分: 1
创建自定义数据源时,我们可以创建HikariDataSource的实例,而不是DataSource。
假设您正在使用Spring默认的连接池(Hikari):
public DataSource createCustomConnection(String driverClass, String url, String username, String password) {
HikariConfig config = new HikariConfig();
config.setDriverClassName(driverClass);
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);
config.setMaximumPoolSize(MENTIONED_TOTAL_CONNECTIONS);
// 像这样,您可以在此处配置多个属性
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}
DataSource dataSource = createCustomConnection(...) // 在此处传递参数
在这里,您创建了一个同时创建单个连接的数据源。
了解更多信息,请访问https://github.com/brettwooldridge/HikariCP#initialization
谢谢。
英文:
While creating the custom datasource we can create an instance of HikariDataSource instead of DataSource.
Assuming you are using Spring default Connection pooling (Hikari)
public DataSource createCustomConnection(String driverClass, String url, String username, String password) {
HikariConfig config = new HikariConfig();
config.setDriverClassName(driverClass);
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);
config.setMaximumPoolSize(MENTIONED_TOTAL_CONNECTIONS);
// Like this you can configure multiple properties here
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}
DataSource dataSource = createCustomConnection(...) // Pass Parameters here
Here you have created the datasource that creates a single connection at the same time.
for more information https://github.com/brettwooldridge/HikariCP#initialization
Thank you.
答案2
得分: 0
你可以尝试设置minimumIdle
属性。默认情况下,它设置为maxPoolSize
,因此最初会创建10个连接。你可以在此链接中查看minimumIdle
部分,链接为:https://github.com/brettwooldridge/HikariCP#frequently-used
英文:
You can try setting the minimumIdle property. By default it is set to maxPoolSize, so 10 connections are created initially. </p> You can refer minimumIdle section in this link, https://github.com/brettwooldridge/HikariCP#frequently-used
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论