You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2013/06/06 20:50:11 UTC
svn commit: r1490393 - in /jena/Experimental/jena-jdbc:
jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/
jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/
jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/
jena-jdbc-co...
Author: rvesse
Date: Thu Jun 6 18:50:10 2013
New Revision: 1490393
URL: http://svn.apache.org/r1490393
Log:
Tests and bug fixes for query timeouts in JDBC
Modified:
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/statements/AbstractJenaStatementTests.java
jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java
jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/TestRemoteEndpointConnection.java
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java?rev=1490393&r1=1490392&r2=1490393&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java Thu Jun 6 18:50:10 2013
@@ -28,6 +28,7 @@ import org.apache.jena.jdbc.results.meta
import org.apache.jena.jdbc.statements.JenaStatement;
import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.query.QueryCancelledException;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.ResultSetFactory;
import com.hp.hpl.jena.sparql.core.Var;
@@ -144,7 +145,14 @@ public class SelectResults extends Strea
protected boolean hasNext() throws SQLException {
// No null check here because superclass will not call us after we are
// closed and set to null
- return this.innerResults.hasNext();
+ try {
+ return this.innerResults.hasNext();
+ } catch (QueryCancelledException e) {
+ throw new SQLException("Query was cancelled, it is likely that your query exceeded the specified execution timeout", e);
+ } catch (Throwable e) {
+ // Wrap as SQL exception
+ throw new SQLException("Unexpected error while moving through results", e);
+ }
}
/**
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java?rev=1490393&r1=1490392&r2=1490393&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java Thu Jun 6 18:50:10 2013
@@ -29,6 +29,7 @@ import org.apache.jena.jdbc.statements.J
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
+import com.hp.hpl.jena.query.QueryCancelledException;
import com.hp.hpl.jena.query.QueryExecution;
/**
@@ -56,7 +57,8 @@ public class TripleIteratorResults exten
* @throws SQLException
* Thrown if there is a problem creating the results
*/
- public TripleIteratorResults(JenaStatement statement, QueryExecution qe, Iterator<Triple> ts, boolean commit) throws SQLException {
+ public TripleIteratorResults(JenaStatement statement, QueryExecution qe, Iterator<Triple> ts, boolean commit)
+ throws SQLException {
super(statement, qe, commit);
if (ts == null)
throw new SQLException("Triple Iterator cannot be null");
@@ -81,7 +83,14 @@ public class TripleIteratorResults exten
protected boolean hasNext() throws SQLException {
// No null check here because superclass will not call us after we are
// closed and set to null
- return this.triples.hasNext();
+ try {
+ return this.triples.hasNext();
+ } catch (QueryCancelledException e) {
+ throw new SQLException("Query was cancelled, it is likely that your query exceeded the specified execution timeout", e);
+ } catch (Throwable e) {
+ // Wrap as SQL exception
+ throw new SQLException("Unexpected error while moving through results", e);
+ }
}
@Override
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java?rev=1490393&r1=1490392&r2=1490393&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java Thu Jun 6 18:50:10 2013
@@ -293,7 +293,7 @@ public abstract class JenaStatement impl
// Manipulate the query execution if appropriate
if (this.timeout > NO_LIMIT) {
- qe.setTimeout(this.timeout, TimeUnit.SECONDS);
+ qe.setTimeout(this.timeout, TimeUnit.SECONDS, this.timeout, TimeUnit.SECONDS);
}
// Return the appropriate result set type
@@ -694,7 +694,7 @@ public abstract class JenaStatement impl
@Override
public final int getResultSetType() throws SQLException {
- return ResultSet.TYPE_FORWARD_ONLY;
+ return this.type;
}
@Override
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java?rev=1490393&r1=1490392&r2=1490393&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java Thu Jun 6 18:50:10 2013
@@ -118,6 +118,16 @@ public abstract class AbstractJenaConnec
}
/**
+ * Method which indicates whether the connection being tested supports query
+ * timeouts
+ *
+ * @return True if query timeouts are supported
+ */
+ protected boolean supportsTimeouts() {
+ return true;
+ }
+
+ /**
* Method which returns the name of the default graph when a named graph is
* being used as the default graph
*
@@ -969,6 +979,209 @@ public abstract class AbstractJenaConnec
Assert.assertTrue(conn.isClosed());
}
+ /**
+ * Runs a SELECT query on a non-empty database with max rows set and checks
+ * that the appropriate number of rows are returned
+ *
+ * @throws SQLException
+ */
+ @Test
+ public void connection_statement_query_select_max_rows_03() throws SQLException {
+ // Prepare a dataset
+ Dataset ds = TestUtils.generateDataset(3, 10, false);
+
+ // Work with the connection
+ JenaConnection conn = this.getConnection(ds);
+ Statement stmt = conn.createStatement();
+
+ // Set max rows to 20, note that if the query specifies a higher limit
+ // then the max rows are still enforced
+ stmt.setMaxRows(10);
+ ResultSet rset = stmt.executeQuery("SELECT * WHERE { GRAPH ?g { ?s ?p ?o } } LIMIT 50");
+ Assert.assertNotNull(rset);
+ Assert.assertFalse(rset.isClosed());
+ Assert.assertTrue(rset.isBeforeFirst());
+
+ // Check result set metadata
+ checkSelectMetadata(rset, 4);
+
+ // Check expected number of rows
+ int count = 0;
+ while (rset.next()) {
+ count++;
+ }
+ Assert.assertEquals(10, count);
+
+ // Should be no further rows
+ Assert.assertFalse(rset.next());
+ Assert.assertTrue(rset.isAfterLast());
+ Assert.assertFalse(rset.isClosed());
+
+ // Close things
+ rset.close();
+ Assert.assertTrue(rset.isClosed());
+ stmt.close();
+ Assert.assertTrue(stmt.isClosed());
+ conn.close();
+ Assert.assertTrue(conn.isClosed());
+ }
+
+ /**
+ * Runs a SELECT query on a non-empty database with timeout
+ *
+ * @throws SQLException
+ * @throws InterruptedException
+ */
+ @Test(expected = SQLException.class)
+ public void connection_statement_query_select_timeout_01() throws SQLException, InterruptedException {
+ Assume.assumeTrue(this.supportsTimeouts());
+
+ // Prepare a dataset
+ Dataset ds = TestUtils.generateDataset(1, 1000, true);
+
+ // Work with the connection
+ JenaConnection conn = this.getConnection(ds);
+ Statement stmt = conn.createStatement();
+
+ // Set timeout to 1 second
+ stmt.setQueryTimeout(1);
+ try {
+ ResultSet rset = stmt.executeQuery("SELECT * WHERE { "
+ + (this.usesNamedGraphAsDefault() ? "GRAPH <" + this.getDefaultGraphName() + "> {" : "")
+ + " ?a ?b ?c . ?d ?e ?f . ?g ?h ?i . " + (this.usesNamedGraphAsDefault() ? "}" : "") + "}");
+
+ // Note that we have to start iterating otherwise the query doesn't
+ // get executed and the timeout will never apply
+ while (rset.next()) {
+ Thread.sleep(100);
+ }
+
+ rset.close();
+ Assert.fail("Expected a query timeout");
+ } finally {
+ // Close things
+ stmt.close();
+ conn.close();
+ }
+ }
+
+ /**
+ * Runs a SELECT query on a non-empty database with timeout
+ *
+ * @throws SQLException
+ * @throws InterruptedException
+ */
+ @Test(expected = SQLException.class)
+ public void connection_statement_query_select_timeout_02() throws SQLException, InterruptedException {
+ Assume.assumeTrue(this.supportsTimeouts());
+
+ // Prepare a dataset
+ Dataset ds = TestUtils.generateDataset(1, 1000, true);
+
+ // Work with the connection
+ JenaConnection conn = this.getConnection(ds);
+ Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
+
+ // Set timeout to 1 second
+ stmt.setQueryTimeout(1);
+ try {
+ ResultSet rset = stmt.executeQuery("SELECT * WHERE { "
+ + (this.usesNamedGraphAsDefault() ? "GRAPH <" + this.getDefaultGraphName() + "> {" : "")
+ + " ?a ?b ?c . ?d ?e ?f . ?g ?h ?i . " + (this.usesNamedGraphAsDefault() ? "}" : "") + "}");
+
+ // Note that we have to start iterating otherwise the query doesn't
+ // get executed and the timeout will never apply
+ while (rset.next()) {
+ Thread.sleep(100);
+ }
+
+ rset.close();
+ Assert.fail("Expected a query timeout");
+ } finally {
+ // Close things
+ stmt.close();
+ conn.close();
+ }
+ }
+
+ /**
+ * Runs a SELECT query on a non-empty database with timeout
+ *
+ * @throws SQLException
+ * @throws InterruptedException
+ */
+ @Test(expected = SQLException.class)
+ public void connection_statement_query_construct_timeout_01() throws SQLException, InterruptedException {
+ Assume.assumeTrue(this.supportsTimeouts());
+
+ // Prepare a dataset
+ Dataset ds = TestUtils.generateDataset(1, 1000, true);
+
+ // Work with the connection
+ JenaConnection conn = this.getConnection(ds);
+ Statement stmt = conn.createStatement();
+
+ // Set timeout to 1 second
+ stmt.setQueryTimeout(1);
+ try {
+ ResultSet rset = stmt.executeQuery("CONSTRUCT { ?s ?p ?o } WHERE { "
+ + (this.usesNamedGraphAsDefault() ? "GRAPH <" + this.getDefaultGraphName() + "> {" : "")
+ + " ?a ?b ?c . ?d ?e ?f . ?g ?h ?i . " + (this.usesNamedGraphAsDefault() ? "}" : "") + "}");
+
+ // Note that we have to start iterating otherwise the query doesn't
+ // get executed and the timeout will never apply
+ while (rset.next()) {
+ Thread.sleep(100);
+ }
+
+ rset.close();
+ Assert.fail("Expected a query timeout");
+ } finally {
+ // Close things
+ stmt.close();
+ conn.close();
+ }
+ }
+
+ /**
+ * Runs a SELECT query on a non-empty database with timeout
+ *
+ * @throws SQLException
+ * @throws InterruptedException
+ */
+ @Test(expected = SQLException.class)
+ public void connection_statement_query_construct_timeout_02() throws SQLException, InterruptedException {
+ Assume.assumeTrue(this.supportsTimeouts());
+
+ // Prepare a dataset
+ Dataset ds = TestUtils.generateDataset(1, 1000, true);
+
+ // Work with the connection
+ JenaConnection conn = this.getConnection(ds);
+ Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
+
+ // Set timeout to 1 second
+ stmt.setQueryTimeout(1);
+ try {
+ ResultSet rset = stmt.executeQuery("CONSTRUCT { ?s ?p ?o } WHERE { "
+ + (this.usesNamedGraphAsDefault() ? "GRAPH <" + this.getDefaultGraphName() + "> {" : "")
+ + " ?a ?b ?c . ?d ?e ?f . ?g ?h ?i . " + (this.usesNamedGraphAsDefault() ? "}" : "") + "}");
+
+ // Note that we have to start iterating otherwise the query doesn't
+ // get executed and the timeout will never apply
+ while (rset.next()) {
+ Thread.sleep(100);
+ }
+
+ rset.close();
+ Assert.fail("Expected a query timeout");
+ } finally {
+ // Close things
+ stmt.close();
+ conn.close();
+ }
+ }
+
protected void checkSelectMetadata(ResultSet results, int numColumns) throws SQLException {
ResultSetMetaData metadata = results.getMetaData();
Assert.assertEquals(numColumns, metadata.getColumnCount());
@@ -1832,7 +2045,7 @@ public abstract class AbstractJenaConnec
conn.close();
Assert.assertTrue(conn.isClosed());
}
-
+
/**
* Runs a batch of operations and checks the results results
*
@@ -1877,7 +2090,7 @@ public abstract class AbstractJenaConnec
conn.close();
Assert.assertTrue(conn.isClosed());
}
-
+
/**
* Runs a batch of operations and checks the results results
*
@@ -1912,7 +2125,7 @@ public abstract class AbstractJenaConnec
ResultSet rset2 = stmt.getResultSet();
Assert.assertTrue(rset2 instanceof AskResults);
checkAskMetadata(rset2);
-
+
// Leave open and grab the next result set
Assert.assertTrue(stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
Assert.assertFalse(rset.isClosed());
@@ -1920,7 +2133,7 @@ public abstract class AbstractJenaConnec
ResultSet rset3 = stmt.getResultSet();
Assert.assertTrue(rset3 instanceof TripleIteratorResults);
checkConstructDescribeMetadata(rset3);
-
+
// Grab next result set closing all previous
Assert.assertTrue(stmt.getMoreResults(Statement.CLOSE_ALL_RESULTS));
Assert.assertTrue(rset.isClosed());
@@ -1942,7 +2155,7 @@ public abstract class AbstractJenaConnec
conn.close();
Assert.assertTrue(conn.isClosed());
}
-
+
/**
* Runs a batch of operations and checks the results results
*
@@ -1959,13 +2172,13 @@ public abstract class AbstractJenaConnec
stmt.addBatch("ASK WHERE { }");
stmt.addBatch("CONSTRUCT WHERE { ?s ?p ?o }");
stmt.addBatch("DESCRIBE <http://example>");
-
+
// Clear the batch
stmt.clearBatch();
-
+
int[] batchResults = stmt.executeBatch();
Assert.assertEquals(0, batchResults.length);
-
+
stmt.close();
conn.close();
}
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/statements/AbstractJenaStatementTests.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/statements/AbstractJenaStatementTests.java?rev=1490393&r1=1490392&r2=1490393&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/statements/AbstractJenaStatementTests.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/statements/AbstractJenaStatementTests.java Thu Jun 6 18:50:10 2013
@@ -430,6 +430,25 @@ public abstract class AbstractJenaStatem
conn.close();
}
}
+
+ /**
+ * Tests error cases for trying to execute things on closed statements
+ *
+ * @throws SQLException
+ */
+ @Test(expected = SQLException.class)
+ public void statement_closed_execute_11() throws SQLException {
+ JenaConnection conn = this.getConnection();
+ Statement stmt = conn.createStatement();
+
+ stmt.close();
+
+ try {
+ stmt.executeBatch();
+ } finally {
+ conn.close();
+ }
+ }
/**
* Tests error cases for trying to execute invalid SPARQL
@@ -778,6 +797,8 @@ public abstract class AbstractJenaStatem
Statement stmt = conn.createStatement();
Assert.assertEquals(ResultSet.FETCH_FORWARD, stmt.getFetchDirection());
+ stmt.setFetchDirection(ResultSet.FETCH_FORWARD);
+ Assert.assertEquals(ResultSet.FETCH_FORWARD, stmt.getFetchDirection());
try {
stmt.setFetchDirection(ResultSet.FETCH_REVERSE);
} finally {
@@ -785,4 +806,66 @@ public abstract class AbstractJenaStatem
conn.close();
}
}
+
+ /**
+ * Tests manipulating some settings on a statement
+ *
+ * @throws SQLException
+ */
+ @Test
+ public void statement_settings_07() throws SQLException {
+ JenaConnection conn = this.getConnection();
+ Statement stmt = conn.createStatement();
+
+ stmt.setEscapeProcessing(true);
+
+ stmt.close();
+ conn.close();
+ }
+
+ /**
+ * Tests manipulating some settings on a statement
+ *
+ * @throws SQLException
+ */
+ @Test(expected = SQLFeatureNotSupportedException.class)
+ public void statement_settings_08() throws SQLException {
+ JenaConnection conn = this.getConnection();
+ Statement stmt = conn.createStatement();
+
+ try {
+ stmt.setCursorName("test");
+ } finally {
+ stmt.close();
+ conn.close();
+ }
+ }
+
+ /**
+ * Tests getting metadata from the statement
+ * @throws SQLException
+ */
+ @Test
+ public void statement_metadata_01() throws SQLException {
+ JenaConnection conn = this.getConnection();
+ Statement stmt = conn.createStatement();
+
+ Assert.assertEquals(ResultSet.TYPE_FORWARD_ONLY, stmt.getResultSetType());
+ Assert.assertEquals(conn.getHoldability(), stmt.getResultSetHoldability());
+ Assert.assertEquals(ResultSet.CONCUR_READ_ONLY, stmt.getResultSetConcurrency());
+ }
+
+ /**
+ * Tests getting metadata from the statement
+ * @throws SQLException
+ */
+ @Test
+ public void statement_metadata_02() throws SQLException {
+ JenaConnection conn = this.getConnection();
+ Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
+
+ Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, stmt.getResultSetType());
+ Assert.assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, stmt.getResultSetHoldability());
+ Assert.assertEquals(ResultSet.CONCUR_READ_ONLY, stmt.getResultSetConcurrency());
+ }
}
Modified: jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java?rev=1490393&r1=1490392&r2=1490393&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java Thu Jun 6 18:50:10 2013
@@ -114,7 +114,7 @@ public class RemoteEndpointStatement ext
if (this.remoteConn.getModelResultsType() != null) {
exec.setModelContentType(this.remoteConn.getModelResultsType());
}
-
+
// Return execution
return exec;
}
Modified: jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/TestRemoteEndpointConnection.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/TestRemoteEndpointConnection.java?rev=1490393&r1=1490392&r2=1490393&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/TestRemoteEndpointConnection.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/TestRemoteEndpointConnection.java Thu Jun 6 18:50:10 2013
@@ -61,7 +61,7 @@ public class TestRemoteEndpointConnectio
public static void cleanup() {
ServerTest.freeServer();
}
-
+
@Override
protected JenaConnection getConnection() throws SQLException {
return new RemoteEndpointConnection(BaseServerTest.serviceQuery, BaseServerTest.serviceUpdate, JenaConnection.DEFAULT_HOLDABILITY, JdbcCompatibility.DEFAULT);