检测数据库连接泄漏的最佳方法

检测数据库连接泄漏的最佳方法介绍数据库连接不是免费的,这就是首先使用连接池解决方案的原因。但是,单独的连接池并不能解决与管理数据库连接相关的所有问题。应用程序开发人员必须确保Connection在不再需要时关闭每一个。在幕后,连接池提供了一个逻辑事务,当它被关闭时,它会返回到池中,以便其他并发事务可以进一步重用它。当连接被获取而从未被关闭时,就会发生连接泄漏。何时应检测到连接泄漏?每个关系数据库都提供了一种检查底层连接状态的方法,因此可以轻松打开一个新的SQL终端并检查是否有任何悬空连接。但是,这种简约的方法是

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

介绍

数据库连接不是免费的,这就是首先使用连接池解决方案的原因。但是,单独的连接池并不能解决与管理数据库连接相关的所有问题。应用程序开发人员必须确保Connection在不再需要时关闭每一个。在幕后,连接池提供了一个逻辑事务,当它被关闭时,它会返回到池中,以便其他并发事务可以进一步重用它。

当连接被获取而从未被关闭时,就会发生连接泄漏。

何时应检测到连接泄漏?

每个关系数据库都提供了一种检查底层连接状态的方法,因此可以轻松打开一个新的 SQL 终端并检查是否有任何悬空连接。但是,这种简约的方法是错误的,因为它意味着我们将应用程序的损坏版本部署到生产环境中。

在测试期间应检测连接泄漏,从而防止在生产环境中发生连接泄漏。

这篇文章将演示如何仅使用单元测试来自动化连接池检测。这种方法使我们能够在我们的实际代码库以及我们的测试例程中检测连接泄漏。如果单元测试正在泄漏连接,那么当达到最大数据库连接阈值时,持续集成过程将中断。

连接检漏仪

要检查给定的测试类是否泄漏连接,我们将检查 JUnit 测试运行器使用给定类之前和之后的悬空连接数:

1
2
3
4
5
6
7
8
9
10
11
12
13
@BeforeClass
public static void initConnectionLeakUtility() {
    if ( enableConnectionLeakDetection ) {
        connectionLeakUtil = new ConnectionLeakUtil();
    }
}
 
@AfterClass
public static void assertNoLeaks() {
    if ( enableConnectionLeakDetection ) {
        connectionLeakUtil.assertNoLeaks();
    }
}

ConnectionLeakUtil实用程序如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class ConnectionLeakUtil {
 
    private JdbcProperties jdbcProperties = JdbcProperties.INSTANCE;
 
    private List idleConnectionCounters =
        Arrays.asList(
            H2IdleConnectionCounter.INSTANCE,
            OracleIdleConnectionCounter.INSTANCE,
            PostgreSQLIdleConnectionCounter.INSTANCE,
            MySQLIdleConnectionCounter.INSTANCE
    );
 
    private IdleConnectionCounter connectionCounter;
 
    private int connectionLeakCount;
 
    public ConnectionLeakUtil() {
        for ( IdleConnectionCounter connectionCounter :
            idleConnectionCounters ) {
            if ( connectionCounter.appliesTo(
                Dialect.getDialect().getClass() ) ) {
                this.connectionCounter = connectionCounter;
                break;
            }
        }
        if ( connectionCounter != null ) {
            connectionLeakCount = countConnectionLeaks();
        }
    }
 
    public void assertNoLeaks() {
        if ( connectionCounter != null ) {
            int currentConnectionLeakCount = countConnectionLeaks();
            int diff = currentConnectionLeakCount - connectionLeakCount;
            if ( diff > 0 ) {
                throw new ConnectionLeakException(
                    String.format(
                        "%d connection(s) have been leaked! Previous leak count: %d, Current leak count: %d",
                        diff,
                        connectionLeakCount,
                        currentConnectionLeakCount
                    )
                );
            }
        }
    }
 
    private int countConnectionLeaks() {
        try ( Connection connection = newConnection() ) {
            return connectionCounter.count( connection );
        }
        catch ( SQLException e ) {
            throw new IllegalStateException( e );
        }
    }
 
    private Connection newConnection() {
        try {
            return DriverManager.getConnection(
                jdbcProperties.getUrl(),
                jdbcProperties.getUser(),
                jdbcProperties.getPassword()
            );
        }
        catch ( SQLException e ) {
            throw new IllegalStateException( e );
        }
    }
}

IdleConnectionCounter接口定义了使用特定于数据库的实现来计算非活动连接数的协定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface IdleConnectionCounter {
 
    /**
     * Specifies which Dialect the counter applies to.
     *
     * @param dialect dialect
     *
     * @return applicability.
     */
    boolean appliesTo(Class<? extends Dialect> dialect);
 
    /**
     * Count the number of idle connections.
     *
     * @param connection current JDBC connection to be used for querying the number of idle connections.
     *
     * @return idle connection count.
     */
    int count(Connection connection);
}

对于我们在测试期间使用的每个受支持的 Hibernate Dialect,都
需要有一个IdleConnectionCounter实现,以便我们可以检查泄漏连接的数量。

H2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class H2IdleConnectionCounter implements IdleConnectionCounter {
 
    public static final IdleConnectionCounter INSTANCE =
        new H2IdleConnectionCounter();
 
    @Override
    public boolean appliesTo(Class<? extends Dialect> dialect) {
        return H2Dialect.class.isAssignableFrom( dialect );
    }
 
    @Override
    public int count(Connection connection) {
        try ( Statement statement = connection.createStatement() ) {
            try ( ResultSet resultSet = statement.executeQuery(
                    "SELECT COUNT(*) " +
                    "FROM information_schema.sessions " +
                    "WHERE statement IS NULL" ) ) {
                while ( resultSet.next() ) {
                    return resultSet.getInt( 1 );
                }
                return 0;
            }
        }
        catch ( SQLException e ) {
            throw new IllegalStateException( e );
        }
    }
}

甲骨文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class OracleIdleConnectionCounter implements IdleConnectionCounter {
 
    public static final IdleConnectionCounter INSTANCE =
        new OracleIdleConnectionCounter();
 
    @Override
    public boolean appliesTo(Class<? extends Dialect> dialect) {
        return Oracle10gDialect.class.isAssignableFrom( dialect );
    }
 
    @Override
    public int count(Connection connection) {
        try ( Statement statement = connection.createStatement() ) {
            try ( ResultSet resultSet = statement.executeQuery(
                    "SELECT COUNT(*) " +
                    "FROM v$session " +
                    "WHERE status = 'INACTIVE'" ) ) {
                while ( resultSet.next() ) {
                    return resultSet.getInt( 1 );
                }
                return 0;
            }
        }
        catch ( SQLException e ) {
            throw new IllegalStateException( e );
        }
    }
}

PostgreSQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class PostgreSQLIdleConnectionCounter implements IdleConnectionCounter {
 
    public static final IdleConnectionCounter INSTANCE =
        new PostgreSQLIdleConnectionCounter();
 
    @Override
    public boolean appliesTo(Class<? extends Dialect> dialect) {
        return PostgreSQL91Dialect.class.isAssignableFrom( dialect );
    }
 
    @Override
    public int count(Connection connection) {
        try ( Statement statement = connection.createStatement() ) {
            try ( ResultSet resultSet = statement.executeQuery(
                    "SELECT COUNT(*) " +
                    "FROM pg_stat_activity " +
                    "WHERE state ILIKE '%idle%'" ) ) {
                while ( resultSet.next() ) {
                    return resultSet.getInt( 1 );
                }
                return 0;
            }
        }
        catch ( SQLException e ) {
            throw new IllegalStateException( e );
        }
    }
}

MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MySQLIdleConnectionCounter implements IdleConnectionCounter {
 
    public static final IdleConnectionCounter INSTANCE =
        new MySQLIdleConnectionCounter();
 
    @Override
    public boolean appliesTo(Class<? extends Dialect> dialect) {
        return MySQL5Dialect.class.isAssignableFrom( dialect );
    }
 
    @Override
    public int count(Connection connection) {
        try ( Statement statement = connection.createStatement() ) {
            try ( ResultSet resultSet = statement.executeQuery(
                    "SHOW PROCESSLIST" ) ) {
                int count = 0;
                while ( resultSet.next() ) {
                    String state = resultSet.getString( "command" );
                    if "sleep".equalsIgnoreCase( state ) ) {
                        count++;
                    }
                }
                return count;
            }
        }
        catch ( SQLException e ) {
            throw new IllegalStateException( e );
        }
    }
}

测试时间

我构建了这个实用程序,以便我们可以跟踪Hibernate ORM项目中所有泄漏连接的单元测试。当针对 运行它时hibernate-core,我可以很容易地发现罪魁祸首测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
:hibernate-core:test
 
org.hibernate.jpa.test.EntityManagerFactoryClosedTest > classMethod FAILED
    org.hibernate.testing.jdbc.leak.ConnectionLeakException
 
org.hibernate.jpa.test.EntityManagerFactoryUnwrapTest > classMethod FAILED
    org.hibernate.testing.jdbc.leak.ConnectionLeakException
 
org.hibernate.jpa.test.cdi.NoCdiAvailableTest > classMethod FAILED
    org.hibernate.testing.jdbc.leak.ConnectionLeakException
 
org.hibernate.jpa.test.factory.SynchronizationTypeTest > classMethod FAILED
    org.hibernate.testing.jdbc.leak.ConnectionLeakException

当我打开 的报告时EntityManagerFactoryClosedTest,我什至可以看到有多少连接被泄露:

1
org.hibernate.testing.jdbc.leak.ConnectionLeakException: 1 connection(s) have been leaked! Previous leak count: 0, Current leak count: 1

SynchronizationTypeTest甚至表明以前的连接也存在泄漏:

1
org.hibernate.testing.jdbc.leak.ConnectionLeakException: 1 connection(s) have been leaked! Previous leak count: 2, Current leak count: 3

结论

检测连接泄漏是每个企业应用程序的强制性要求。虽然您可以找到定期运行并终止所有空闲数据库连接的脚本,但这只是一种创可贴的方法。

处理连接泄漏的最佳方法是修复底层代码库,以便始终正确关闭连接。为了确保生产环境没有连接泄漏,每个集成测试都必须验证实际测试的源代码或测试逻辑本身没有泄漏连接。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191784.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 5G LTE窄带物联网(NB-IoT) 10

    5G LTE窄带物联网(NB-IoT) 10第7章物理子层物理子层是底层子层,负责MACPDU的物理信道,传输和接收;如图7.1所示。RRC提供PHY子层的配置参数。在MAC/PHY接口,传输信道在发送和接收时分别映射到物理信道,反之亦然[28]。 RRC将其配置参数发送到每个子层,包括PHY子层,如第4.2,5.2,6.2和7.1节所示。7.1RRC配置参数RRC将专用或默认无线电配置参数发送到PHY子层,…

    2022年10月6日
    2
  • 最火的C语言编程软件,适合编写C语言代码的编程软件有哪些

    最火的C语言编程软件,适合编写C语言代码的编程软件有哪些C语言基本上是大学计算机及其相关专业在大一上学期就会开的一门课程,但是很多学生就是在大一上学期期末的时候很着急,因为自己完全没有学好C语言,感觉一学期白学了,其实究其主要原因,还是因为你在上课认真听了,也做了课堂作业,但是却没有在课后好好的自己去主动敲代码,笔者不能让你有多主动去自己实践,但是笔者可以给你介绍几款更好的写代码的软件(手机电脑都可以)。C语言作为一门起源比较早的编程语言,可以编程的手…

    2022年5月29日
    69
  • Inno Setup:x64 vs x86

    Inno Setup:x64 vs x86BydefaultInnoSetupalwaysinstallsyourapplicationin32-bitmode.Butifyourapplicationiscompiledto64-bitbinaries(atleast,partly)youwill,probably,wanttouse64-bitmode.Firstthin

    2022年6月7日
    35
  • Windows服务器如何修改SQL server内存大小等设置

    Windows服务器如何修改SQL server内存大小等设置

    2021年6月1日
    114
  • 固态硬盘有哪些协议知识点?「建议收藏」

    固态硬盘有哪些协议知识点?「建议收藏」固态硬盘的知识点固态硬盘的协议:同品牌,同型号,不同容量速度差距删除数据是真的将数据删除了吗?固态硬盘的协议:硬盘是属于NVM:Non-volatilememory非易失性存储器件。NVM的种类​接口总线协议:​我们固态硬盘一般有两种接口的固态,一种是SATA接口,一种是M.2接口的固态。SATA固态硬盘接口​M.2接口的固态:,这个有两种的接口,一种是2个金手指…

    2022年9月20日
    3
  • 堆排序实现及应用

    堆排序实现及应用

    2022年2月6日
    45

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号