Java Code Examples for java.sql.Connection#setNetworkTimeout()

The following examples show how to use java.sql.Connection#setNetworkTimeout() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 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;
}
 
Example 2
Source Project: r-course   File: ConnectionRegressionTest.java    License: MIT License 5 votes vote down vote up
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;
        }
    });
}
 
Example 3
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;
        }
    });
}
 
Example 4
Source Project: snowflake-jdbc   File: ConnectionIT.java    License: Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 5
Source Project: athenz   File: AthenzDataSource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Connection getConnection() throws SQLException {
    Connection conn = super.getConnection();
    if (networkTimeout > 0) {
        conn.setNetworkTimeout(timeoutThreadPool, networkTimeout);
    }
    return conn;
}
 
Example 6
protected void prepareConnection(Connection connection) {
    try {
        connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        connection.setNetworkTimeout(Executors.newSingleThreadExecutor(), 3000);
    } catch (Throwable ignore) {
    }
}
 
Example 7
Source Project: Tomcat8-Source-Read   File: Jdbc41Bridge.java    License: MIT License 3 votes vote down vote up
/**
 * 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
    }
}
 
Example 8
Source Project: commons-dbcp   File: Jdbc41Bridge.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 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
    }
}