【问题标题】:How to enable connection pooling in spring boot embedded tomcat如何在 Spring Boot 嵌入式 tomcat 中启用连接池
【发布时间】:2019-08-28 09:50:36
【问题描述】:

我有一个不是 Web 应用程序的 Spring Boot 应用程序。在这个应用程序中,我在以下 bean 的帮助下配置了嵌入式 tomcat。

@豆 public TomcatServletWebServerFactory tomcatFactory() {

    return new TomcatServletWebServerFactory() {

        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatWebServer(tomcat);
        }

        protected void postProcessContext(Context context) {
            ContextResource contextResource = new ContextResource();
            contextResource.setName("jdbc/BPMDB");
            contextResource.setType(DataSource.class.getName());
            contextResource.setProperty("driverClassName", env.getProperty("bpm.db.driverClassName"));
            contextResource.setProperty("url", env.getProperty("bpm.db.url"));
            contextResource.setProperty("username", env.getProperty("bpm.db.username"));
            contextResource.setProperty("password", env.getProperty("bpm.db.password"));
            context.getNamingResources().addResource(contextResource);
        }
    };

}

我如何为这个嵌入式 tomcat 做连接池。我正在使用 spring boot 2.x,它说 hikaricp 是默认的连接池,但是如何将它设置到这个嵌入式 tomcat 中。 这是否需要设置诸如 spring.datasource.hikari.initial-size=15 之类的属性 spring.datasource.hikari.max-wait=20000

但同样,boot 将如何知道以及我将如何知道这些属性已被使用。

谢谢。

【问题讨论】:

  • 放弃这个bean方法,你基本上就完成了。将属性从 bpm.db 重命名为 spring.datasource 就完成了。
  • @M.Deinum 那么嵌入式服务器将在没有这个 bean 的情况下运行吗?
  • 当然会,我为什么还要提到这个。我强烈建议阅读 Spring Boot 参考指南和/或 Spring.io 网站中的一些指南/教程来学习 Spring Boot。
  • HikariCP 的可用性取决于依赖关系,希望您使用过这两个启动器 spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa 中的任何一个。除非您定义自己的数据源 bean,否则始终自动配置。
  • @NirajJha 是的,我正在使用 spring-boot-starter-data-jpa。这会启用与 hikari 的连接池吗?

标签: spring-boot java-8 hikaricp embedded-tomcat


【解决方案1】:

我的问题得到了答案。

很简单。我们只需要创建一个 DataSource 引用并自动装配它,并提及与数据库相关的属性以及与 hikari 相关的属性。

代码如下。

@Autowired
public DataSource dataSource; 

将上面添加到您的@Configuration 标记类,并将以下属性添加到 application.properties 文件。

spring.datasource.driver-class=...
spring.datasource.url=jdbc:oracle:thin:....
spring.datasource.username=..
spring.datasource.password=..

spring.datasource.hikari.initial-size=15
spring.datasource.hikari.max-wait=20000
spring.datasource.hikari.max-active=50
spring.datasource.hikari.max-idle=50
spring.datasource.hikari.min-idle=8

我还编写了一个测试用例来检查 hikari 连接池。下面是代码。

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(
    properties = "spring.datasource.type=com.zaxxer.hikari.HikariDataSource",
    classes = {ApplicationConfiguration.class,PersistenceJpaContext.class}
)
public class HikariConnectionPoolTest {


    @Autowired
    private DataSource dataSource;

    @Test
    public void hikariConnectionPoolIsConfigured() {
        assertEquals("com.zaxxer.hikari.HikariDataSource", dataSource.getClass().getName());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2020-07-10
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 2015-11-03
    • 2016-05-28
    相关资源
    最近更新 更多