@@ -1489,6 +1489,12 @@ public ICloseableIterator<BigdataGraphAtom> project(final String queryStr,
14891489 result = query .evaluate ();
14901490
14911491 } catch (Exception ex ) {
1492+ if (queryId != null ) {
1493+ /*
1494+ * In case the exception happens during evaluate().
1495+ */
1496+ finalizeQuery (queryId );
1497+ }
14921498 if (!readFromWriteConnection ) {
14931499 cxn .close ();
14941500 }
@@ -1657,6 +1663,12 @@ public ICloseableIterator<BigdataBindingSet> select(final String queryStr,
16571663 result = query .evaluate ();
16581664
16591665 } catch (Exception ex ) {
1666+ if (queryId != null ) {
1667+ /*
1668+ * In case the exception happens during evaluate().
1669+ */
1670+ finalizeQuery (queryId );
1671+ }
16601672 if (!readFromWriteConnection ) {
16611673 cxn .close ();
16621674 }
@@ -1743,16 +1755,20 @@ public boolean ask(final String queryStr, String externalQueryId)
17431755
17441756 final boolean result = query .evaluate ();
17451757
1746- finalizeQuery (queryId );
1758+ // finalizeQuery(queryId);
17471759
17481760 return result ;
17491761
17501762 } finally {
1751-
1763+ if (queryId != null ) {
1764+ /*
1765+ * In case the exception happens during evaluate().
1766+ */
1767+ finalizeQuery (queryId );
1768+ }
17521769 if (!readFromWriteConnection ) {
17531770 cxn .close ();
17541771 }
1755-
17561772 }
17571773
17581774 }
@@ -1821,20 +1837,13 @@ public ICloseableIterator<BigdataGraphEdit> history(final List<URI> ids)
18211837
18221838 @ SuppressWarnings ("unchecked" )
18231839 public ICloseableIterator <BigdataGraphEdit > history (final List <URI > ids ,
1824- final String extQueryId ) throws Exception {
1840+ final String extQueryId ) throws Exception {
18251841
1826- // final List<URI> ids = new LinkedList<URI>();
1827- // for (Object id : vertexIds) {
1828- // ids.add(factory.toVertexURI(id));
1829- // }
1830- // for (Object id : edgeIds) {
1831- // ids.add(factory.toEdgeURI(id));
1832- // }
1842+ final RepositoryConnection cxn = readFromWriteConnection ?
1843+ getWriteConnection () : getReadConnection ();
18331844
18341845 final StringBuilder sb = new StringBuilder (HISTORY_TEMPLATE );
18351846
1836- UUID queryId = null ;
1837-
18381847 if (ids .size () > 0 ) {
18391848 final StringBuilder vc = new StringBuilder ();
18401849 vc .append (" values (?s) { \n " );
@@ -1852,30 +1861,45 @@ public ICloseableIterator<BigdataGraphEdit> history(final List<URI> ids,
18521861 ? queryStr : queryStr .substring (0 , SPARQL_LOG_MAX )+" ..." ));
18531862 }
18541863
1855- final RepositoryConnection cxn = readFromWriteConnection ?
1856- getWriteConnection () : getReadConnection ();
1857-
1858- final TupleQuery query = (TupleQuery )
1859- cxn .prepareTupleQuery (QueryLanguage .SPARQL , queryStr );
1864+ final TupleQueryResult result ;
1865+ UUID queryId = null ;
18601866
1861- if (query instanceof BigdataSailTupleQuery
1862- && cxn instanceof BigdataSailRepositoryConnection ) {
1863-
1864- final BigdataSailTupleQuery bdtq = (BigdataSailTupleQuery ) query ;
1865- queryId = setupQuery ((BigdataSailRepositoryConnection ) cxn ,
1866- bdtq .getASTContainer (), QueryType .SELECT ,
1867- extQueryId );
1868- }
1867+ try {
1868+
1869+ final TupleQuery query = (TupleQuery )
1870+ cxn .prepareTupleQuery (QueryLanguage .SPARQL , queryStr );
1871+
1872+ if (query instanceof BigdataSailTupleQuery
1873+ && cxn instanceof BigdataSailRepositoryConnection ) {
1874+
1875+ final BigdataSailTupleQuery bdtq = (BigdataSailTupleQuery ) query ;
1876+ queryId = setupQuery ((BigdataSailRepositoryConnection ) cxn ,
1877+ bdtq .getASTContainer (), QueryType .SELECT ,
1878+ extQueryId );
1879+ }
1880+
1881+ if (sparqlLog .isTraceEnabled ()) {
1882+ if (query instanceof BigdataSailTupleQuery ) {
1883+ final BigdataSailTupleQuery bdtq = (BigdataSailTupleQuery ) query ;
1884+ sparqlLog .trace ("optimized AST:\n " +bdtq .optimize ());
1885+ }
1886+ }
18691887
1870- if (sparqlLog .isTraceEnabled ()) {
1871- if (query instanceof BigdataSailTupleQuery ) {
1872- final BigdataSailTupleQuery bdtq = (BigdataSailTupleQuery ) query ;
1873- sparqlLog .trace ("optimized AST:\n " +bdtq .optimize ());
1888+ result = query .evaluate ();
1889+
1890+ } catch (Exception ex ) {
1891+ if (queryId != null ) {
1892+ /*
1893+ * In case the exception happens during evaluate().
1894+ */
1895+ finalizeQuery (queryId );
1896+ }
1897+ if (!readFromWriteConnection ) {
1898+ cxn .close ();
18741899 }
1900+ throw ex ;
18751901 }
1876-
1877- final TupleQueryResult result = query .evaluate ();
1878-
1902+
18791903 final IStriterator sitr = new Striterator (new WrappedResult <BindingSet >(
18801904 result , readFromWriteConnection ? null : cxn , queryId
18811905 ));
0 commit comments