sql: set connection timeout in the connection string - sql

In a console app I have a connection string like this:
Data Source=localhost;Initial Catalog=database_name;Integrated Security=True;Connect Timeout=100
I've read here that I need to add Connect Timeout to the connection string to set the timeout, but if I run a query that contains
WAITFOR DELAY '00:00:40';
I'll get a timeout exception, so it seems that the timeoput from the connection string is ignored. so how to set the timeout in the connection string (and not on the connection or command objects)?

There are two timeouts relating to SQL connections/commands - there is a connection timeout, that affects how long a connection is willing to wait when you try to open it, and a command timeout that affects how long an individual command being executed will wait.
You need to adjust the second of these - by e.g. setting the CommandTimeout property on the SqlCommand object.

It's not ignored, that's the behavior...if it times out, it throws an exception. If it didn't timeout, it would just run until the query returned.
Just use a try-catch block and do what you want when the exception is thrown.

Related

Cancel signal (close connection) what could be the cause?

In Kibana of our application, I keep seeing this line of log from org.springframework.web.reactive.function.client.ExchangeFunctions:
[2f5e234b] Cancel signal (to close connection)
The thread is reactor-http-epoll-1 or so.
It could happen in two situations:
when the connection is successful and returns a response, then it does not matter
when for some unknown cause, after 10 seconds, the connection does not return anything, and this line also happens, and period, nothing more. It seems to be a timeout but I am not sure(because the default timeout in my WebClient config is 10s)
What could be the cause of this? Client active drop or server active refusal?
Is the 2nd case a timeout? But not TimeoutException() is thrown afterwards.
I now do a doOnCancel() logging in WebClient to deal with the 2nd case, but then I notice there is case 1, and this doOnCancel() handling does not make sense anymore, because it seems to happen in all cases.
I have the same log. But in my WebClient i returned Mono.empty() and the method signature was Mono< Void>. After changing to Mono< String> the problem was gone.

Control the timeout for locking Exclusive SQLite3 database

I have a SQLite database that I want to lock for synchronization purposes. I don't want a process that runs async on a different box processing data that has been added from a different box until it has finished with updates. DataAccess is a class that connects to sPackageFileName and reuses the same connection as long as sPackageFileName is the same or unless .Close method is called. So basically DataAccess.ExecCommand executes a command.
In Google I found this ....
DataAccess.ExecCommand("PRAGMA locking_mode = EXCLUSIVE", sPackageFileName)
DataAccess.ExecCommand("BEGIN EXCLUSIVE", sPackageFileName)
DataAccess.ExecCommand("COMMIT", sPackageFileName)
This works as advertise. If I run this on box A and then on box B I get a "database locked" exception. The problem is how long it takes. I found a PRAGMA busy_timeout. This PRAGMA is timeout controls access locks, not database locks. I am stratring to think there is not PRAGMA for database lock timeout. Right now it seems about 3-4 minutes. One other note, the sPackageFileName is not on either box, they (box A and B) connect to it over a share drive.
Also I am using the VB.NET wrapper for the SQLite dll.
CL got me on the right trail. It was the timeout of the .NET command. Here the code setting it up from my class.
Dim con As DbConnection = OpenDb(DatabaseName, StoreNumber, ShareExclusive, ExtType)
Dim cmd As DbCommand = con.CreateCommand()
If _QueryTimeOut > -1 Then cmd.CommandTimeout = _QueryTimeOut
Don't get hang up on the variables, the purpose of posting the code is show I could show the property I was talking about. The default _QueryTimeOut was set the 300 (seconds). I set cmd.ComandTimeout to 1 (second) and it returned as expected.
As CL finally got through to me, the timeout was happening someplace else. Sometimes it takes a kick to get you out of the box. :-)

SSHLibrary Retry connection to host

I am using SSHLibrary 2.0 for Robot. I am trying to open connect to a host using private key, but sometime (not always) the connection does not establish.
Sample code below:
index = self.SSHLibrary.open_connection(host)
self.SSHLibrary.login_with_public_key(username,passkey, password`)
Is there a way to force a connection retry at least one more time?
You can use the keyword Wait until keyword succeeds, which will retry a keyword several times until it succeeds or times out.

SQL bcp The semaphore timeout period has expired

I'm making a bulk copy onto a file of a select in my database.
DECLARE #cmd varchar(1000)
DECLARE #sql varchar(8000)
SET #cmd='"select * from [MyDB].[dbo].MyTable"'
SELECT #sql = 'bcp '+#cmd+' queryout C:\myfile.txt -c -t -T -S MyServer -U user -P password';
exec xp_cmdshell #sql;
If I change the parameters and I execute the same command on the database test on my machine it works, but on the database server I get this error:
Msg 121, Level 20, State 0, Line 0
A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The semaphore timeout period has expired.)
I check server name, user, password, table name and they are correct, so I cannot understand what I'm doing wrong.
Can someone help me with this issue?
thanks
Increase timeout seconds in connection string.
private static void OpenSqlConnection() {
string connectionString = GetConnectionString();
using(SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionTimeout: {0}",
connection.ConnectionTimeout);
}
}
static private string GetConnectionString() {
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file, using the
// System.Configuration.ConfigurationSettings.AppSettings property
return "Data Source=(local);Initial Catalog=AdventureWorks;" + "Integrated Security=SSPI;Connection Timeout=30";
}
I had the same error. A third-party tool was connecting to the database to extract data for import into a business intelligence system. Each extract/import would run for about 1 hour and there would be something like 10 to 15 separate queries in the process. Fortunately we had logging in place - so we knew at what time each of those queries was starting and ending. The extract/import process was saying that it was completing successfully after 30 minutes - rather than taking about 1 hour. I was able to isolate the query during which the process was failing. When I ran that query in SSMS I got the same error you've given in the question.
However, when I ran that query in another environment, I got an error that a subquery cannot return more than one row.
Sure enough, when I commented out my subqueries in the prod environment, the query ran without any error.
So ultimately, the root cause for me was a subquery returning more than one row - and the issue only appeared because for some reason, "bad data" got into the database - ie. that particular subquery should never have found a scenario where there is more than one row, and hence too, the error just began to appear one day. (For other people who are experiencing this error intermittently - it could be because it is only some of your queries - or one of your queries - that is failing).

How to close connection created with WebMatrix.Data.Database.Open?

I'm using the Database class from webmatrix in a windows service. (And debugging in a winform causes same error)
The error I receive (after running for a while) is:
Timeout expired. The timeout period elapsed prior to obtaining a
connection from the pool. This may have occured because all pooled
connections were in use and max pool size was reached.
This should be solved by using using statements to make sure all connections are closed. ( as described in this good answer) But I've recently started using also the WebMatrix database class, and it does not seem to close the connections I open with it.
Question is: How to close connections created with the Database class from WebMatrix?
Using this code:
Public Shared Function GetProperty(pid As Integer, propertyName As String) As String
Dim db = Database.Open("SSEConnectionString")
Dim item = db.QuerySingle("select PropertyValue from eConfiguration where PID=#0 and PropertyName=#1", pid, propertyName)
Dim retVal = item.PropertyValue
db.Connection.Close()
db.Close()
Return retVal
End Function
Each time I run this code I get a new entry in my sys.sysprocesses table (and from what I've figured out, this is indicating a new connection is created and maintained)
My connectionstring looks like this:
<add name="SSEConnectionString" connectionString="Data
Source=123.45.67.890;Initial Catalog=SSE;User
ID=+++;Password=+++;Connect Timeout=5;Application Name=SSE_Service"
providerName="System.Data.SqlClient" />
Any idea what I'm doing wrong?
Edit: Asked a second question regarding the sysprocesses table - unsure if it helps in debugging
Thanks for any help
Edit2: Arghhhhh.... I just found what was causing the error - and it's only related to myself. I forgot to close a connection. :-(
Larsi
Use:
db.Connection.Close()
db.Close()