Skip to Main Content
  • Questions
  • How does Read only transactions work internally

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Sukumar.

Asked: April 09, 2002 - 5:51 pm UTC

Last updated: May 23, 2005 - 7:06 pm UTC

Version: 8.1.7

Viewed 1000+ times

You Asked

Hi,

In the following example on "Read Only transaction" testing on Oracle 8.1.7 (Windows NT 4.0 platform)

The table "testread" in this example
has a column teststring char(25)

SQLPLUS Session 1
=================

commit;
insert into testread values('testdata1')
commit;

SQLPLUS Session 2
=================

commit;
set transaction read only;
select * from testread;

Results are :

teststring
-----------

testdata1


SQLPLUS Session 1 continued
===========================
insert into testread values('testdata2');
commit;

SQLPLUS Session 2 continued
===========================

select * from testread;

Results are : (again it displays the same results since we have not committed yet for Session 2 to end the Read only transaction)

teststring
-----------

testdata1

SQLPLUS Session 3 (A brand new session to the same database)
=================
commit;
set transaction read only;
select * from testread;

Results are :

teststring
-----------

testdata1
testdata2

From this example, one can learn how Read Only isolation is neatly performed by Oracle.

The question to you is how does Oracle accomplish "Read Consistency" in the above scenarios.
i.e. Readonly transaction in Session 2 is different from Session 3. The row testdata2 for the second insert was seen only by Session 3
and not by Session 2, since it was started just after the first insert of row testdata1 (i.e. after the first insert was committed)

How does Oracle maintain consistency of the data for these 2 sessions in a consistent fashion until you end the "read only transaction" by committing it. i.e. Any number of selects in Session 1 before commit always returns "testdata1" and does not include "testdata2".

Is it from the REDO logs ?

What are the sideeffects of using this. Does the inserts into the datablocks take longer than usual, since there are multiple "read only transactions" that are reading them.


Please explain a little bit on the internals if you can

Thanks a lot

Sukumar Alagappan






and Tom said...

The concepts guide does this as good as I could:

</code> http://docs.oracle.com/cd/A87860_01/doc/server.817/a76965/c23cnsis.htm#17882 <code>

the concept guide is the BEST document with the LEAST number of people reading it for some reason. I read it myself with every major release. Invaluable.

We do this with the UNDO (not redo) information. We just roll back work.

The inserts take no longer, but as time goes on -- it may take the read only transaction longer and longer to roll back and eventually you may hit an ora-01555 (search this site for 1555 or 01555 for details)



Rating

  (9 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, April 09, 2002 - 9:46 pm UTC


This looks familiar....

Mark J. Bobak, April 09, 2002 - 10:57 pm UTC

Or you could just check Doc ID 338634.999 on MetaLink,
which is a forum thread where you asked the same question
and I answered it in detail...and also referred you
to the Concepts manual....;-)

-Mark


RE: This looks familiar...

Mark A. Williams, April 10, 2002 - 10:12 am UTC

Mark:

Surely you don't expect people to actually read the Oracle documentation. Unheard of! I mean, what else would Tom do?

Just look at today's questions... "svrmgrl is gone in 9i??? Gosh, what do I do???" Tom must be "ghost writing" a lot of applicationsl...

(Of course, this is somewhat tongue-in-cheek, as I make my fair share of mistakes, etc...)

- Mark

This looks familiar

Mark J. Bobak, April 10, 2002 - 11:38 am UTC

Yeah, we all do make our fair share of mistakes....

But I agree with Tom that the Concepts manual is the most
valuable and overlooked piece of documentation out there.


Concepts Guide Rules!

Debi Lorraine, September 03, 2002 - 7:34 pm UTC

A developer asked me an "obscure" question about how to achive "read consistency" throughout a session. We both thought of "asking Tom" but alas, Tom was not taking any questions at the time. A search of the archives and asking myself, "What would Tom Do?" (WWTD) led me to the concepts guide recommended here, and to the ultimate answer:

alter session set isolation_level = serializable;

Yes, I am convert to reading the concepts guide! Sorry, I have strayed ~somewhat~ from the topic...


Read-Only and Lock table

Yogesh Purabiya, May 21, 2005 - 1:26 pm UTC

Recently I was going through an old document (SQL Reference Language Manual of Oracle 7.3).

In the topic of "SET TRANSACTION" and the option of "READ ONLY", the manual gave (gives) a list of statements that can be given during a READ-ONLY-Transaction. It includes "LOCK TABLE" ! (It gave me a surprise)

I cannot give SELECT FOR UPDATE; but I can give LOCK TABLE !

( Well, I tried out and found as per the manual. )

I want to know what a user would get by LOCK TABLE during a READ-ONLY-Transaction.


Tom Kyte
May 21, 2005 - 2:20 pm UTC

didn't say they "would gain anything", just that the activity is not prohibited.

set transaction read only prevents you from modifying data -- lock table doesn't modify anything, hence it is not prevented.


READ ONLY Transaction & LOCK TABLE

Yogesh Purabiya, May 23, 2005 - 1:19 pm UTC

Then, READ ONLY blocks the WRITES.
Can we say READ blocks WRITES ?!

Tom Kyte
May 23, 2005 - 7:02 pm UTC

read only prevents the session that said "make me read only" from writing data.

You cannot in any way shape or form say "reads block writes" from this.


You can say "a lock table command may be used to prevent modifications to a table"

READ ONLY Transaction & LOCK TABLE

Yogesh Purabiya, May 23, 2005 - 1:21 pm UTC

SELECT FOR UPDATE also does not update, it only locks few / some / all rows.
But that is not allowed.
While, the whole table can be locked !

Tom Kyte
May 23, 2005 - 7:06 pm UTC

I'm not understanding your point at all.

And select for update actually does go out and modify things (blocks). It gets locks on the rows, and to do that you modify blocks because row locks which are only limited by the number of rows in your database are attributes of the data itself.

A table lock is on the other hand, not.

Lock table locks the table at the table level. It is a permitted command in a read only table. It modifies *nothing*

select for update however is not a permitted command in a read only transaction.


A read only transaction simply prevents modifications (unless you are SYS in which case all bets are off, no such thing as readonly for SYS). That is all. Select for update actually touches the rows, modifies them, by locking them.

Read Only & Table Lock

Yogesh Purabiya, May 24, 2005 - 10:58 am UTC

Thanks ...
Now it is pretty clear.