Transaction Locked Down SQL Server 2005 - sql

I have noticed something over the last few months. Each time we run a script that has a transaction statements let say we stop the query unexpectedly this action actually lock the database.
The only way out is to destroy the transaction each time. I have never experience this before even though I have stopped the query in the middle of transaction in the past and it has never locked the database.
Could it be that we are missing something in settings or I should not stop transaction queries unexpectedly?
The problem occurred with SQL SERVER 2005. please I need your brain. Thanks Guys

This is usual: you have sent a client abort which say "stop processing"
To rollback and release lock, you need to use SET XACT_ABORT ON
SO 1 and SO 2

Related

Why is SELECT creating an uncommitted transaction in SQL Server?

I have disabled autocommit in SSMS.
I have been playing around with it today and after some bizarre (to me, anyway) behaviour narrowed the use case to the following:
connect to db
open a new a query (ctrl n)
issue a simple SELECT statement eg
select TOP (5) * from AdventureWorks2017.Person.Person
close the window
I get the following message in a dialog (dialogue, hmm) box:
There are uncommitted transactions. Do you wish to commit these transactions before closing the window?
Being a long-time Oracle user, I was aware there are differences in locking between Oracle and SS, but selects causing an outstanding transaction that needs to be committed or rolled back? Really? This can't be so, surely. No data has changed. Please could somebody explain? Thanks in advance.
When you're working with the console, it automatically puts your queries into a transaction. Because you haven't closed the transaction it automatically started when you started running queries of any kind, you still have a transaction open.
This MSDN article talks a bit about it.
https://learn.microsoft.com/en-us/sql/t-sql/statements/set-implicit-transactions-transact-sql

What is the effect of having open transaction in SQL Server for too long?

I'm just wondering what happens if you opened a transaction in SQL Server and forgot to commit or roll it back? Will the server be down? Let's say you left it for 3 days.
There are also users who are using it for too long assuming that the other users did not know that there is an unclosed transaction (let's just assume that the users are just inserting data on the database).
What are the consequences of this action?
The server such wont be down.
Here are few things that may happen
1.Table might be totally blocked
2.Since this transaction is not committed,log space reusing wont happen
3.Number of locks taken *96 bytes amount of memory will be blocked
4.memory reserved for this query execution might not be released

If I KILL(SPID), what happen the running transaction?

I have a procedure, currently running at one SPID. Now, I found the query running too slow. In this Proc update/insert going on. If I kill the session, will what happen?
SQL Server will stop executing the query and rollback any open transactions. That rollback will undo any changes that haven't been fully committed. Since SQL Server adheres to the ACID principle, you shouldn't be able to leave your database in a bad state, even by killing SPIDs. That isn't to say you couldn't leave your data in a bad state, i.e. not wrapping multiple operations in a transaction to enforce consistency upon failure.
https://msdn.microsoft.com/en-us/library/ms173730.aspx
Check the docs here. In a nutshell which ever spid you kill will be disconnected
KILL can be used to terminate a normal connection, which internally terminates the transactions that are associated with the specified session ID

SQL Server database keeps locking

I have a SQL Server 2005 database that keeps locking and won't release. My application cannot commit updates to the database because there are tasks that are waiting to be processed and my app keeps crashing. If I look on activity monitor, waiting tasks just keeps going up and up until I kill the process. The problem is I can see what is in activity monitor that is causing the lock but I do not have enough information it just says blocked by session ID.
Is there any way by using TSQL to find out what that process is exactly and what is it doing? i.e. query for locks on database with long wait time and how to force them to release, or prevent them?
Try sp_lock #ProcessID then pick out the exclusive locks (mode = X or IX). You can then find out the offending objects using SELECT object_name(id). The ID is obtained from the ObjId column.
Use the Deadlock Graph event in Profiler: See Track Down Deadlocks Using SQL Server 2005 Profiler

Killing the mysqld process

I have a table with ~800k rows. I ran an update users set hash = SHA1(CONCAT({about eight fields})) where 1;
Now I have a hung Sequel Pro process and I'm not sure about the mysqld process.
This is two questions:
What harm can possibly come from killing these programs? I'm working on a separate database, so no damage should come to other databases on the system, right?
Assume you had to update a table like this. What would be a quicker / more reliable method of updating without writing a separate script.
I just checked with phpMyAdmin and it appears as though the query is complete. I still have Sequel Pro using 100% of both my cores though...
If you're using InnoDB, which is backed by a transaction log for recovery and rollback purposes, then you can get away with a lot, especially in a non-production environment.
The easiest way to terminate a renegade query is to use the MySQL shell as the root user:
SHOW PROCESSLIST;
This will give you a list of the current connections and a process ID for each one. To terminate any given query, such as number 19, use:
KILL 19;
Usually this will undo and roll back the query. In some cases this is not sufficient and you may have to force-quit the MySQL server process with kill -9. Under most circumstances you should be able to restart the server right away, and the DB will be in the last fully committed state.
To get the thread IDs (it'll show the query alongside):
mysqladmin proc
To safely kill the query thread:
mysqladmin kill [id]
You'll end up with a partially updated table unless you use innodb, but you should be fine. Details:
During UPDATE or DELETE operations,
the kill flag is checked after each
block read and after each updated or
deleted row. If the kill flag is set,
the statement is aborted. Note that if
you are not using transactions, the
changes are not rolled back.
As for your second question, there is no better way to update a table if one is not allowed to write a separate script (to, say, throttle the updates).