c mysql 查询超时设置_MySQL查询超时的设置方法

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 这里顺带解释一下什么是non-interactive connection Non-Interactive Commands Just do a quick look up on a table without logging into the client, running the query then logging back out aga

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

这里顺带解释一下什么是non-interactive connection

> Non-Interactive Commands

Just do a quick look up on a table without logging into the client, running the query then logging back out again.

You can instead just type one line using the ' -e ' flag.

[sql]

c:\mysql\bin\mysql -u admin -p myDatabase -e 'SELECT * FROM employee'

net_read_timeout / net_write_timeout

The number of seconds to wait for more data from a connection before aborting the read. Before MySQL 5.1.41, this timeout applies only to TCP/IP connections, not to connections made through Unix socket files, named pipes, or shared memory. When the server is reading from the client, net_read_timeout is the timeout value controlling when to abort. When the server is writing to the client, net_write_timeout is the timeout value controlling when to abort. See also slave_net_timeout.

On Linux, the NO_ALarm build flag affects timeout behavior as indicated in the description of the net_retry_count system variable.

解释:这个参数只对TCP/IP链接有效,分别是数据库等待接收客户端发送网络包和发送网络包给客户端的超时时间,这是在Activity状态下的线程才有效的参数

JDBC setQueryTimeout函数:

为了避免查询出现死循环,或时间过长等现象,而导致线程阻塞,在获得Statement的实例后,stmt.setQueryTimeout(10); 避免因为查询导致程序出现线程阻塞。

但昨天发现程序出现了,“ORA-01013: 用户请求取消当前的操作”的异常。手工执行出错SQL语句发现,这个语句耗时20多秒。因为setQueryTimeout(10),所以还没有执行完查询语句就抛出异常了。使用setQueryTimeout(10)时一定要把时间设置的长一些,如60秒以上。只要不导致线程长期阻塞,就可以。太短了容易抛出,“ORA-01013: 用户请求取消当前的操作”的异常

JDBC实现setQueryTimeout的原理:

[java]

class IfxCancelQueryImpl extends TimerTask

implements IfmxCancelQuery

{

IfxStatement stmt;

Timer t = null;

public void startCancel(IfxStatement paramIfxStatement, int paramInt)

throws Exception

{

this.stmt = paramIfxStatement;

this.t = new Timer(true);

this.t.schedule(this, paramInt * 1000);

}

public void run()

{

try

{

this.stmt.cancel();

this.t.cancel();

}

catch (SQLException localSQLException)

{

this.t.cancel();

throw new Error(localSQLException.getErrorCode() + ":" + localSQLException.getMessage());

}

}

}

可见,query timeout是通过客户端解决方案来做的,服务器端无需知晓。通过一个timer线程来监控执行时间,如果执行时间超时,则会schedule run()函数。

[1] [2]

test.jsp?url=http%3A%2F%2Fwww.chinaitlab.com%2Fcms%2Fimages%2Flogo14.gif&refer=http%3A%2F%2FLinux.chinaitlab.com%2FMYSQL%2F923448_2.html

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值