什么是 sql server 连接字符串中的“连接超时”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20142746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is "Connect Timeout" in sql server connection string?
提问by Tinwor
I have the following connection string(get from a property of sql server):
我有以下连接字符串(从 sql server 的属性中获取):
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\myUser\Desktop\adoBanche\Banche\bin\Debug\banche.mdf;Integrated Security=True;Connect Timeout=30
I don't understand what mean Timeout=30
. Someone could explain what means?
我不明白什么意思Timeout=30
。谁能解释一下是什么意思?
回答by Hans Ke?ing
That is the timeout to createthe connection, NOT a timeout for commands executed overthat connection.
这是创建连接的超时,而不是通过该连接执行的命令的超时。
See for instance http://www.connectionstrings.com/all-sql-server-connection-string-keywords/(note that the property is "Connect Timeout" (or "Connection Timeout"), not just "Timeout")
参见例如http://www.connectionstrings.com/all-sql-server-connection-string-keywords/(请注意,该属性是“连接超时”(或“连接超时”),而不仅仅是“超时”)
回答by sreejithsdev
Connect Timeout=30
means, within 30second sql server should establish the connection.other wise current connection request will be cancelled.It is used to avoid connection attempt to waits indefinitely.
Connect Timeout=30
意思是,sql server应该在30秒内建立连接。否则当前的连接请求将被取消。它用于避免连接尝试无限期等待。
回答by Rae Lee
Connection Timeout=30means that the database server has 30 seconds to establish a connection.
Connection Timeout=30表示数据库服务器有 30 秒的时间建立连接。
Connection Timeoutspecifies the time limit (in seconds), within which the connection to the specified server must be made, otherwise an exception is thrown i.e. It specifies how long you will allow your program to be held up while it establishes a database connection.
连接超时指定时间限制(以秒为单位),必须在该时间限制内建立到指定服务器的连接,否则将引发异常,即它指定在建立数据库连接时允许程序被阻止的时间。
DataSource=server;
InitialCatalog=database;
UserId=username;
Password=password;
Connection Timeout=30
SqlConnection.ConnectionTimeout.specifies how many seconds the SQL Server service has to respond to a connection attempt. This is always set as part of the connection string.
SqlConnection.ConnectionTimeout。指定 SQL Server 服务必须响应连接尝试的秒数。这始终设置为连接字符串的一部分。
Notes:
笔记:
The value is expressed in seconds, not milliseconds.
The default value is 30 seconds.
A value of 0 means to wait indefinitely and never time out.
该值以秒表示,而不是毫秒。
默认值为 30 秒。
值 0 表示无限期等待并且永不超时。
In addition, SqlCommand.CommandTimeoutspecifies the timeout value of a specific query running on SQL Server, however this is set via the SqlConnection object/setting (depending on your programming language), and not in the connection string i.e. It specifies how long you will allow your program to be held up while the command is run.
此外,SqlCommand.CommandTimeout指定在 SQL Server 上运行的特定查询的超时值,但是这是通过 SqlConnection 对象/设置(取决于您的编程语言)设置的,而不是在连接字符串中,即它指定您将使用多长时间允许您的程序在命令运行时暂停。
回答by Christos
Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.
获取尝试建立连接时等待的时间,然后终止尝试并生成错误。
回答by Aditi_Systematix
By default connection timeout is 240 but if you are faceing the problem of connection time out then you can increase upto "300" "Connection Timeout=300"
默认连接超时为 240,但如果您遇到连接超时问题,则可以增加到“300”“连接超时 = 300”
回答by Pablo Francisco Pérez Hidalgo
Maximum time between connection request and a timeout error. When the client tries to make a connection, if the timeout wait limit is reached, it will stop trying and raise an error.
连接请求和超时错误之间的最长时间。当客户端尝试建立连接时,如果达到超时等待限制,它将停止尝试并引发错误。
回答by Kiril Rusev
Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error. (MSDN, SqlConnection.ConnectionTimeout Property, 2013)
获取尝试建立连接时等待的时间,然后终止尝试并生成错误。(MSDN,SqlConnection.ConnectionTimeout 属性,2013)
回答by WonderWorker
How a connection works in a nutshell
简而言之,连接的工作原理
A connection between a program and a database server relies on a handshake.
程序和数据库服务器之间的连接依赖于握手。
What this means is that when a connection is opened then the thread establishing the connection will send network packets to the database server. This thread will then pause until either network packets about this connection are received from the database server or when the connection timeout expires.
这意味着当连接打开时,建立连接的线程将向数据库服务器发送网络数据包。然后,此线程将暂停,直到从数据库服务器收到有关此连接的网络数据包或连接超时到期。
The connection timeout
连接超时
The connection timeout is measured in seconds from the point the connection is opened.
连接超时以从连接打开时算起的秒数计算。
When the timeout expires then the thread will continue, but it will do so having reported a connection failure.
当超时到期时,线程将继续,但它会报告连接失败。
If there is no value specified for connection timeout in the connection string then the default value is 30.
A value greater than zeromeans how many seconds before it gives up e.g. a value of 10 means to wait 10 seconds.
A value of 0means to never give up waiting for the connection
如果连接字符串中没有为连接超时指定值,则默认值为 30。
大于零的值表示在放弃之前的秒数,例如值 10 表示等待 10 秒。
值 0表示永不放弃等待连接
Note: A value of 0 is not advised since it is possible for either the connection request packets or the server response packets to get lost. Will you seriously be prepared to wait even a day for a response that may never come?
注意:不建议使用 0 值,因为连接请求数据包或服务器响应数据包都可能丢失。你会认真准备等待一天的回应可能永远不会到来吗?
What should I set my Connection Timeout value to?
我应该将连接超时值设置为什么?
This setting should depend on the speed of your network and how long you are prepared to allow a thread to wait for a response.
此设置应取决于您的网络速度以及您准备允许线程等待响应的时间。
As an example, on a task that repeats hourly during the day, I know my network has always responded within one second so I set the connection timeout to a value of 2 just to be safe. I will then try again three times before giving up and either raising a support ticket or escalating a similar existing support ticket.
例如,在一天中每小时重复一次的任务中,我知道我的网络总是在一秒内做出响应,因此为了安全起见,我将连接超时值设置为 2。然后我会在放弃之前再试三遍,要么提出支持请求,要么升级类似的现有支持请求。
Test your own network speed and consider what to do when a connection fails as a one off, and also when it fails repeatedly and sporadically.
测试您自己的网络速度,并考虑当连接失败作为一次性失败时以及当它反复和偶尔失败时该怎么办。