use of java.sql.SQLTimeoutException in project jaybird by FirebirdSQL.
the class WireConnection method identify.
/**
* Performs the connection identification phase of the Wire protocol and
* returns the FbWireDatabase implementation for the agreed protocol.
*
* @return FbWireDatabase
* @throws SQLTimeoutException
* @throws SQLException
*/
@Override
public final C identify() throws SQLException {
try {
xdrIn = new XdrInputStream(socket.getInputStream());
xdrOut = new XdrOutputStream(socket.getOutputStream());
xdrOut.writeInt(op_connect);
xdrOut.writeInt(op_attach);
xdrOut.writeInt(CONNECT_VERSION3);
xdrOut.writeInt(arch_generic);
xdrOut.writeString(getAttachObjectName(), getEncoding());
// Count of protocols understood
xdrOut.writeInt(protocols.getProtocolCount());
xdrOut.writeBuffer(createUserIdentificationBlock());
for (ProtocolDescriptor protocol : protocols) {
// Protocol version
xdrOut.writeInt(protocol.getVersion());
// Architecture of client
xdrOut.writeInt(protocol.getArchitecture());
// Minimum type
xdrOut.writeInt(protocol.getMinimumType());
// Maximum type
xdrOut.writeInt(protocol.getMaximumType());
// Preference weight
xdrOut.writeInt(protocol.getWeight());
}
xdrOut.flush();
final int operation = readNextOperation();
if (operation == op_accept || operation == op_cond_accept || operation == op_accept_data) {
FbWireAttachment.AcceptPacket acceptPacket = new FbWireAttachment.AcceptPacket();
acceptPacket.operation = operation;
// Protocol version
protocolVersion = xdrIn.readInt();
// Architecture for protocol
protocolArchitecture = xdrIn.readInt();
// Minimum type
protocolMinimumType = xdrIn.readInt();
if (protocolVersion < 0) {
protocolVersion = (protocolVersion & FB_PROTOCOL_MASK) | FB_PROTOCOL_FLAG;
}
if (operation == op_cond_accept || operation == op_accept_data) {
byte[] data = acceptPacket.p_acpt_data = xdrIn.readBuffer();
acceptPacket.p_acpt_plugin = xdrIn.readString(getEncoding());
final int isAuthenticated = xdrIn.readInt();
byte[] serverKeys = acceptPacket.p_acpt_keys = xdrIn.readBuffer();
clientAuthBlock.setServerData(data);
clientAuthBlock.setAuthComplete(isAuthenticated == 1);
addServerKeys(serverKeys);
clientAuthBlock.resetClient(serverKeys);
clientAuthBlock.switchPlugin(acceptPacket.p_acpt_plugin);
} else {
clientAuthBlock.resetClient(null);
}
ProtocolDescriptor descriptor = protocols.getProtocolDescriptor(protocolVersion);
if (descriptor == null) {
throw new SQLException(String.format("Unsupported or unexpected protocol version %d connecting to database %s. Supported version(s): %s", protocolVersion, getServerName(), protocols.getProtocolVersions()));
}
C connectionHandle = createConnectionHandle(descriptor);
if (operation == op_cond_accept) {
connectionHandle.authReceiveResponse(acceptPacket);
}
return connectionHandle;
} else {
try {
if (operation == op_response) {
// Handle exception from response
AbstractWireOperations wireOperations = getDefaultWireOperations();
wireOperations.processResponse(wireOperations.processOperation(operation));
}
} finally {
try {
close();
} catch (Exception ex) {
log.debug("Ignoring exception on disconnect in connect phase of protocol", ex);
}
}
throw new FbExceptionBuilder().exception(ISCConstants.isc_connect_reject).toFlatSQLException();
}
} catch (SocketTimeoutException ste) {
throw new FbExceptionBuilder().timeoutException(ISCConstants.isc_network_error).messageParameter(getServerName()).cause(ste).toSQLException();
} catch (IOException ioex) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_network_error).messageParameter(getServerName()).cause(ioex).toSQLException();
}
}
use of java.sql.SQLTimeoutException in project drill by axbaretto.
the class PreparedStatementTest method testServerTriggeredQueryTimeout.
/**
* Test setting timeout for a query that actually times out because of lack of timely server response
*/
@Ignore("Pause Injection appears broken for PreparedStatement")
@Test(expected = SqlTimeoutException.class)
public void testServerTriggeredQueryTimeout() throws Exception {
// Setting to a very low value (2sec)
int timeoutDuration = 2;
// Server will be paused marginally longer than the test timeout
long serverPause = timeoutDuration + 2;
// Additional time for JDBC timeout and server pauses to complete
int cleanupPause = 3;
// Simulate a lack of timely server response by injecting a pause in the Screen operator's sending-data RPC
final String controls = Controls.newBuilder().addTimedPause(ScreenCreator.class, "sending-data", 0, TimeUnit.SECONDS.toMillis(serverPause)).build();
// Fetching an exclusive connection since injected pause affects all sessions on the connection
try (Connection exclusiveConnection = new Driver().connect("jdbc:drill:zk=local", null)) {
try (Statement stmt = exclusiveConnection.createStatement()) {
assertThat(stmt.execute(String.format("ALTER session SET `%s` = '%s'", ExecConstants.DRILLBIT_CONTROL_INJECTIONS, controls)), equalTo(true));
}
try (PreparedStatement pStmt = exclusiveConnection.prepareStatement(SYS_RANDOM_SQL)) {
pStmt.setQueryTimeout(timeoutDuration);
logger.info("Set a timeout of {} seconds", pStmt.getQueryTimeout());
// Executing a prepared statement with the paused server. Expecting timeout to occur here
ResultSet rs = pStmt.executeQuery();
// Fetch rows
while (rs.next()) {
rs.getBytes(1);
}
} catch (SQLTimeoutException sqlEx) {
logger.info("SQLTimeoutException thrown: {}", sqlEx.getMessage());
throw (SqlTimeoutException) sqlEx;
} finally {
// Pause briefly to wait for server to unblock
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(cleanupPause));
} catch (InterruptedException e) {
/*DoNothing*/
}
}
}
}
use of java.sql.SQLTimeoutException in project drill by axbaretto.
the class StatementTest method testServerTriggeredQueryTimeout.
/**
* Test setting timeout for a query that actually times out because of lack of timely server response
*/
@Test(expected = SqlTimeoutException.class)
public void testServerTriggeredQueryTimeout() throws Exception {
// Setting to a very low value (2sec)
int timeoutDuration = 2;
// Server will be paused marginally longer than the test timeout
long serverPause = timeoutDuration + 2;
// Additional time for JDBC timeout and server pauses to complete
int cleanupPause = 3;
// Simulate a lack of timely server response by injecting a pause in the Screen operator's sending-data RPC
final String controls = Controls.newBuilder().addTimedPause(ScreenCreator.class, "sending-data", 0, TimeUnit.SECONDS.toMillis(serverPause)).build();
// Fetching an exclusive connection since injected pause affects all sessions on the connection
try (Connection exclusiveConnection = new Driver().connect("jdbc:drill:zk=local", null)) {
try (Statement stmt = exclusiveConnection.createStatement()) {
assertThat(stmt.execute(String.format("ALTER session SET `%s` = '%s'", ExecConstants.DRILLBIT_CONTROL_INJECTIONS, controls)), equalTo(true));
}
try (Statement stmt = exclusiveConnection.createStatement()) {
stmt.setQueryTimeout(timeoutDuration);
logger.info("Set a timeout of {} seconds", stmt.getQueryTimeout());
// Executing a query with the paused server. Expecting timeout to occur here
ResultSet rs = stmt.executeQuery(SYS_VERSION_SQL);
// Fetch rows
while (rs.next()) {
rs.getBytes(1);
}
} catch (SQLTimeoutException sqlEx) {
logger.info("SQLTimeoutException thrown: {}", sqlEx.getMessage());
throw (SqlTimeoutException) sqlEx;
} finally {
// Pause briefly to wait for server to unblock
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(cleanupPause));
} catch (InterruptedException e) {
/*DoNothing*/
}
}
}
}
use of java.sql.SQLTimeoutException in project drill by axbaretto.
the class StatementTest method testClientTriggeredQueryTimeout.
/**
* Test setting timeout for a query that actually times out because of lack of timely client response
*/
@Test
public void testClientTriggeredQueryTimeout() throws Exception {
// Setting to a very low value (3sec)
int timeoutDuration = 3;
int rowsCounted = 0;
try (Statement stmt = connection.createStatement()) {
stmt.setQueryTimeout(timeoutDuration);
logger.info("Set a timeout of {} seconds", stmt.getQueryTimeout());
ResultSet rs = stmt.executeQuery(SYS_RANDOM_SQL);
// Fetch each row and pause (simulate a slow client)
try {
while (rs.next()) {
rs.getString(1);
rowsCounted++;
// Pause briefly (a second beyond the timeout) before attempting to fetch rows
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(timeoutDuration + 1));
} catch (InterruptedException e) {
/*DoNothing*/
}
logger.info("Paused for {} seconds", (timeoutDuration + 1));
}
} catch (SQLTimeoutException sqlEx) {
logger.info("Counted " + rowsCounted + " rows before hitting timeout");
// Successfully return
return;
}
}
// Throw an exception to indicate that we shouldn't have reached this point
throw new Exception("Failed to trigger timeout of " + timeoutDuration + " sec");
}
use of java.sql.SQLTimeoutException in project sqlite-jna by gwenn.
the class SqliteStatementTest method testQueryTimeout.
@Ignore
@Test
public void testQueryTimeout() throws Exception {
try (Statement stmt = conn.createStatement()) {
try {
stmt.setQueryTimeout(-1);
fail("negative timeout value allowed?");
} catch (SQLException e) {
}
((Conn) conn).getConn().createScalarFunction("delay", 0, FunctionFlags.SQLITE_UTF8, new ScalarCallback() {
@Override
public void func(SQLite3Context pCtx, SQLite3Values args) {
try {
Thread.currentThread().join(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pCtx.setResultInt(0);
}
});
stmt.setQueryTimeout(1);
assertEquals(1, stmt.getQueryTimeout());
long startTime = System.currentTimeMillis();
try (ResultSet rs = stmt.executeQuery("SELECT *, delay() from test_table")) {
rs.next();
fail("Expected a timeout exception");
} catch (SQLTimeoutException e) {
long endTime = System.currentTimeMillis();
if (endTime - startTime < 1000) {
fail("Timeout expired early -- " + (endTime - startTime));
}
}
try {
stmt.execute("INSERT INTO test_table VALUES (2, delay())");
} catch (SQLiteException e) {
long endTime = System.currentTimeMillis();
if (endTime - startTime < 1000) {
fail("Timeout expired early -- " + (endTime - startTime));
}
}
}
}
Aggregations