下面列出了怎么用java.sql.SQLTimeoutException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* Waits for the first byte of the server response.
*
* @param timeOut the timeout period in seconds or 0
*/
private void wait(int timeOut) throws IOException, SQLException {
Object timer = null;
try {
if (timeOut > 0) {
// Start a query timeout timer
timer = TimerThread.getInstance().setTimer(timeOut * 1000,
new TimerThread.TimerListener() {
public void timerExpired() {
TdsCore.this.cancel(true);
}
});
}
in.peek();
} finally {
if (timer != null) {
if (!TimerThread.getInstance().cancelTimer(timer)) {
throw new SQLTimeoutException(
Messages.get("error.generic.timeout"), "HYT00");
}
}
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* <p>Checks if the throwable was caused by timeout exception.</p>
* <b>This method has been tested for Oracle and MySQL only and might not work
* for other DB engines.</b>
*
* @param throwable to check
* @return true if the throwable is caused by a timeout, false otherwise
*/
public boolean isCausedByTimeoutException(Throwable throwable) {
// Valid test for Oracle timeout exception and some (not all!) MySQL
// exceptions.
if (ExceptionUtils.indexOfType(throwable, SQLTimeoutException.class) != -1) {
return true;
}
// MySQL database has two timeout exceptions in two packages. One of them
// doesn't extend SQLTimeoutException but only SQLException. It is therefore
// necessary to do ugly name check...
for (Throwable causeThrowable : ExceptionUtils.getThrowables(throwable)) {
if (MYSQL_TIMEOUT_EXCEPTION_NAME.equals(causeThrowable.getClass().getSimpleName())) {
return true;
}
}
return false;
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* @throws Exception If failed.
*/
@Test
public void testUrlQueryTimeoutProperty() throws Exception {
try (final Connection conn = DriverManager.getConnection(URL + "?connectionTimeout=10000&queryTimeout=1")) {
conn.setSchema('"' + DEFAULT_CACHE_NAME + '"');
final Statement stmt = conn.createStatement();
GridTestUtils.assertThrows(log, () -> {
stmt.executeQuery("select sleep_func(3) from Integer;");
return null;
}, SQLTimeoutException.class, "The query was cancelled while executing.");
stmt.execute("select 1");
}
}
static void checkConnectionState(PoolItem<Connection> connection, SQLException e) {
String state = e.getSQLState();
// for state starts with "08"
// refer to com.mysql.jdbc.integration.c3p0.MysqlConnectionTester, and standard sql state list:
// http://dev.mysql.com/doc/connector-j/en/connector-j-reference-error-sqlstates.html
//
// for state: S1009
// refer to com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException() and com.mysql.cj.exceptions.MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT
// MySQL jdbc connector will translate "statement is closed" and other errors to "S1009"
if (state != null && (state.startsWith("08") || "S1009".equals(state))) {
connection.broken = true;
}
// if query timeout, com.mysql.cj.CancelQueryTaskImpl sends "KILL QUERY" to mysql server in cancel task thread
// and as result, in current thread it triggers com.mysql.cj.jdbc.ConnectionImpl.handleCleanup with whyCleanedUp = com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure, The last packet successfully received from the server was x milliseconds ago.
// so for query timeout, the connection needs to be marked as broken and close,
// otherwise when other thread retrieves this connection, will encounter "statement is already closed" exception
if (e instanceof SQLTimeoutException) {
connection.broken = true;
}
}
@Test
public void testLongValidationQueryTime() throws Exception {
// use our mock driver
Connection con = this.datasource.getConnection();
Statement stmt = null;
long start = 0, end = 0;
try {
stmt = con.createStatement();
// set the query timeout to 2 sec
// this keeps this test from slowing things down too much
stmt.setQueryTimeout(2);
// assert that our long query takes longer than one second to run
// this is a requirement for other tests to run properly
start = System.currentTimeMillis();
stmt.execute(longQuery);
} catch (SQLTimeoutException ex) {
} catch (SQLException x) {
Assert.fail("We should have got a timeout exception.");
} finally {
end = System.currentTimeMillis();
if (stmt != null) { stmt.close(); }
if (con != null) { con.close(); }
Assert.assertTrue(start != 0 && end != 0);
//we're faking it
//Assert.assertTrue((end - start) > 1000);
}
}
@Override
public boolean execute(String sql) throws SQLException {
if (longQuery.equals(sql)) {
throw new SQLTimeoutException();
} else {
return super.execute(sql);
}
}
/**
* Setting timeout that is greater than query execution time. <code>SQLTimeoutException</code> is expected.
*
* @throws Exception If failed.
*/
@Test
public void testQueryTimeout() throws Exception {
stmt.setQueryTimeout(2);
GridTestUtils.assertThrows(log, () -> {
stmt.executeQuery("select sleep_func(10) from Integer;");
return null;
}, SQLTimeoutException.class, "The query was cancelled while executing.");
}
/**
* Serialize a SQLTimeoutException and make sure you can read it back properly
*/
@Test
public void test10() throws Exception {
SQLTimeoutException e =
new SQLTimeoutException(reason, state, errorCode, t);
SQLTimeoutException ex1 =
createSerializedException(e);
assertTrue(reason.equals(ex1.getMessage())
&& ex1.getSQLState().equals(state)
&& cause.equals(ex1.getCause().toString())
&& ex1.getErrorCode() == errorCode);
}
/**
* Blocks until an existing connection is available.
*
* @return An existing connection.
* @throws SQLException If the connection is invalid.
*/
private Connection acquireConnection() throws SQLException {
Connection connection;
try {
connection = connectionQueue.poll(MAX_TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new SQLTimeoutException();
}
if (connection == null) releaseConnection(connection = newConnection());
if (connection.isClosed()) throw new SQLTimeoutException();
return connection;
}
/**
* Create SQLTimeoutException with Throwable
*/
@Test
public void test9() {
SQLTimeoutException ex = new SQLTimeoutException(t);
assertTrue(ex.getMessage().equals(cause)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with no-arg constructor
*/
@Test
public void test1() {
SQLTimeoutException ex = new SQLTimeoutException();
assertTrue(ex.getMessage() == null
&& ex.getSQLState() == null
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with message, and SQLState
*/
@Test
public void test3() {
SQLTimeoutException ex = new SQLTimeoutException(reason, state);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with message, SQLState, and error code
*/
@Test
public void test4() {
SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == errorCode);
}
/**
* Create SQLTimeoutException with message, and Throwable
*/
@Test
public void test7() {
SQLTimeoutException ex = new SQLTimeoutException(reason, t);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with null Throwable
*/
@Test
public void test8() {
SQLTimeoutException ex = new SQLTimeoutException((Throwable)null);
assertTrue(ex.getMessage() == null
&& ex.getSQLState() == null
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with Throwable
*/
@Test
public void test9() {
SQLTimeoutException ex = new SQLTimeoutException(t);
assertTrue(ex.getMessage().equals(cause)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with no-arg constructor
*/
@Test
public void test1() {
SQLTimeoutException ex = new SQLTimeoutException();
assertTrue(ex.getMessage() == null
&& ex.getSQLState() == null
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with message, SQLState, and error code
*/
@Test
public void test4() {
SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == errorCode);
}
/**
* Serialize a SQLTimeoutException and make sure you can read it back properly
*/
@Test
public void test10() throws Exception {
SQLTimeoutException e =
new SQLTimeoutException(reason, state, errorCode, t);
SQLTimeoutException ex1 =
createSerializedException(e);
assertTrue(reason.equals(ex1.getMessage())
&& ex1.getSQLState().equals(state)
&& cause.equals(ex1.getCause().toString())
&& ex1.getErrorCode() == errorCode);
}
/**
* Create SQLTimeoutException with message, and SQLState
*/
@Test
public void test3() {
SQLTimeoutException ex = new SQLTimeoutException(reason, state);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with message, and SQLState
*/
@Test
public void test3() {
SQLTimeoutException ex = new SQLTimeoutException(reason, state);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with message, SQLState, and error code
*/
@Test
public void test4() {
SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == errorCode);
}
/**
* Create SQLTimeoutException with message
*/
@Test
public void test2() {
SQLTimeoutException ex = new SQLTimeoutException(reason);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState() == null
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with message, and Throwable
*/
@Test
public void test7() {
SQLTimeoutException ex = new SQLTimeoutException(reason, t);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTimeoutException with message, and SQLState
*/
@Test
public void test3() {
SQLTimeoutException ex = new SQLTimeoutException(reason, state);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}