Skip to content

Commit a8ee275

Browse files
Merge pull request #26784 from Jeffery-Wasty/timeouts
Timeout properties for JDBC summarized.
2 parents 9f93a66 + 8a010fa commit a8ee275

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎docs/connect/jdbc/improving-performance-and-reliability-with-the-jdbc-driver.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Improving performance and reliability
33
description: Learn about various techniques for improving application performance and reliability when using the Microsoft JDBC driver for SQL Server.
44
author: David-Engel
55
ms.author: v-davidengel
6-
ms.date: 01/12/2022
6+
ms.date: 05/19/2023
77
ms.service: sql
88
ms.subservice: connectivity
99
ms.topic: conceptual
@@ -21,6 +21,7 @@ The articles in this section describe various techniques for improving applicati
2121

2222
|Article|Description|
2323
|-----------|-----------------|
24+
|[Understanding timeout properties](understand-timeouts.md)| Describes the timeouts used by the JDBC driver.|
2425
|[Closing objects when not in use](closing-objects-when-not-in-use.md)|Describes the importance of closing JDBC driver objects when they're no longer needed.|
2526
|[Managing transaction size](managing-transaction-size.md)|Describes techniques for improving transaction performance.|
2627
|[Working with statements and result sets](working-with-statements-and-result-sets.md)|Describes techniques for improving performance when using the Statement or ResultSet objects.|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Understanding timeouts
3+
description: Learn about the timeouts used by JDBC. LoginTimeout, queryTimeout, cancelQueryTimeout, and socketTimeout can be used to ensure application responsiveness.
4+
author: David-Engel
5+
ms.author: v-davidengel
6+
ms.date: 06/05/2023
7+
ms.service: sql
8+
ms.subservice: connectivity
9+
ms.topic: conceptual
10+
---
11+
12+
# Understanding timeout properties in the JDBC driver
13+
14+
[!INCLUDE[Driver_JDBC_Download](../../includes/driver_jdbc_download.md)]
15+
16+
Timeout settings in the JDBC driver can be used to prioritize application responsiveness. By default, most of the driver's timeouts prioritize waiting for a result to ensure data consistency. Ensure you choose timeouts that are appropriate for your application's needs.
17+
18+
For the initial connection, `loginTimeout` is used:
19+
20+
- `loginTimeout` is the amount of time, in seconds, the driver waits to establish a connection to the server. If this amount is exceeded, an error is returned, and no open connection is established. A zero value indicates that the timeout is the default system timeout, which is 30 seconds in versions 11.2 and above. For versions 10.2 and below, the default timeout is 15 seconds. Any nonzero value is the number of seconds the driver should wait before timing out a failed connection. If you're consistently having trouble establishing a connection with the JDBC driver, you may need to increase this timeout to 90, or even 120, seconds.
21+
22+
Once the connection has been established, `queryTimeout`, `cancelQueryTimeout`, and `lockTimeout` are used during statement executions. `socketTimeout` is used for any driver communication with the server.
23+
24+
- `queryTimeout` is the time, in seconds, the driver will wait, after sending an execute command to the server, to receive a reply from the server with data. If this time is exceeded, the command is canceled. Exceeding this timeout doesn't close the connection. The default value is -1, which means infinite timeout.
25+
- `cancelQueryTimeout` is the time, in seconds, the driver waits for an acknowledgment of the `queryTimeout` cancellation from the server, before forcefully terminating/closing the connection. That is, the driver waits the total amount of `cancelQueryTimeout` plus `queryTimeout` seconds, before the connection is closed. Setting this timeout to a nonzero value ensures applications can remain responsive if there's network or communication failure with the server, when a query has timed out. The default value for this property is -1, which is an infinite wait time.
26+
- `lockTimeout` is the amount of time to wait for a lock to be freed, in cases where there's a lock blocking statement execution. Exceeding this timeout doesn't result in a closed connection. The default value for this property is -1, which is an infinite wait time.
27+
- `socketTimeout` applies to all socket communications with the server. If the server stops communication with the driver, either by not acknowledging or replying to data, the driver waits for the value of `socketTimeout` before it closes the connection. Setting this timeout to a nonzero value ensures applications can remain responsive if there's network or communication failure with the server. The default value is 0, which means an infinite timeout.
28+
29+
Reasonable timeout values for your application depend on the application's priorities. Setting lower values for timeouts prioritizes application responsiveness over data consistency. When timeouts are reached, applications need to decide the best course of action. That decision is based on the database action being performed. For example, for a `SELECT` statement, the decision might be to report an error to the user, or it may be to reconnect and retry. For `INSERT` or `UPDATE` statements, that decision may be different.
30+
31+
For a responsive application, `loginTimeout` and `queryTimeout` should be set to relatively low values. Similarly, `cancelQueryTimeout` should also be set to a low value to ensure the driver doesn't wait too long for the server to acknowledge the query cancellation, when a `queryTimeout` is exceeded. Finally, `socketTimeout` should be set to guard against the driver waiting too long during any scenario where connectivity to the server is broken (network interruption, server crash, etc.).
32+
33+
## Property summary
34+
35+
| Property | Description | Default | Connection result |
36+
|----------------------|---------------------------------------------------------------------------------------|-----------------------------------------------| ------------------ |
37+
| `loginTimeout` | The number of seconds the driver should wait before timing out a failed connection. | 30 seconds [11.2+], <br/>otherwise 15 seconds | Closed connection |
38+
| `queryTimeout` | The number of seconds to wait before canceling a query. | -1 [Infinite timeout] | Open connection |
39+
| `cancelQueryTimeout` | The number of seconds to wait for an acknowledgment of the QueryTimeout cancellation. | -1 [Infinite timeout] | Closed connection |
40+
| `lockTimeout` | The number of milliseconds to wait before the database returns a lock time-out error. | -1 [Infinite timeout] | Open connection |
41+
| `socketTimeout` | The number of milliseconds to wait on a socket read or write. | Zero [Infinite timeout] | Closed connection |
42+
43+
## See also
44+
45+
[Setting the connection properties](setting-the-connection-properties.md)

‎docs/connect/toc.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,6 +2700,8 @@
27002700
- name: Improving performance and reliability
27012701
href: ../connect/jdbc/improving-performance-and-reliability-with-the-jdbc-driver.md
27022702
items:
2703+
- name: Understanding timeout properties
2704+
href: ../connect/jdbc/understand-timeouts.md
27032705
- name: Closing objects when not in use
27042706
href: ../connect/jdbc/closing-objects-when-not-in-use.md
27052707
- name: Managing transaction size

0 commit comments

Comments
 (0)