简体   繁体   English

打开与数据库服务器的jdbc连接是否会降低应用程序的性能?

[英]Will Open jdbc connections to a database server slow down the performance of an application?

I have an automation script to test database which creates connection to db server and verifies table values. 我有一个自动化脚本来测试数据库,它创建与数据库服务器的连接并验证表值。 Meanwhile the application has got very slow. 同时应用程序非常慢。 Is there any relation between open connections and performance of application? 开放连接和应用程序性能之间是否有任何关系?

When you are done with using your Connection, you need to explicitly close it by calling its close() method in order to release any other database resources (cursors, handles, etc) the connection may be holding on to. 完成使用Connection后,需要通过调用close()方法显式关闭它,以释放连接可能保留的任何其他数据库资源(游标,句柄等)。

   Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        // Do stuff
        ...

    } catch (SQLException ex) {
        // Exception handling stuff
        ...
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) { /* ignored */}
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) { /* ignored */}
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) { /* ignored */}
        }
    }

It is always better to close the database/resource objects after usage. 使用后关闭数据库/资源​​对象总是更好。 Better to close connection, resultset and statement objects in the finally block. 最好关闭finally块中的连接,结果集和语句对象。

Until Java7, all these resources needs to be closed using a finally block. 在Java7之前,需要使用finally块关闭所有这些资源。 If you are using Java 7, then for closing the resources you can use a try-with-resources as follows. 如果您使用的是Java 7,那么要关闭资源,您可以使用try-with-resources ,如下所示。

try(Connection con = getConnection(url, username, password, "org.postgresql.Driver");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
) {

//statements
}catch(....){}

Not necessarily. 不必要。 In fact, it is not expected that open connections will slow down the application's performance, unless it is specifically justified . 事实上,除非特别合理 ,否则开放连接不会降低应用程序的性能。

Performance may be affected by three factors: 性能可能受三个因素的影响:

  • CPU usage. CPU使用率。
  • Memory occupation. 记忆占领。
  • I/O traffic. I / O流量。

Should a JDBC connection, just for being opened and not for being used, increase the CPU usage? JDBC连接只是用于打开而不是用于增加CPU使用率吗? In principle no, unless the driver is running in background some thread of its own. 原则上不行,除非驱动程序在后台运行一些自己的线程。

Should it occupy a lot of memory? 它应该占用大量内存吗? In principle no, because JDBC APIs are designed to recover data thorugh cursors (It should not occupy more than a simple working buffer) - unless the driver is not doing a correct garbage collection of used udata. 在原则上没有,因为JDBC API旨在恢复thorugh 游标数据(它不应该占比简单的工作缓冲区更多) -除非司机是不是做使用UDATA的正确的垃圾收集。

Should it do a lot of I/O traffic? 它应该做很多I / O流量吗? In principle no, unless the driver is doing some polling or something in background. 原则上不,除非司机在后台进行一些轮询或其他事情。

So, as you can see, the answer is that it depends on the JCBC driver implementation . 因此,正如您所看到的,答案是它取决于JCBC驱动程序的实现 Closing a connection as soon as it is not used is a good practice for releasing resources at the server side, but usually it is not critical at the client side. 一旦不使用就关闭连接是在服务器端释放资源的一种很好的做法,但通常在客户端并不重要。

If you open and close connections very frequently I would suggest using a JDBC connection pool, any JDBC connection pool will do. 如果您经常打开和关闭连接,我建议使用JDBC连接池,任何JDBC连接池都可以。

The pool keeps track of the use/reuse/multiplexing of the connections without opening anc closing them frequently. 池跟踪连接的使用/重用/多路复用,而无需经常关闭它们。 This way the database load becomes lighter. 这样,数据库负载变得更轻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM