ORMLiteforeignCollection:必须使用ClosableIterator?
关于使用 ORMLite 的快速问题。我正在努力确保我的实施是正确的。文档中有一部分讨论了 closableIterators 以及访问它如何加载 LazyForeignCollection 类,并且需要关闭它(或读到最后)才能关闭数据库连接:
注意:与 Dao.iterator() 方法一样,惰性集合返回的迭代器在使用完毕后必须关闭,因为下面有一个与数据库打开的连接。如果您一直遍历迭代器或对其调用 close() ,则会发生关闭。只有ForeignCollection返回可关闭的迭代器。
所以我的问题很简单:只能通过 closeableIterator 访问集合吗?我是否可以像使用任何其他 Java 集合一样使用 Collection/ForeignCollection 对象,而不用担心数据库连接问题(例如:foreach 循环)?
quick question about using ORMLite. I am trying to make sure that my implementation is correct. There's a part of the documentation that talks about closableIterators and how accessing this loads the LazyForeignCollection class and it needs to be closed (or read to the end) for the database connection to be closed:
NOTE: Like with the Dao.iterator() method, the iterator returned by a lazy collection must be closed when you are done with it because there is a connection open to the database underneath. A close happens either if you go all of the way through the iterator or if you call close() on it. Only the ForeignCollection returns a closable iterator.
So my question is simply: can the collection be accessed only through the closableIterator? Am I able to just use a Collection / ForeignCollection object as I would any other Java collection and not worry about the database connection stuff (say for example: foreach loop)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为文档足以解释这一点。问题是完成后需要关闭连接,否则与 SQL 数据库的连接将保持打开状态。如果您使用
for (Account account : accountDao)
类型的模式,则只有在您全部完成后才会关闭连接桌子。如果您使用break
或其他语句(return、goto、exception 等)在中间跳出循环,则 ORMLite 不会自动关闭连接。如果您要跳出循环,则文档中指定了要使用的正确模式。 http://ormlite.com/docs/iterator
您还可以使用“包装的迭代器”,它允许你用for循环在finally中完成关闭。
I thought the documentation would be enough to explain this. The issue is that the connection needs to be closed when you are done otherwise the connection to the SQL database will be left open. If you use a
for (Account account : accountDao)
type of pattern then the connection will only be closed if you go all of the way through the table. If you use abreak
or other statement (return, goto, exception, etc.) to break out of the loop in the middle then the connection will not be closed automatically by ORMLite.If you are going to break out of your loop then the proper pattern to use is specified in the docs. http://ormlite.com/docs/iterator
You can also use the "wrapped iterable" which allows you to do the close in the finally with for loops.