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)?
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.