如何使用Java.sql.Connection.setNetworkTimeout? [英] How to use Java.sql.Connection.setNetworkTimeout?

查看:414
本文介绍了如何使用Java.sql.Connection.setNetworkTimeout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了setNetworkTimeout应该根据 Oracle 解决的确切问题.查询在socket.read()中停留了几分钟.

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.

但是我不知道此方法的第一个参数是什么.提交null会导致AbstractMethodError异常,因此...该实现是否真的需要某种线程池来设置网络超时?

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?

是否有某种方法可以在不为此情况运行线程池的情况下达到相同的效果?

Is there some way to achieve the same effect without running a thread pool just for this one condition?

推荐答案

文档似乎对此进行了可怕的解释,但是如果不查看该类背后的任何代码,我想您可能希望将Executor实例传递给该实例.方法,以便实现可以生成作业/线程以检查连接状态.

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.

由于连接读取将被阻塞,为了实现任何超时逻辑,除了读取一个可以检查连接状态的线程外,还必须有另一个线程.

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.

听起来好像已经做出了一个设计决定,该API希望您作为客户端传入一个Executor,而不是在内部实现逻辑的JDBC驱动程序(如何/何时产生线程来处理该逻辑),而不是在内部实现该逻辑.检查超时.这样,作为客户端的您可以控制检查执行的频率,防止它在您的容器中产生比您期望的线程更多的线程等.

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.

如果您还没有Executor实例,则可以创建一个默认实例:

If you don't already have an Executor instance around you can just create a default one:

conn.setNetworkTimeout(Executors.newFixedThreadPool(numThreads), yourTimeout);

这篇关于如何使用Java.sql.Connection.setNetworkTimeout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆