【问题标题】:How to check connection pool settings values of spring boot application w/ multiple data sources如何检查带有多个数据源的 Spring Boot 应用程序的连接池设置值
【发布时间】:2019-12-14 15:25:34
【问题描述】:

我确实有使用 spring data jpa 的 spring boot 应用程序。我正在连接到多个数据源(不同的数据库)。我想检查两个数据源的连接池设置值。

最简单的方法是什么?

我使用的是 Spring Boot 版本 1.5.4.RELEASE。我的数据库是 DB2。

【问题讨论】:

    标签: spring-boot spring-data-jpa connection spring-boot-actuator


    【解决方案1】:

    一般应用:

    import javax.sql.DataSource;
    
    @SpringBootApplication
    public class SpringBootConsoleApplication implements CommandLineRunner {
    
        @Autowired
        DataSource dataSource;
    
        public static void main(String[] args) throws Exception {
                SpringApplication.run(SpringBootConsoleApplication.class, args);
            }
    
        @Override
        public void run(String... args) throws Exception {
    
            System.out.println("DATASOURCE = " + dataSource);
    
        }
    }
    

    用于测试:

    import javax.sql.DataSource;
    
    import static org.junit.Assert.assertEquals;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class BankConceptTests {
        @Autowired
        private DataSource dataSource;
        @Test
        public void contextLoads() {
            System.out.println("DATASOURCE = " + dataSource);
            assertEquals("com.zaxxer.hikari.HikariDataSource", dataSource.getClass().getName());
        }
    }
    

    DataBaseRepository 打印连接池详细信息的示例类:

    package com.threadminions.service;
    
    import javax.sql.DataSource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class DatabaseRepository {
    
        @Autowired
        DataSource dataSource;
    
        /***
         * If you are using DBCP connection pooling then use this method to get the connection details
         */
        /*public void printDBCPConnectionDetails()
        {
            BasicDataSource basicDataSource = (BasicDataSource) dataSource;
            System.out.println("Instace of DBCP basic data source: " + basicDataSource);
            System.out.println("Driver class name: " + basicDataSource.getDriverClassName());
            System.out.println("Max idle connection: " + basicDataSource.getMaxIdle());
            System.out.println("Total connection: " + basicDataSource.getMaxTotal());
        }*/
    
        /***
         * If using Hikari connection pooling
         */
        /*public void printHikariConnectionDetails()
        {
            HikariDataSource ds = (HikariDataSource)dataSource;
            System.out.println("Instace of DBCP basic data source: " + ds);
            System.out.println("Driver class name: " + ds.getDriverClassName());
            System.out.println("Connection Pool size : " + ds.getMaximumPoolSize());
            System.out.println("Url: " + ds.getJdbcUrl());
    
        }*/
    
        /***
         * If using C3P0 connection pooling
         */
        /*public void printC3P0ConnectionDetails()
        {
            ComboPooledDataSource ds = (ComboPooledDataSource) dataSource;
            System.out.println("Instace of DBCP basic data source: " + ds);
            System.out.println("Connection Pool size : " + ds.getMaxPoolSize());
            System.out.println("Min connection pool size: " + ds.getMinPoolSize());
            System.out.println("Max statements: " + ds.getMaxStatements());
            System.out.println("Url: " + ds.getJdbcUrl());
        }*/
    }
    

    查看详情:https://threadminions.com/2017/12/25/spring-boot-with-different-connection-pooling/

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 2020-01-13
      • 2018-10-16
      • 2019-12-14
      • 2019-03-15
      • 2019-08-05
      • 2015-07-10
      • 2020-10-26
      • 2018-09-11
      相关资源
      最近更新 更多