Home » SQL & PL/SQL » SQL & PL/SQL » Understanding locking (12c)
Understanding locking [message #648891] Sun, 06 March 2016 16:38 Go to next message
gorants
Messages: 85
Registered: May 2014
Location: ATL
Member
Hello experts,

I was reading through online resources to understand locking mechanism. i didnt get the high lighted lines below, why U2 is rollbacked here ? i expected after U2 commit U3 will commit. I have attached PNG will which will explains the below. please help me understand this.

In this case, we have 3 transactions that are all attempting to make changes to a single row in Table A. U1 obtains an exclusive lock on this table when issuing the first update statement. Subsequently, U2 attempts to update the same row and is blocked by U1's lock. U3 also attempts to manipulate this same row, this time with a delete statement, and that is also blocked by U1's lock.

When U1 commits its transaction, it releases the lock and U2's update statement is allowed to complete. In the process, U2 obtains an exclusive lock and U3 continues to block. Only when U2's transaction is rolled back does the U3's delete statement complete.
/forum/fa/13058/0/


[mod-edit: imagine inserted into messaged body by bb]




  • Attachment: Capture.PNG
    (Size: 27.97KB, Downloaded 1434 times)

[Updated on: Sun, 06 March 2016 20:50] by Moderator

Report message to a moderator

Re: Understanding locking [message #648892 is a reply to message #648891] Sun, 06 March 2016 17:16 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
You are looking for things that are not there. U2 just happens to rollback. Same way U2 could have committed. It doesn't matter rollback/commit - all that matters is U2 completed transaction which in turn released the lock and then and only then U3 can delete the row.

SY.

[Updated on: Sun, 06 March 2016 17:17]

Report message to a moderator

Re: Understanding locking [message #648893 is a reply to message #648892] Sun, 06 March 2016 20:31 Go to previous messageGo to next message
gorants
Messages: 85
Registered: May 2014
Location: ATL
Member
Thanks for the reply. If i understand correctly at that instance U2 could have either committed or rollback, which does lock release hence U3 will execute. -- is this correct?

Incase of exceptions only U2 will rollback and it is not because of locking?
Re: Understanding locking [message #648902 is a reply to message #648893] Mon, 07 March 2016 00:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Commit and rollback release the locks.
Some points:

Quote:
U1 obtains an exclusive lock on this table when issuing the first update statement.


No, U1 gets an exclusive lock on the table ROW not on the table.

Quote:
Only when U2's transaction is rolled back does the U3's delete statement complete.


U2 can complete when the lock is released, it does not need that the whole U2 transaction rollback to be completed.
Re: Understanding locking [message #648903 is a reply to message #648902] Mon, 07 March 2016 01:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note that depending on what does U1 update, U3 may or not delete a row.

[Updated on: Mon, 07 March 2016 02:07]

Report message to a moderator

Re: Understanding locking [message #648904 is a reply to message #648891] Mon, 07 March 2016 01:58 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
What is the document you are studying? The reason I ask is is that there is no "start transaction" in Oracle SQL. Perhaps your doc is describing how rings work in MySQL, or something else.
Re: Understanding locking [message #648915 is a reply to message #648904] Mon, 07 March 2016 07:09 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Well, there is no physical "start transaction" command however there is transaction definition:

Beginning of a Transaction

A transaction begins when the first executable SQL statement is encountered.
An executable SQL statement is a SQL statement that generates calls to a database instance, including DML and DDL statements and the SET TRANSACTION statement.

And you can find "Transaction begins" in many diagrams within Oracle documentation just to tell readers there are no pending uncommitted changes.

SY.
Re: Understanding locking [message #648916 is a reply to message #648915] Mon, 07 March 2016 07:31 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
Yes, but I think Oracle breaks the rules: it doesn't count a SELECT as starting a transaction, though (I think) ANSI SQL does. Or to put it another way, Oracle's READ COMMITTED isolation level doesn't conform to the REPEATABLE READ standard.

I suppose one could say that SET TRANSACTION is the equivalent of START TRANSACTION, but how many people ever use that?
Re: Understanding locking [message #648921 is a reply to message #648916] Mon, 07 March 2016 08:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Oracle's READ COMMITTED isolation level doesn't conform to the REPEATABLE READ standard.


Of course as READ COMMITTED and REPEATABLE READ are 2 different levels of SQL standard which has 4 levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ and SERIALIZABLE.
Oracle supports only the second and the last ones (plus a READ-ONLY level which is not in the standard).

Re: Understanding locking [message #648931 is a reply to message #648916] Mon, 07 March 2016 16:14 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
John Watson wrote on Mon, 07 March 2016 08:31
Yes, but I think Oracle breaks the rules: it doesn't count a SELECT as starting a transaction, though (I think) ANSI SQL does. Or to put it another way, Oracle's READ COMMITTED isolation level doesn't conform to the REPEATABLE READ standard.

I suppose one could say that SET TRANSACTION is the equivalent of START TRANSACTION, but how many people ever use that?


Not completely true. Readers never block writers, except if the select is done with a FOR UPDATE on the select. Then it will block writers.
Re: Understanding locking [message #648966 is a reply to message #648903] Tue, 08 March 2016 21:58 Go to previous messageGo to next message
gorants
Messages: 85
Registered: May 2014
Location: ATL
Member
Thanks for the details .. As mentioned in the table diagram , rollback happen if there is an exceptions not because of locking -- is my understanding is correct?
Re: Understanding locking [message #648969 is a reply to message #648966] Wed, 09 March 2016 00:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
rollback happen if there is an exceptions not because of locking


What does this mean?

Re: Understanding locking [message #648971 is a reply to message #648966] Wed, 09 March 2016 00:57 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
gorants wrote on Wed, 09 March 2016 03:58
Thanks for the details .. As mentioned in the table diagram , rollback happen if there is an exceptions not because of locking -- is my understanding is correct?

If I understand this correctly, the answer is that a statement (not the transaction) will be automatically rolled back if it raises an error.
Re: Understanding locking [message #648992 is a reply to message #648971] Wed, 09 March 2016 09:38 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Your understanding is incorrect. A rollback ONLY occurs if a rollback command is given or the user terminates the session. And terminating the session will commit or rollback depending on the settings in the application. If you want to commit, issue a COMMIT command. If you want to rollback, issue a ROLLBACK command.
Re: Understanding locking [message #649005 is a reply to message #648992] Wed, 09 March 2016 11:42 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
Hey? If a statement hits an error, it gets rolled back. Automatically.
Re: Understanding locking [message #649006 is a reply to message #649005] Wed, 09 March 2016 11:43 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
If it hits an error, it never gets fired. But if you have 10 statements that are executed and it fails on the 11th, the preceding 10 are NOT rolled back.
Re: Understanding locking [message #649008 is a reply to message #649006] Wed, 09 March 2016 11:47 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
No, no. Consider a statement that updates a zillion rows, then hits a constraint error. The whole statement gets rolled back.
Re: Understanding locking [message #649009 is a reply to message #649008] Wed, 09 March 2016 11:49 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Yes, I agree. However he said that he assumed that an error rolled back everything. It only rolls back the one statement. That was what I was trying to say. If it was not clear, sorry.
Re: Understanding locking [message #649010 is a reply to message #649009] Wed, 09 March 2016 11:55 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
It only rolls back the one statement.


Unless they are in a PL/SQL anonymous block or stored objects (unless the exception is trapped). Smile


[Updated on: Wed, 09 March 2016 11:57]

Report message to a moderator

Previous Topic: Outer query
Next Topic: Grouping Query
Goto Forum:
  


Current Time: Fri Apr 19 21:22:42 CDT 2024