gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:19 27 4
gpt4 key购买 nike

考虑以下代码片段:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com