何时关闭JDBC中的Connection,Statement,PreparedStatement和ResultSet [英] When to close Connection, Statement, PreparedStatement and ResultSet in JDBC

查看:528
本文介绍了何时关闭JDBC中的Connection,Statement,PreparedStatement和ResultSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于JDBC编码的几个问题:


  1. 对于单个客户端应用程序,我们需要一个连接池吗?

  2. 这是一个好主意,在开始创建一个连接,并保持它活着,而不关闭它,直到应用程序退出?为什么?

  3. PreparedStatement与Connection相关联,如果我的连接在每次查询后未关闭,为什么不保留PreparedStatement并在其他方法中重复使用?

  4. 如果我们创建PreparedStatement每个查询,数据库知道它是相同的PreparedStaement和第一次后忽略不必要的操作?

  5. PreparedStatement不创建一次并重用多次语句?

  6. $ b 我知道对close()的调用会释放资源。但是如果我们知道以后要使用它,为什么要释放它,然后再次请求它呢?



    多客户端应用程序如何?我们需要连接池,所以我们需要每次创建并关闭Connection,Statement和PreparedStatement。



    感谢,

    解决方案

    我个人会使用池,因为这将为你处理所有的资源管理。如果您的连接要求更改,那么您可以轻松地修改池配置。通过池,您可以根据最佳实践打开/关闭连接和准备语句



    通常,在使用池时:




    • 关闭连接实际上只是将其返回到池

    • 准备语句的操作将从Connection的语句高速缓存检索先前准备的语句,

    • 关闭PreparedStatement的操作实际上只是将其返回到连接的语句缓存。



    此外 - 根据池实现 - 它可能能够在有资源泄漏时通知您,以便更容易识别代码中的这些问题。



    查看示例实现的源代码,如 DBCP - 这很有趣,看看它们是如何工作的。


    Few questions on JDBC coding:

    1. For a single client application, do we need a Connection pool?
    2. Is it a good idea to create a Connection at beginning and keep it alive without close it until application exit? Why?
    3. PreparedStatement is associated with Connection, if my connection is not closed after each query, why not keep the PreparedStatement alive and reuse it in other methods?
    4. if we create PreparedStatement each query, does database knows it is the same PreparedStaement and ignore unnecessary actions after the first time?
    5. PreparedStatement is not create once and reuse many times statement? if yes, why need close it each time?

    I know the call to close() will release resource. But If we know we are going to use it later, why release it and then request it again later?

    How about multi-client application? we need connection pool and so we need to create and close Connection, Statement and PreparedStatement each time?

    thanks,

    解决方案

    Personally I'd use a pool as this will take care of all of the resource management for you. If your connection requirements change then you can easily modify the pool configuration. With a pool in place you can open/close connections and prepared statements according to best-practice and leave the resource management to the pool.

    Typically, when using a pool:

    • closing a connection will actually just return it to the pool
    • the act of preparing a statement will either retrieve a previously prepared statement from the Connection's statement cache, or if one is not available, create a new statement and cache it for later use.
    • the act of closing a PreparedStatement will actually just return it to the connection's statement cache.

    Furthermore - depending on the pool implementation - it may be able to notify you when there are resource leaks making it easier to identify these sorts of problems in your code.

    Take a look at the source of an example implementation like DBCP - it's quite interesting to see how they work.

    这篇关于何时关闭JDBC中的Connection,Statement,PreparedStatement和ResultSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆