Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql client transactions from code vs database-controlled transactions

I've always done transactions from within stored procedures but now I need to wrap a bunch of "dynamic" statements executed from code against sp_executesql in a transaction.

Specifically I need the READ UNCOMMITED isolation level for these in some cases (I know what that does, and yes, that's what I need). This is SQL2008.

My question is this: If I use the BeginTransaction() method of my SqlConnection instance with the isolation level set to IsolationLevel.ReadUncommitted will that have the same effect as if I executed a stored proc that has the READ UNCOMMITED statement?

like image 746
kprobst Avatar asked Aug 09 '10 17:08

kprobst


People also ask

What is SQL database transaction?

A transaction is a unit of work that is performed against a database. Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program. A transaction is the propagation of one or more changes to the database.

When should I use transaction in SQL?

If you need the set to fail or succeed as a whole then wrap it in a transaction. The classic example is transfer of money from a checking to a savings account - if the deposit fails then I want the withdrawal to fail. If data out of synch because of a developer mistake is OK then don't use transactions.

What are the two ways of ending a transaction in SQL?

A transaction begins with the first executable SQL statement. A transaction ends when it is committed or rolled back, either explicitly with a COMMIT or ROLLBACK statement or implicitly when a DDL statement is issued.


1 Answers

Yes, it will.

The SqlConnection uses the SQL native client, and a call to BeginTransaction causes exactly this to be sent to the server:

SET TRANSACTION ISOLATION LEVEL <WHATEVER>; BEGIN TRANSACTION;
like image 162
GSerg Avatar answered Sep 29 '22 23:09

GSerg