title: How can I enforce a query timeout for MS SQL Server queries run via the MS JDBC Driver?
body: <p>How can we get the MS JDBC Driver to throw a timeout error after n seconds?</p>
  
  <p><strong>Background</strong></p>
  
  <p>We have an application that by default uses the Microsoft JDBC driver (version 4.0) to query SQL Server 2014.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I <em>have</em> found timeout settings for the jTDS driver (socketTimeout).</p>
  
  <p>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:</p>
  
  <pre><code>#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
  </code></pre>