SQL Server Log - Souce (SPID) - sql-server

In checking Log File Viewer for noteworthy events I came across a log that I wanted to investigate.
Let's say the log notes the Source as SPID1234
My question:
Is this SPID tied to the actual connection that generated the log or is the SPID associated with the process that is writing this to the log?

In your scenario, the SPID1234, which means the session that is running this command has ID of 1234.
from here
A SPID in SQL Server is a Server Process ID. These process ID’s are
essentially sessions in SQL Server. Everytime an application connects
to SQL Server, a new connection (or SPID) is created. This connection
has a defined scope and memory space and cannot interact with other
SPIDs. The term SPID is synonymous with Connection, or Session.

SPID in Transaction Log - is ID of the process which originated the transaction, which was written into Transaction Log

Related

Locked by wait type OLEDB with SQL server 2012

I have a batch process that generate one linked server over huge Excel files to fetch data into SQL Server 2012
Sometimes the process is locked by a wait type "OLEDB"
I can't find the root ause but my biggest problem is that I can't kill the process that has the wait_type OLEDB
I have try
KILL spid
Not works, never kill the spid.
SPID 90: transaction rollback in progress. Estimated rollback completion: 0%. Estimated time remaining: 0 seconds.
ALTER DATABASE [DataMigration] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
But never finish (due PRINT_ROLLBACK_PROGRESS)
If I look into the process, I see the file that cause the block, but I don't know how solve the issue
SELECT * FROM SYSPROCESSES where spid=90
-There is any way to kill this process without restart the servers?
-How can avoid the wait type OLEDB? the same file in the same location usually works fine, the process hangs only some times.
This is quite simple. OLEDB as a wait type often indicates some wait on another Server. This is also the reason why you can't kill the process. OLEDB is often (not always) used as a wait type for connections between SQL Server instances.
You kill the connection on your server, but if the process is running on another instance/linked server it will run there too. It will kill the process after the process on the other instance is finished.
So much to the bad news. The good news for you is, that you easily can find the query which runs on another linked server using this query:
SELECT spid, waitresource
FROM sys.sysprocesses
WHERE spid = <yourKilledSpid>
Just filter it for the spid you try to kill. The waitresource will indicate the remote server including the spid on the remote server. Go to the remote server and kill the spid there too. Your connection will immediately be killed/rolledback. Hopefully this solves your issue.
You can additionally try to take a look at the waiting_tasks. Maybe you'll see something helpful like a blocking resource in there.
SELECT *
FROM sys.dm_os_waiting_tasks

Process/SPID is being blocked by itself, how to clear/kill without restarting Sql Server

We have a process that was running for 4 hours. Because it was running so long, it was causing other issues in the database, so it was decided to kill the process.
Now, the process is in a suspended state. It also states that it's being blocked by itself after querying sp_who2.
In activity monitor, here's the waitresource information:
objectlock lockPartition=0 objid=xxx subresource=FULL dbid=2 id=lockyyyy mode=X associatedObjectid=xxx
You'll notice that the objid and associatedObjectId are the same value.
Querying the sys.objects table shows NO results for that object id.
Is Sql Server waiting for a lock on an object that doesn't exist anymore? How can I get rid of this process without restarting Sql Server? (our DBA's are not responding to help requests).
Keep in mind, this is a test environment, but it is stopping all development/testing because we are unable to deploy any changes to our database, because one of those changes is affecting one of the objects that the process was accessing.
Edit: more info from activity monitor:
Command = 'KILLED/ROLLBACK'
TASK STATE = 'SUSPENDED'
I have experienced this may times. When you kill a large INSERT/UDPATE/DELETE statement, it can take hours to recover (if it ever does recover) from this state.
run kill <spid> with statusonly.
It will give you a percentage and estimated wait time of the ROLLBACK process.
Sometimes it says 0% or 100% and 0 estimated time. If you are patient, it may recover eventually. If you restart the server, the rollback process will be completed offline, and the database will show IN RECOVERY state and usually will be faster than waiting the server to recover itself.
Be aware that users won't be able to use the database until the recovery process ends, but if the SPID in KILLED/ROLLBACK state is locking other process, it might be an option to restart.
Well, this seems to be lock due to parallel processing inside the tempdb.
You can try kill [processid] if you have the rights to?
Another way is to get more detailed process information with this:
SELECT * FROM sys.sysprocesses WHERE spid = YOURSPID
As the Process runs in DB:2 try this:
SELECT * FROM tempdb.sys.all_objects WHERE object_id = OBJECTID
As I've seen, you have edited your question. If the Spid is in KILLED/ROLLBACK you have to wait until your transaction is rolled back. After that the process will be killed and removed. You can't do anything else, as the transaction security must be given.

SQL Server SPID doesnt change for every connection

We are building an ERP system, and sometimes we need to see the SQL statement in SQL Server Profiler for debuging purposes. I can catch my ERP's queries by the SPID on Profiler. My ERP doesnt keep an open connection, it opens a connection everytime it needs it.
I have been thinking that everytime my ERP opens a connection, it should get a new SPID. How does it give the same SPID?
SPIDs are recycled, they are guaranteed to be unique at any one time, but if a connection drops, its SPID can be reused by a new connection.

Sending and Receiving SQL Server Service Broker Messages within Nested Transactions

I'd like to use SQL Server 2008 Service Broker to log the progress of a long-running (up to about 30 minutes) transaction that is dynamically created by a stored procedure. I have two goals:
1) To get real-time logging of the dynamically-created statements that make up the transaction so that the progress of the transaction can be monitored remotely,
2) To be able to review the steps that made up the transaction up to a point where a failure may have occurred requiring a rollback.
I cannot simply PRINT (or RAISERROR(msg,0,0)) to the console because I want to log the progress messages to a table (and have that log remain even if the stored procedure rollsback).
But my understanding is that messages cannot be received from the queue until the sending thread commits (the outer transaction). Is this true? If so, what options do I have?
It is true that you cannot read messages from the service queue until the transaction is committed.
You could try some other methods:
use a sql clr procedure to send a .net remoting message to a .net app that receives the messages and them log them.
use a sql clr procedure to write a text or other log file to disk.
Some other method...
Regards
AJ

Drop all active database connections failed for Server when executing KillAllProcesses

I need to perform a database restore from my application. Before doing this, I want to kill all processes as follows:
private void KillAllProcessesOnSMARTDatabases(Server targetServer)
{
targetServer.KillAllProcesses(SMART_DB);
targetServer.KillAllProcesses(SMART_HISTORY_DB);
targetServer.KillAllProcesses(SMART_METADATA_DB);
SqlConnection.ClearAllPools();
}
However, when the first KillAllProcesses is run, I get the following exception:
Microsoft.SqlServer.Management.Smo.FailedOperationException: Drop all active database connections failed for Server 'MYServer'. ---> Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. ---> System.Data.SqlClient.SqlException: Only user processes can be killed.
The connection string used to create the server has sa credentials, however, the processes that need to be terminated are started under a different user. I tested the similar scenario and the test succeeded.
This started happening only recently. To me it appears there are some processes running that are not started by the user?
It would appear that your code is attempting to terminate all SQL Server Processes, which is not a good idea.
If you want to perform a database restore, you should set the database in question into either single_user mode or RESTRICTED_USER mode, the later being the most suitable.
Take a look at the following example of switching a database to RESTRICTED_USER mode and how to close any open user connections in the process.
How to: Set a Database to Single-User mode
You can use SMO to "kill" a particular database.
This will force a drop of all client connections to that database only and then drop the database itself.
Microsoft.SqlServer.Management.Smo.Server oServer = this.GetSmoServer();
oServer.KillDatabase(this.DatabaseName);

Resources