首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何跟踪数据库连接泄漏

如何跟踪数据库连接泄漏
EN

Stack Overflow用户
提问于 2011-04-21 16:58:19
回答 2查看 9K关注 0票数 8

我们有一个应用程序似乎有连接泄漏(SQL Server说已经达到最大池大小)。我一个人在我的开发机器上(很明显),仅仅通过导航应用程序,我就触发了这个错误。SQL Server活动监视器显示大量使用我的数据库的进程。

我想找出哪些文件打开了连接,但没有使用它。我在考虑使用像grep这样的东西来计算每个文件的".Open()“和".Close()”的数量,并获得这两个数字不相等的文件。这现实吗?

附加问题:在SQL Server活动监视器中找到的进程是否与连接相对应?如果没有,我如何找出我的数据库上有多少个连接是打开的?

该应用程序是带有SQL Server2005的asp.net (vb) 3.5。我们目前还没有使用LINQ或任何类似的东西。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2011-04-21 19:23:41

当从SQL Server端查看代码时,您可以运行以下查询来查看上一次在休眠连接上运行哪些查询。(未执行任何操作的打开连接)

代码语言:javascript
复制
SELECT ec.session_id, last_read, last_write, text, client_net_address, program_name, host_process_id, login_name
FROM sys.dm_exec_connections  ec
JOIN sys.dm_exec_sessions es
  ON ec.session_id = es.session_id
CROSS APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) AS dest
where es.status = 'sleeping'

在应用程序端,您可以使用sos.dll进行调试,如以下文章所述:

如果你需要更多关于如何使用windbg的信息,这些文章是一个很好的介绍:

票数 9
EN

Stack Overflow用户

发布于 2016-07-12 21:10:58

解决连接泄漏的最好方法是在测试期间进行。

您可以使用自动实用程序,以便每次测试都验证是否存在连接泄漏。

代码语言:javascript
复制
@BeforeClass
public static void initConnectionLeakUtility() {
    if ( enableConnectionLeakDetection ) {
        connectionLeakUtil = new ConnectionLeakUtil();
    }
}
 
@AfterClass
public static void assertNoLeaks() {
    if ( enableConnectionLeakDetection ) {
        connectionLeakUtil.assertNoLeaks();
    }
}

ConnectionLeakUtil如下所示:

代码语言:javascript
复制
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实现,MySQL版本如下:

代码语言:javascript
复制
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 );
        }
    }
}

现在,当你运行你的测试时,当一个连接被泄露时,你会得到一个失败:

代码语言:javascript
复制
:hibernate-core:test
 
org.hibernate.jpa.test.EntityManagerFactoryClosedTest > classMethod FAILED
    org.hibernate.testing.jdbc.leak.ConnectionLeakException
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5741813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档