V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
noble4cc
V2EX  ›  Java

jdbc 中每 create 一个 Statement 就是和数据库建立一个会话吗?

  •  
  •   noble4cc · 2019-01-19 19:23:28 +08:00 · 2075 次点击
    这是一个创建于 2169 天前的主题,其中的信息可能已经有所发展或是发生改变。

    还是说仅仅执行 sql 的工具,mysql 的会话是是在 sql 执行的过程中创建的

    另外 Statement 的 close 有什么用?不关闭会有问题吗

    4 条回复    2019-01-21 09:34:26 +08:00
    snappyone
        1
    snappyone  
       2019-01-19 21:46:18 +08:00
    Connection 才是你所说的会话
    Statement 是会话中执行的一条语句
    close 的作用就是释放资源,不关闭会导致内存泄漏
    noble4cc
        2
    noble4cc  
    OP
       2019-01-20 17:47:12 +08:00
    @snappyone 但是 Statement 不会被 vm 回收不就不会内存泄露了?

    另外 mysql 中的 connect 和 session 是两个概念,一个 connect 可能会有好几个 session
    但是到了 jdbc 这里就合二为一了?
    snappyone
        3
    snappyone  
       2019-01-21 08:05:50 +08:00 via Android
    @noble4cc 连接和会话的区别我又查了下,感觉一个偏物理层一个偏应用层,但是总体基本一个意思。close 内存泄露这个可以直接看 jdk 的解释,愿意是 release resources as soon as possible,不释放是有可能造成问题的,具体应该应该跟垃圾回收机制有关
    wleexi
        4
    wleexi  
       2019-01-21 09:34:26 +08:00
    * A Connection object represents a connection to a data source via a JDBC technology-enabled driver. The data source can be a DBMS, a legacy file system, or some other source of data with a corresponding JDBC driver. A single application using the JDBC API may maintain multiple connections. These connections may access multiple data sources, or they may all access a single data source.
    From the JDBC driver perspective, a Connection object represents a client session. It has associated state information such as user ID, a set of SQL statements and result sets being used in that session, and what transaction semantics are in effect.

    * The Statement interface defines methods for executing SQL statements that do not contain parameter markers. The PreparedStatement interface adds methods for setting input parameters, and the CallableStatement interface adds methods for retrieving output parameter values returned from stored procedures.


    * An application calls the method Connection.close() to indicate that it has finished using a connection. All Statement objects created from a given Connection object will be closed when the close method for the Connection object is called.


    jsr221
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2594 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:08 · PVG 13:08 · LAX 21:08 · JFK 00:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.