当前位置: 首页>>代码示例>>Java>>正文


Java Connection.setNetworkTimeout方法代码示例

本文整理汇总了Java中java.sql.Connection.setNetworkTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.setNetworkTimeout方法的具体用法?Java Connection.setNetworkTimeout怎么用?Java Connection.setNetworkTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.sql.Connection的用法示例。


在下文中一共展示了Connection.setNetworkTimeout方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testBug75615

import java.sql.Connection; //导入方法依赖的package包/类
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;
        }
    });
}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:39,代码来源:ConnectionRegressionTest.java


注:本文中的java.sql.Connection.setNetworkTimeout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。