Netscaler MSSQL Mirror Monitor query not getting a response - sql-server

I'm trying to setup a NetScaler (version 9.3) monitor to determine which SQL server my database is running on (principal / mirror). The query I'm using is:
select mirroring_role_desc from sys.database_mirroring where database_id = DB_ID('OperationsManager')
The query works fine running under the user credentials that I created. The problem is, whenever the query comes from the NetScaler it's not completing. In the SQL Profiler trace it's showing an abort / rollback and I have no idea why.
I've enabled literally every trace event, including all the errors, to try to see why it's not completing and I've gotten nowhere. There are no Reads and no RowCounts. The connection appears to be aborting, but not from any sort of error.
This is what profiler looks like when it runs from the NetScaler
This is what it looks like when I run the same query, as the same user, from SSMS.
I thought maybe the NS was closing the connection too early so I upped the response timeout from 2s to 30s. That didn't make any difference. I'm completely baffled.

Can you let us know the version of NetScaler code you are running, and paste in the monitor CLI command from the CLI?
Also - you need to add a semi colon at the end of your query.

Add a semi colon to the end of your SQL statement in the monitor string. Otherwise the statement is not executed.

Related

Couldn't connect to database when using Top Resource Consumers QueryStore Report

We recently upgraded our SQL server to 2016 and I turned on QueryStore to do the analysis that it provides. I'm encountering a problem where, even if the time period of the report is Last hour, it will generate a message that says "Couldn't connect to database" even when running it on the database server itself. Sometimes if I keep refreshing the report it will eventually display some data, but it's intermittent at best. I'm running SSMS 17.5 on a sql server 2016 server.
We are having a somewhat similar issue with another program that connects to the database where it will sometimes not be able to connect, but every time I run my queries in SSMS, run reports in SSRS, or even use activity monitor, I never see any connection drops, so I'm not sure if it is related.
Thank you in advance for any help!
I find it works fine with the statistic set to Avg, StdDev, or Total. Max and Min give the error.
I found this happens when the query store runs out of space and gets into cleanup mode.
In database properties in SSMS try playing around with Query Store settings: for how many days it stores the query stats and does it get into "size cleanup" mode. More info on how to keep it adjusted: https://learn.microsoft.com/en-us/sql/relational-databases/performance/best-practice-with-the-query-store?view=sql-server-ver15#Configure

Screen/Database query keep on loading/executing unless sql service is restarted

I am using MVC razor in one of the Web Application using .Net
One of the screen in my web application has 2 DropDownList, 1 button and 1 Grid. When I click on 1st DDL, the screen instead of focusing on 2nd DDL it starts to keep on loading.. and gives exception: the timeout period expired prior to completion of the operation
DDL select query is: select * from Table where year='2014'and name='pqr'(used in code behind Entity Framework) which throws this exception. Same thing is happening in DB, when I run the above LINQ query in SSMS, it keeps on executing.
If I restart SQL Service, everything starts to work normal and the result is shown but after some time if I try same then again the same issue occurs. In this case, I need to restart the sql service again and again which shouldn't happen.
In SSMS, I checked Activity Monitoring and found that sometimes some SPId's are getting blocked and that time only screen/query keep on loading/executing.
Using DBCC INPUTBUFFER(spid);I came to know which Stored Procedure / Table is blocking (please correct if my understanding is wrong here). I also tried using with (NOLOCK) on certain tables inside Stored Proc, which also didn't workout for me.
How can I resolve this issue at DB level? My application is already live.
Note: This issue occurs intermittently.

How to stop a running query?

I use RODBC to send queries to an SQL-Server. Sometimes they take too much time to run, so I need to cancel them.
Clicking the red "stop" button in RStudio yields this error message:
R is not responding to your request to interrupt processing so to stop
the current operation you may need to terminate R entirely.
Terminating R will cause your R session to immediately abort. Active
computations will be interrupted and unsaved source file changes and
workspace objects will be discarded.
Do you want to terminate R now?
And if I click yes my session is indeed terminated. (note: using Rgui instead of RStudio doesn't make things better)
However:
when I use another software (named "Query ExPlus") to connect to this same SQL-Server, I have a similar stop button, and clicking it instantly interrupts the query, without any crash.
when I connect to a PostgreSQL database using the RPostgres package I can also interrupt the query at any time.
These two points lead me to think that there should be a way to solve my problem. What can I do?
So far my workaround is:
library(RODBC)
library(R.utils)
withTimeout(mydf <- sqlQuery(myconnection, myquery), timeout=120)
Note: I don't have permission to kill queries from the database side.
I've just stumbled upon the odbc package. It allows to interrupt a query at any time.
Basic usage goes like this:
library(DBI)
myconnection <- dbConnect(odbc::odbc(),
driver = "SQL Server",
server = "my_server_IP_address",
database = "my_DB_name",
uid = "my_user_id",
pwd = "my_password")
dbGetQuery(myconnection, myquery)
I don't have a deep understanding of what happens behind the scenes, but for what I've seen so far in my personal use this package has other advantages over RODBC:
really faster
get the column types from the DB instead of guessing them (see here)
no stringsAsFactors and as.is arguments necessary
Most SQL Server users use SQL Server Management Studio (which is free and can be downloaded from Microsoft) to connect to SQL Server or execute commands from the command line via a tool called SQLCMD.
If you can determine the session id that the SQL Command is being run in you can kill the session which would stop any executing command(s). SQL Server will still need time (could be a 'long' time) to rollback any changes made during the execution of the command.
Terminating a session (depending on the software) can take a while to communicate to SQL Server that the session has been terminated. When I connected to DB2 from SQL Server using linked servers DB2 would buffer the terminate command and it would frequently take up to an hour for DB2 to realize the session had been terminated.
To determine what the session you are running in you can try:
select ##spid;
once you have the spid (lets say 86)
you can then issue (depending on if you have permission to do so)
kill 86;
but as Microsoft notes:
Terminates a user process that is based on the session ID or unit of work (UOW). If the specified session ID or UOW has a lot of work to undo, the KILL statement may take some time to complete, particularly when it involves rolling back a long transaction.
Try to close your "tab query" on SQL Server Management Studio
Then it will appear pop-up,
This Query is currently executing. Do you want to cancel this query ?
Cancel anyway, choose "yes".
try to set your connection prior to query:
sql = odbcConnect('Database name')
Then use same line to run your query:
mydf <- sqlQuery(sql, " myquery ")
Note: The running time is dependant on both database and R server but setting up the connection this way should resolve termination problem.

Identify source of a SQL Server job running nightly

I am looking into performance issues with an app that uses SQL Server Express (Microsoft SQL Server 2014 (SP2) (KB3171021) - 12.0.5000.0 (X64)) for the back end database.
I'm logging slow running queries using Extended Events. Looking at these I have identified a job that runs every night a series of queries, some of which take quite an extended period. The longest single query takes 10 minutes or so. It seems to be some sort of reporting, gathering broad information about everything in the tables it is looking at.
This seems to be an automated process that is running nightly at exactly the same time and takes about the same length of time to execute each night (around 20 minutes).
It runs on a Windows account with Administrative privileges that connects to SQL using Windows authentication. There is other legit stuff we do know about also running on this account.
The client doesn't know what it is.
The app name logged is just ".Net SqlClient Data Provider" which I believe is a generic default.
This is different than the client application which identifies itself correctly. I have identified with the client everything else that is running against the database- there are a few other jobs that generate accounting emails for example.
I can't find anything in Windows Scheduled Tasks that corresponds and SQL Express obviously doesn't have SQL Server Agent- everything other SQL job I have identified is scheduled in Windows Scheduled Tasks.
I've tried turning login auditing on and I can see the login occurring but it gives even less information as to what it is, it doesn't even give me the client app name.
I have no reason to believe it is in any way malicious, it is likely something that was set up years ago and simply forgotten.
Any ideas for how I could pinpoint where exactly this is coming from?
You will need to do this while you think the mystery program is running.
Open a Remote Desktop windows session on the actual SQL server since you are pretty sure that is where it is running.
Have TaskMgr.exe ready. Go to the “Details” tab so that you can search by TaskManager PID (not SQL PID) . Sort by this TaskMgr PID.
Right Click on the Columns at the top of this Details tab.. Choose “Select Columns”
Add this column …
Command line
While you think the mystery program is running run the following sql.
select x.host_process_id as USE_ME_TaskMgr_PID_OnClient_PC
, x.host_name as PCNameFromClient
, x.program_name as ProgNmFromClient
, x.client_interface_name
, x.original_login_name
, x.login_name
, x.nt_domain
, x.nt_user_name
, x.*
FROM sys.dm_exec_sessions x
order by x.host_name DESC
, x.program_name DESC
GO
Look at the first column of this sql. Find your mystery program in the list. Take the first value. This is the Program PID that will be the PID on the TaskMgr PID. Then you will see the command line full execution directory path.
By using something like sp_who you can find the hostname for each user connecting. Maybe that can give you a hint on where to look?

Error 17886 - The server will drop the connection

We are running a website on a vps server with sql server 2008 x64 r2. We are being bombarded with 17886 errors - namely:
The server will drop the connection, because the client driver has
sent multiple requests while the session is in single-user mode. This
error occurs when a client sends a request to reset the connection
while there are batches still running in the session, or when the
client sends a request while the session is resetting a connection.
Please contact the client driver vendor.
This causes sql statements to return corrupt results. I have tried pretty much all of the suggestions I have found on the net, including:
with mars, and without.
with pooling and without
with async=true and without
we only have one database and it is absolutely multi-user.
Everything has been installed recently so it is up to date. They may be correlated with high cpu (though not exclusively according to the monitors I have seen). Also correlated with high request rates from search engines. However, high cpu/requests shouldn't cause sql connections to reset - at worst we should have high response times or iis refusing to send response.
Any suggestions? I am only a developer not dba - do i need a dba to solve this problem?
Not sure but some of your queries might cause deadlocks on the server.
At the point you detect this error again
Open Management Studio (on the server, install it if necessary)
Open a new query window
Run sp_who2
Check the blkby column which is short for Blocked By. If there is any data in that column you have a deadlock problem (Normally it should be like the screenshot I attached, completely empty).
If you have a deadlock then we can continue with next steps. But right now please check that.
To fix the error above, ”MultipleActiveResultSets=True” needs to be added to the connection string.
via Event ID 17886 MSSQLServer – The server will drop the connection
I would create an eventlog task to email you whenever 17886 is thrown. Then go immediately to the db and execute the sp_who2, get the blkby spid and run a dbcc inputbuffer. Hopefully the eventinfo will give you something a bit more tangible to go on.
sp_who2
DBCC INPUTBUFFER(62)
GO
Use a "Instance Per Request" strategy in your DI-instantiation code and your problem will be solved
Most probably you are using dependency injection. During web development you have to take into account the possibility of concurrent requests. Therefor you have to make sure every request gets new instances during DI, otherwise you will get into concurrency issues. Don't be cheap by using ".SingleInstance" for services and contexts.
Enabling MARS will probably decrease the number of errors, but the errors that are encountered will be less clear. Enabling MARS is always never the solution, do not use this unless you know what you're doing.

Resources