下面列出了java.sql.Connection#setNetworkTimeout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Establishes a new connection.
*
* @return A new connection.
* @throws SQLException If the connection is invalid.
*/
private Connection newConnection() throws SQLException {
final Connection connection = connectionSupplier.get();
connection.setAutoCommit(true);
try {
connection.setNetworkTimeout(executorService, (int) MAX_TIMEOUT_MS);
} catch (AbstractMethodError e) {
// No-op, really old drivers do not support timeouts
}
return connection;
}
public void testBug75615() throws Exception {
// Main use case: although this could cause an exception due to a race condition in MysqlIO.mysqlConnection it is silently swallowed within the running
// thread.
final Connection testConn1 = getConnectionWithProps("");
testConn1.setNetworkTimeout(Executors.newSingleThreadExecutor(), 1000);
testConn1.close();
// Main use case simulation: this simulates the above by capturing an eventual exeption in the main thread. This is where this test would actually fail.
// This part is repeated several times to increase the chance of hitting the reported bug.
for (int i = 0; i < 25; i++) {
final ExecutorService execService = Executors.newSingleThreadExecutor();
final Connection testConn2 = getConnectionWithProps("");
testConn2.setNetworkTimeout(new Executor() {
public void execute(Runnable command) {
// Attach the future to the parent object so that it can track the exception in the main thread.
ConnectionRegressionTest.this.testBug75615Future = execService.submit(command);
}
}, 1000);
testConn2.close();
try {
this.testBug75615Future.get();
} catch (ExecutionException e) {
e.getCause().printStackTrace();
fail("Exception thrown in the thread that was setting the network timeout: " + e.getCause());
}
execService.shutdownNow();
}
// Test the expected exception on null executor.
assertThrows(SQLException.class, "Executor can not be null", new Callable<Void>() {
public Void call() throws Exception {
Connection testConn = getConnectionWithProps("");
testConn.setNetworkTimeout(null, 1000);
testConn.close();
return null;
}
});
}
public void testBug75615() throws Exception {
// Main use case: although this could cause an exception due to a race condition in MysqlIO.mysqlConnection it is silently swallowed within the running
// thread.
final Connection testConn1 = getConnectionWithProps("");
testConn1.setNetworkTimeout(Executors.newSingleThreadExecutor(), 1000);
testConn1.close();
// Main use case simulation: this simulates the above by capturing an eventual exeption in the main thread. This is where this test would actually fail.
// This part is repeated several times to increase the chance of hitting the reported bug.
for (int i = 0; i < 25; i++) {
final ExecutorService execService = Executors.newSingleThreadExecutor();
final Connection testConn2 = getConnectionWithProps("");
testConn2.setNetworkTimeout(new Executor() {
public void execute(Runnable command) {
// Attach the future to the parent object so that it can track the exception in the main thread.
ConnectionRegressionTest.this.testBug75615Future = execService.submit(command);
}
}, 1000);
testConn2.close();
try {
this.testBug75615Future.get();
} catch (ExecutionException e) {
e.getCause().printStackTrace();
fail("Exception thrown in the thread that was setting the network timeout: " + e.getCause());
}
execService.shutdownNow();
}
// Test the expected exception on null executor.
assertThrows(SQLException.class, "Executor can not be null", new Callable<Void>() {
public Void call() throws Exception {
Connection testConn = getConnectionWithProps("");
testConn.setNetworkTimeout(null, 1000);
testConn.close();
return null;
}
});
}
@Test
public void testNetworkTimeout() throws SQLException
{
Connection con = getConnection();
int millis = con.getNetworkTimeout();
assertEquals(0, millis);
con.setNetworkTimeout(null, 200);
assertEquals(200, con.getNetworkTimeout());
con.close();
}
@Override
public Connection getConnection() throws SQLException {
Connection conn = super.getConnection();
if (networkTimeout > 0) {
conn.setNetworkTimeout(timeoutThreadPool, networkTimeout);
}
return conn;
}
protected void prepareConnection(Connection connection) {
try {
connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
connection.setNetworkTimeout(Executors.newSingleThreadExecutor(), 3000);
} catch (Throwable ignore) {
}
}
/**
* Delegates to {@link Connection#setNetworkTimeout(Executor, int)} without throwing a {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Connection#setNetworkTimeout(Executor, int)}, then do nothing.
* </p>
*
* @param connection
* the receiver
* @param executor
* See {@link Connection#setNetworkTimeout(Executor, int)}
* @param milliseconds
* {@link Connection#setNetworkTimeout(Executor, int)}
* @throws SQLException
* {@link Connection#setNetworkTimeout(Executor, int)}
* @see Connection#setNetworkTimeout(Executor, int)
*/
public static void setNetworkTimeout(final Connection connection, final Executor executor, final int milliseconds)
throws SQLException {
try {
connection.setNetworkTimeout(executor, milliseconds);
} catch (final AbstractMethodError e) {
// do nothing
}
}
/**
* Delegates to {@link Connection#setNetworkTimeout(Executor, int)} without throwing an {@link AbstractMethodError}.
* <p>
* If the JDBC driver does not implement {@link Connection#setNetworkTimeout(Executor, int)}, then do nothing.
* </p>
*
* @param connection
* the receiver
* @param executor
* See {@link Connection#setNetworkTimeout(Executor, int)}
* @param milliseconds
* {@link Connection#setNetworkTimeout(Executor, int)}
* @throws SQLException
* {@link Connection#setNetworkTimeout(Executor, int)}
* @see Connection#setNetworkTimeout(Executor, int)
*/
public static void setNetworkTimeout(final Connection connection, final Executor executor, final int milliseconds)
throws SQLException {
try {
connection.setNetworkTimeout(executor, milliseconds);
} catch (final AbstractMethodError e) {
// do nothing
}
}