当原始resultSet从方法返回到新对象时会发生什么? [英] What happens to the original resultSet when it is returned from a method into a new object?

查看:80
本文介绍了当原始resultSet从方法返回到新对象时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伪代码可以更好地说明我的自我.我现在正在学习Java.

pseudo code to explain my self better. I'm learning Java at this point in time.

如果我有方法

public resultSet getEverything() 
{    
   resultSet rs = blabla;    
   return rs 
}

我无法使用rs.close(),因为我需要在检索它的方法中使用它 因此,我将使用它,并可能关闭"我创建的新resultSet.

I can't rs.close() as I need to use it in the method I retrieve it hence then I will use it, and maybe 'close' the new resultSet I create.

上一个resultSet会发生什么? 是否留给垃圾收集器收集? 当我关闭新" resultSet时,它会自行关闭吗? 对代码效率/性能有影响吗?

What happens from the previous resultSet? Is it left to be collected by the garbage collector? Does it close itself when I close the 'new' resultSet? Does it have any impact on code efficiency/performance?

任何反馈将不胜感激:),因为这使我有些困惑.我认为这更多是一个OOP问题,而不是Java. 谢谢!

Any feedback would be greatly appreciated :) as this has confused me a bit. It is more of an OOP question rather than Java I think. Thanks!

推荐答案

您不应该从方法中返回java.sql.ResultSet.应该始终创建它,将其映射到对象或数据结构中,并在finally块中创建它的方法的范围内将其关闭.

You should not be returning a java.sql.ResultSet from a method. It should always be created, mapping into an object or data structure, and closed in the scope of the method in which it was created in a finally block.

java.sql.ResultSet与数据库游标(一种稀缺资源)相关联.您不想长时间打开它们.

A java.sql.ResultSet is associated with a database cursor, a scarce resource. You don't want to keep those open for a long time.

垃圾收集器不会清除结果集,语句或与数据库的连接.它将从内存中删除引用,但是游标或连接仍将在数据库端打开.正确关闭它们是您的责任,否则您将耗尽所有供应.

Garbage collectors do not clean up result sets, statements, or connections to databases. It will remove the reference from memory, but the cursor or connection will still be open on the database side. It's your responsibility to close them properly or you'll exhaust the supply.

要返回正确的对象,数据结构或CachedRowSet.

An object, data structure, or CachedRowSet is the right thing to return.

这篇关于当原始resultSet从方法返回到新对象时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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