如何使用 Java.sql.Connection.setNetworkTimeout?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10654547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Java.sql.Connection.setNetworkTimeout?
提问by Kristaps Baumanis
I ran into the exact issue that setNetworkTimeout is supposed to solve according to Oracle. A query got stuck in socket.read() for several minutes.
我遇到了 setNetworkTimeout 应该根据Oracle解决的确切问题。一个查询在 socket.read() 中停留了几分钟。
But I have little idea what the first parameter for this method needs to be. Submitting a null causes an AbstractMethodError exception, so... does the implementation actually need some sort of thread pool just to set a network timeout?
但我不知道这个方法的第一个参数需要是什么。提交一个空值会导致一个 AbstractMethodError 异常,所以......实现实际上是否需要某种线程池来设置网络超时?
Is there some way to achieve the same effect without running a thread pool just for this one condition?
是否有某种方法可以在不为这种情况运行线程池的情况下实现相同的效果?
回答by matt b
It seems like the documentation explains this horribly, but without looking at any code behind the class my guess would be that you are expected to pass an Executor instance to the method so that implementations can spawn jobs/threads in order to check on the status of the connection.
似乎文档对此进行了可怕的解释,但是如果不查看类背后的任何代码,我的猜测是您应该将 Executor 实例传递给该方法,以便实现可以生成作业/线程以检查状态连接。
Since connection reads will block, in order to implement any sort of timeout logic it's necessary to have another thread besides the reading one which can check on the status of the connection.
由于连接读取会阻塞,为了实现任何类型的超时逻辑,除了读取线程之外,还需要另一个线程来检查连接的状态。
It sounds like a design decision was made that instead of the JDBC driver implementing the logic internally, of how/when to spawn threads to handle this, the API wants you as the client to pass in an Executor that will be used to check on the timeouts. This way you as the client can control things like how often the check executes, preventing it from spawning more threads in your container than you like, etc.
听起来像是做出了一个设计决定,而不是 JDBC 驱动程序在内部实现逻辑,关于如何/何时产生线程来处理这个问题,API 希望您作为客户端传入一个 Executor,该执行程序将用于检查超时。通过这种方式,您作为客户端可以控制诸如检查执行的频率,防止它在您的容器中产生比您喜欢的更多的线程等。
If you don't already have an Executor instance around you can just create a default one:
如果您还没有 Executor 实例,则可以创建一个默认实例:
conn.setNetworkTimeout(Executors.newFixedThreadPool(numThreads), yourTimeout);
回答by Nanda
As far as Postgres JDBC driver is concerned (postgresql-42.2.2.jar), the setNetworkTimeout implementation does not make use of the Executor parameter. It simply sets the specified timeout as the underlying socket's timeout using the Socket.setSoTimeout method.
就 Postgres JDBC 驱动程序(postgresql-42.2.2.jar)而言,setNetworkTimeout 实现不使用 Executor 参数。它只是使用 Socket.setSoTimeout 方法将指定的超时设置为底层套接字的超时。
It looks like the java.sql.Connection interface is trying not to make any assumptions about the implementation and provides for an executor that may be used if the implementation needs it.
看起来 java.sql.Connection 接口试图不对实现做出任何假设,并提供了一个可以在实现需要时使用的执行器。