Transaction Isolation Error Handling

I have two sessions executed in step orders as noted noted in the following codes.

Question: How can I complete and make Step 4: (Second Session) function?

First Session:
-- Step 1: (First Session)
use TSQL2012
BEGIN TRAN;

-- Step 3: (First Session)
Update hr.Employees
Set postalcode = N'10004' 
Where empid =1;

-- Step: not sure if it is executed before or after step 4 of the other session.
Commit tran;

Open in new window

Second Session:
-- Step 2: (Second Session)
use TSQL2012
SET TRANSACTION ISOLATED LEVEL UNCOMITTED

-- Step 4: (Second Session)
-- Add TRY/CATCH or IF/ ELSE to determine whether
-- the following select is comiited or still uncomitted
-- or perhaps it is rolled back.
IF @@TRANCOUNT <of First Session> >0
   SELECT 'Committed Result: ' As Status, lastname, firstname, region FROM HR.Employees
   --<commited result returned: region=1004 for empid=1>
ELSE
   SELECT 'Uncommitted Result: ' As Status, lastname, firstname, region FROM HR.Employees
   --<uncommited result returned: region=1004 for empid=1>

Open in new window

Note: I know that perhaps a better method will be to use RCSI or some other isolation method. Here the idea is to discover if there is a way to add the requested IF/Else using SET TRANSACTION ISOLATED LEVEL UNCOMITTED.

I also have the following few lines from the book. I have not made a good sense of it yet but I am including it FYI:
This step uses a table hint to implement READ UNCOMMITTED in a single command. Instead of applying this isolation level to an entire session, you can apply it at the individual command, per table, by using the READUNCOMMITTED table hint. Replace the SELECT command in the code for the previous step with the following, which has the WITH (READUNCOMMITTED) table hint. Then modify the statement to use WITH
(NOLOCK).
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HuaMin ChenProblem resolverCommented:
It means you can still read the uncommitted records, within one transaction.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If you execute the steps in sequence, it doesn't matter whether you execute the COMMIT prior to step 4 or not. No session will be blocked, and no sequence enforced. The second session will always see the change of session 1.
One of the reasons to use isolation levels is to prevent that. Usually the second session should not be able to read any change (or old values from the changed tables) until committed.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi Qlemo,

When we execute Step 4:
SELECT '??? Result: ' As Status, lastname, firstname, region FROM HR.Employees

The possibilities are:
1. The first session has already committed or rolled back. We want to know what replaces ??? portion to say 1) "rolled back, now is original committed", or 2) "committed, now is new committed".

2. The first session has not committed/ rolled back yet. We want to know about that and replace ??? portion to say "not committed or rolled back possibly a dirty read."

Note change at
 -- Step: not sure if it is executed before or after step 4 of the other session.
Commit tran;  
-- or
-- Rollback;
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
"The second session will always see the change of session 1."
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.