如何检查带有多个数据源的 Spring Boot 应用程序的连接池设置值

问题描述 投票:0回答:1

我有使用 Spring Data JPA 的 Spring Boot 应用程序。我正在连接到多个数据源(不同的数据库)。

我想检查两个数据源的连接池设置值。

最简单的方法是什么?

我正在使用 Spring Boot 版本

1.5.4.RELEASE
。我的数据库是
DB2

spring-boot spring-data-jpa connection spring-boot-actuator
1个回答
2
投票

一般应用:

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/

© www.soinside.com 2019 - 2024. All rights reserved.