如何为通过MS JDBC驱动程序运行的MS SQL Server查询强制执行查询超时? [英] How can I enforce a query timeout for MS SQL Server queries run via the MS JDBC Driver?

查看:159
本文介绍了如何为通过MS JDBC驱动程序运行的MS SQL Server查询强制执行查询超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取MS JDBC驱动程序在n秒后引发超时错误?

How can we get the MS JDBC Driver to throw a timeout error after n seconds?

背景

我们有一个默认情况下使用Microsoft JDBC驱动程序(版本4.0)查询SQL Server 2014的应用程序.

We have an application that by default uses the Microsoft JDBC driver (version 4.0) to query SQL Server 2014.

大多数情况下,查询需要10到20秒才能完成.但是,查询有时需要5分钟以上才能完成.

Most of the time, queries take 10-20 seconds to finish. However, from time-to-time the queries take more than 5 minutes to complete.

我们希望查询花费5分钟以上的时间被应用程序视为错误.当前,该应用程序不提供超时设置,因此我们仍在研究MS JDBC驱动程序的超时设置,但是我找不到任何可以将长时间运行的查询标记为超时错误的内容.

We'd like the queries that take more than 5 minutes to be seen as an error by the application. Currently the application provides no timeout settings, so we are left researching timeout settings for the MS JDBC Driver, but I can't find anything to mark long running queries with a timeout error.

已经找到了jTDS驱动程序的超时设置(socketTimeout).

I have found timeout settings for the jTDS driver (socketTimeout).

MS驱动程序是否有等效的"socketTimeout"或"queryTimeout"?我已经尝试了以下所有方法,但它们均不适用于MS Driver:

Is there an equivalent "socketTimeout" or "queryTimeout" for the MS Driver? I've tried all of the following and none of them work for the MS Driver:

#None of these enforce a timeout with the MS Driver
jdbc:sqlserver://hostname\instanceName;databaseName=mydb;queryTimeout=10
jdbc:sqlserver://hostname\instanceName;databaseName=mydb;socketTimeout=10
jdbc:sqlserver://hostname\instanceName;databaseName=mydb;queryTimeout=10


#This works great with the jTDS driver:
jdbc:sqlserver://hostname\instanceName;databaseName=mydb;socketTimeout=10

推荐答案

Microsoft MSSQL-JDBC现在是开源的.最近,MSSQL-JDBC团队实施了 sockettimeout & 查询超时.这肯定会解决您的问题.

Microsoft MSSQL-JDBC is now Open sourced. Recently MSSQL-JDBC team implemented sockettimeout & querytimeouts. This will definitely solve your problem.

带有连接查询:

String conURL = "jdbc:sqlserver://localhost;userName=sa;password=PASSW0RD;database=master;queryTimeout=5";
SQLServerConnection con = (SQLServerStatement) DriverManager.getConnection(conURL);

或使用DataSource....

or with DataSource....

SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser("sa");  
ds.setPassword("PASSWORD");  
ds.setServerName("localhost");  
ds.setPortNumber(1433);   
ds.setDatabaseName("master");
ds.setQueryTimeout(5);
SQLServerConnection con = (SQLServerConnection) ds.getConnection();

引用: https://github.com/Microsoft/mssql-jdbc/wiki /QueryTimeout

这篇关于如何为通过MS JDBC驱动程序运行的MS SQL Server查询强制执行查询超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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