java.sql.Connection#setNetworkTimeout ( )源码实例Demo

下面列出了java.sql.Connection#setNetworkTimeout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: PGM   文件: ThreadSafeConnection.java
/**
 * 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;
}
 
源代码2 项目: r-course   文件: ConnectionRegressionTest.java
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;
        }
    });
}
 
源代码3 项目: Komondor   文件: ConnectionRegressionTest.java
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;
        }
    });
}
 
源代码4 项目: snowflake-jdbc   文件: ConnectionIT.java
@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();
}
 
源代码5 项目: athenz   文件: AthenzDataSource.java
@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) {
    }
}
 
源代码7 项目: Tomcat8-Source-Read   文件: Jdbc41Bridge.java
/**
 * 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
    }
}
 
源代码8 项目: commons-dbcp   文件: Jdbc41Bridge.java
/**
 * 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
    }
}