[ 
https://issues.apache.org/jira/browse/JENA-1601?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16634346#comment-16634346
 ] 

Andy Seaborne commented on JENA-1601:
-------------------------------------

SDB is (I believe) little used these days. If anyone wants to provide a PR For 
any fixed in SDB, I'll certainly process it.

Jena should use iterators consistently - which includes 
internal-close-on-completion - but it is not checked automatically. In ARQ, its 
use of QueryIterator is checked and it does complain if open iterators are 
found. This is both internal and because QueryExecution is AutoCloseable.

However, it is application code, that is the issue. If we turn warnings on, a 
possible outcome if there are lots of warnings, is that users will turn the 
warning off in their IDE. That's just life. 

There is a natural idiom now with transactions and it applies for TDB. When the 
transaction ends, further use of an iterator will cause an error.

Due to he lack of interest in SDB, deep Jena transaction API integration hasn't 
been retrofitted.

So Jena's transactions, especially used with {{Txn}}, provide a way for a 
system to catch and log misuse of iterators.

Any new API can be conversent of history - if resource manage could matter, 
then design for it so at scale, mistakes do not accumulate. But the core Jena 
API predates try-with-resource.

We can leave this open for a while - its a useful discussion.

> Make ClosableIterator<T> extend AutoCloseable
> ---------------------------------------------
>
>                 Key: JENA-1601
>                 URL: https://issues.apache.org/jira/browse/JENA-1601
>             Project: Apache Jena
>          Issue Type: Improvement
>          Components: Jena
>    Affects Versions: Jena 3.8.0
>            Reporter: David Schwingenschlögl
>            Priority: Minor
>
> The interface org.apache.jena.util.iterator.ClosableIterator<T> defines a 
> method public void close(), so the concept of closing is already baked into 
> it. The only barrier to using a ClosableIterator (and thus, ExtendedIterator) 
> in a try-with-resource block is the missing extension of 
> java.lang.AutoCloseable.
> According to API documentation of ClosableIterator, an iterator should be 
> closed when not completely exhausted, which may be the case when the block 
> consuming the iterator throws an exception, effectively making constructs 
> such as this necessary:
> {code:java}
> final ExtendedIterator<Triple> iterator = someGraph.find();
> try {
>   while (iterator.hasNext()) {
>     // consume iterator, might throw in here
>   }
> } finally {
>   // Prevent resource leaks
>   iterator.close();
> }
> {code}
> This would be better expressed in a try-with-resource-construct:
> {code:java}
> try (final ExtendedIterator<Triple> itrator = someGraph.find()) {
>   // consume iterator, might throw in here
> }
> {code}
> From what I can tell, making a ClosableIterator also extend AutoCloseable 
> only adds to the usability of Jena's API while keeping source backwards 
> compatibility intact.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to