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

2019-01-19 19:23:28 +08:00
 noble4cc

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

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

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

另外 mysql 中的 connect 和 session 是两个概念,一个 connect 可能会有好几个 session
但是到了 jdbc 这里就合二为一了?
snappyone
2019-01-21 08:05:50 +08:00
@noble4cc 连接和会话的区别我又查了下,感觉一个偏物理层一个偏应用层,但是总体基本一个意思。close 内存泄露这个可以直接看 jdk 的解释,愿意是 release resources as soon as possible,不释放是有可能造成问题的,具体应该应该跟垃圾回收机制有关
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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/528647

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX