is it possible to commit/rollback another procedures transaction in current procedure - sql-server

i have two questions
1.i have two stored procedures. is it possible to commit/rollback another procedure's transaction in my current procedure.
2.i have two webservices two services connected with same database or linked server database. one webservices gotsucceed it transactions. when moving to the second webservice some error got occured. if error occured i have to rollback the previous webservice transactions.? is it possible. if anyone explain related to banking transactions like ATM
is it possible?
how?
explain related to banking sector with little understandable coding.

No, a commit must be issued from the same connection that the begin transaction statement was issued.
In this case you would first need to append the data tables with a "transaction" field or something similar that will uniquely identify each transaction. If the second webservice needed to issue a rollback that touched the work of the first webservice, it would have to call a custom process would then issue deletes looking for the transaction identifier that you have built into the tables. There's no built-in functionality of the db engine to accommodate this out of the box.

Related

SQL Server SPIDS go into a sleeping state and never recover

I have a long running stored procedure that is executed from IIS. On average this stored procedure takes between two and five minutes to complete because it is searching through a large dataset. (although it has take around 20 minutes in some cases)
Most of the time the stored procedure works fine but every now and then the SPIDS go into a sleeping state and never recover. The only solution I have found is to restart the SQL Server and re-run the stored procedure
The are no table inserts in the proc (only table variable inserts), and the other statements are selects on a large table.
I'm stuck for where to start debugging this issue. Any hints one what it might be or suggestions on tools that would help me find the issue would be most helpful
EDIT: More info added:
The actual issue is the proc doesn't return the resultset. My first thought was to look at the spids, they were sleeping but the cputime was still increasing
It's a .Net app so .Net Core 3.1 with ASP.NET Core and a Blazor UI. The libary used for db connection is System.data.SqlClient I believe System.data.SqlClient uses it's own custom driver. Calling code below:
The stored procedure doesn't return multiple result sets, however obviously different instances of the proc run at the same time.
No limits to connection pooling in IIS
#RichardWatts when you say " re-run the stored procedure" you mean that the same stored proc with the same parameter and data works once you restart SQL Server ?
If so look over your loc (sp_loc} inside your table probably another process loc some data and doesnt release it properly, specialy if you have transaction accessing the same tables.
What is your your isolation level on your connexion ? If you can, try to change it to READ UNCOMMITTED to see if that solve your problem.
as an alternate you can also add a WITH (NOLOCK) or (READUNCOMMITTED) to your sql command.
Know that you will need to hold query with a read uncommited or nolock if you have some modification on the structure of your table or index re construction for example or they will in turn block its execution
Nevertheless be cautious this solution depend on your environment, specially if your tables gots lots of update, delete, insert,... this kind of isolation can lead to a Dirty read and doesnt adress the root cause of your problem wich I would bet is uncomited transaction (good article that explain it)
Make also a DBCC CHECKTABLE just to be sure on this side

Purpose of TRAN and COMMIT in single/multiple SQL INSERT SELECT UPDATE DELETE statement

Can someone please explain to me in simple layman words what the SQL transaction is doing here?
Here is the scenario:
I am not SQL developer or data engineer. I wish to update column value depending on a filter that you see in where clause. I ran the query in database as I do have an admin rights, it was with approvals of course.
I typed in below command as per my need.
update idx_task set IDXTaskStatusDE=4 where ID='task_ID'
The developer then sent to me the following query and said I was doing the wrong way.
Use Works
begin tran
update idx_task set IDXTaskStatusDE=4 where ID='task_ID'
commit tran
Developer's email response:
When issuing adhoc statements directly against SQL, it is a good idea to wrap your statements in a transaction, just in case something goes wrong and you need to rollback. Updating the same table twice would be different than with or without the transaction. Hence, this is a professional practice which can be unknown to amateurs like you. This job seems to be outside your primary role. Please discuss with your boss and get your team a profession SQL developer.
My Efforts:
Hence, I am here after reading technical jargon to get in few words or with example how both queries would act differently anyway.
My Question:
1.) Can someone please provide a simple theoretical explanations of difference here, of significance of wrapping the single update statement in tran?
2.) is it for any prior, future, safety or professional practices in SQL developers community?
Thank you note to all saviours here
Thanks in advance to all types of responses. Your any type of feedback shall help me gain knowledge anyways :)
Take Care !!
Refer this question's thread. It will clear your confusion.
Using transaction on a single update statement
#Panagiotis Kanavos has pointed out and articulated perfectly.
Just know the simple meaning of following term while you read in regards to SQL Transactions:
1.) Atomic: "Atomic" means "cannot be divided or split in smaller parts". Applied to 1NF this means that a column should not contain more than one value. It should not compose or combine values that have a meaning of their own.
2.) Commit: A COMMIT statement in SQL ends a transaction within a relational database management system (RDBMS) and makes all changes visible to other users.
3.) Lock: Is a mechanism to ensure data consistency. SQL Server locks objects when the transaction starts. When the transaction is completed, SQL Server releases the locked object. Hence, in your case as pointed out by #Panagiotis Kanavos, the TRAN is doing the work of blocking all other commands if executed by other developers working in parallel to the same data that you are executing your UPDATE statement on.
Hope this helps. I do not think there would be more going down as "simple words" than this. Still feel free to ask if you still have a confusion.

Disable transactions on SQL Server

I need some light here. I am working with SQL Server 2008.
I have a database for my application. Each table has a trigger to stores all changes on another database (on the same server) on one unique table 'tbSysMasterLog'. Yes the log of the application its stored on another database.
Problem is, before any Insert/update/delete command on the application database, a transaction its started, and therefore, the table of the log database is locked until the transaction is committed or rolled back. So anyone else who tries to write in any another table of the application will be locked.
So...is there any way possible to disable transactions on a particular database or on a particular table?
You cannot turn off the log. Everything gets logged. You can set to "Simple" which will limit the amount of data saved after the records are committed.
" the table of the log database is locked": why that?
Normally you log changes by inserting records. The insert of records should not lock the complete table, normally there should not be any contention in insertion.
If you do more than inserts, perhaps you should consider changing that. Perhaps you should look at the indices defined on log, perhaps you can avoid some of them.
It sounds from the question that you have a create transaction at the start of your triggers, and that you are logging to the other database prior to the commit transaction.
Normally you do not need to have explicit transactions in SQL server.
If you do need explicit transactions. You could put the data to be logged into variables. Commit the transaction and then insert it into your log table.
Normally inserts are fast and can happen in parallel with out locking. There are certain things like identity columns that require order, but this is very lightweight structure they can be avoided by generating guids so inserts are non blocking, but for something like your log table a primary key identity column would give you a clear sequence that is probably helpful in working out the order.
Obviously if you log after the transaction, this may not be in the same order as the transactions occurred due to the different times that transactions take to commit.
We normally log into individual tables with a similar name to the master table e.g. FooHistory or AuditFoo
There are other options a very lightweight method is to use a trace, this is what is used for performance tuning and will give you a copy of every statement run on the database (including triggers), and you can log this to a different database server. It is a good idea to log to different server if you are doing a trace on a heavily used servers since the volume of data is massive if you are doing a trace across say 1,000 simultaneous sessions.
https://learn.microsoft.com/en-us/sql/tools/sql-server-profiler/save-trace-results-to-a-table-sql-server-profiler?view=sql-server-ver15
You can also trace to a file and then load it into a table, ( better performance), and script up starting stopping and loading traces.
The load on the server that is getting the trace log is minimal and I have never had a locking problem on the server receiving the trace, so I am pretty sure that you are doing something to cause the locks.

Is it possible to use transaction between two instances without enable DTC?

I have two instances of database located in two servers. I want to create an application to insert data into the first one and then update data on the second instance, if one of these process fail then I want to rollback all operations.
The database servers do not enable DTC/MSDTC. I tired to use transaction scope but no luck. Do you guys have any idea how can I do this?
if one of these process fail then I want to rollback all operations
You are describing a distributed transaction. To use distributed transactions, you need a transaction coordinator. You can't have the cake and eat it too.
There are alternatives if you consider asynchronous application of the changes, ie. Replication. This removes the distributed transaction requirement but the changes are applied asynchronously to the second server, after they are committed on the first server.
One option would be to put compensation code into your application. For example, if your application were c# based, you could have a try...catch block. In the catch block, you could add compensation code to "undo" the changes you made to the data on the first server.
The best method however, is of course to make a case to the DBAs to enable DTC

SQL server transaction

I need to understand about sql server transaction? I have gone through some articles available on google but I have not understood anything. Can anyone help me?
You can explicitly start a transaction by writing BEGIN TRANSACTION. You end the transaction by running COMMIT TRANSACTION.
Before the COMMIT is run, the tables affected by your query can still be rolled back to the state they were in at the BEGIN TRANSACTION point-in-time.
This is useful when you are writing a stored procedure that is pumping a lot of data between tables. By dividing it in smaller parts using transactions, the whole bunch does not need to be "rolled back" when the procedure hangs, an error occurs or you cancel it manually.
An article that elaborates on this is for example this one.
See Understanding Transactions.
Database Journal have a lot of good articles abaut mentoed subject

Resources