java - 我是否应该始终关闭 CachedRowSet,即使它超出范围?

标签 java database jdbc cachedrowset

考虑以下代码片段:

public void getUsers() {
    CachedRowSet rowSet = new CachedRowSetImpl();
    .........     /* Initialize the rowset */
    String query = "SELECT user_id FROM users";
    rowSet.setCommand(query);
    rowSet.execute();
    .........    /* Do some job */

}

这里 CachedRowSet 对象在本地方法中使用。我需要在这里手动关闭它还是方法完成时它包含的内容会被自动抓取?

最佳答案

来自:http://docs.oracle.com/javase/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html

A CachedRowSet object is a disconnected rowset, which means that it makes use of a connection to its data source only briefly. It connects to its data source while it is reading data to populate itself with rows and again while it is propagating changes back to its underlying data source. The rest of the time, a CachedRowSet object is disconnected, including while its data is being modified. Being disconnected makes a RowSet object much leaner and therefore much easier to pass to another component. For example, a disconnected RowSet object can be serialized and passed over the wire to a thin client such as a personal digital assistant (PDA).

这意味着没有连接问题需要关闭 这样你就可以离开了

但我认为最好始终关闭您打开的内容,而不是将其留给您通常不接触或告知何时运行的 GC。

给定一个足够大的服务器并有很多请求,如果它导致打开文件描述符,它最终可能成为一个问题。 确保资源关闭总是最好的

关于java - 我是否应该始终关闭 CachedRowSet,即使它超出范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12278351/

相关文章:

java - ActiveMQ-Client osgi bundle ?

java - 检查进程是否在 windows/linux 上运行

node.js - Mongodb使用过滤器在子文档中获取记录

mysql - 哪些特殊情况不应关闭语句?

jdbc - Derby连接错误:错误XBM0H:无法创建目录/usr/opt/derby/db-derby-10.12.1.1-bin/bin/MyDbTest

java - 登录: enable class on specific appender et disable it on the others

java - Spring 预填表格不起作用

sql - 比较两个非标准格式的地址

mongodb - future 整个应用程序中是否可以使用基于文档的数据库来代替 RDBMS?

java - mysql jdbc 字段列表中的未知列 'startdate'