Skip to Main Content
  • Questions
  • DDL Execution - Autocommit Internals

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Raghavendra.

Asked: December 08, 2014 - 1:41 pm UTC

Last updated: December 09, 2014 - 3:05 pm UTC

Version: 11.2

Viewed 1000+ times

You Asked

Hi Tom,

As we all know, one of the Oracle internal feature of DDL execution is that it does an autocommit. This is fine when the DDL is successful. If the DDL executions is a failure, why is that oracle still auto-commits the session, when the very purpose of autocommit is defeated. As read in your book (Effective Oracle by Design), a DDL statement is equivalent to

BEGIN
COMMIT;
EXECUTE_DDL;
COMMIT;
EXCEPTION WHEN OTHERS THEN
COMMIT;
END;


--SESSION s1;

CREATE TABLE t1 (x number)
/
INSERT INTO t1 VALUES (1)
/
CREATE TABLE t1 (x number) --This DDL is a failure as object already exists, but it still auto commits

--SESSION s2;

SELECT * FROM t1;
/
x
--
1

I think if the VERY reason of executing of DDL is defeated , then why still autocommit should happen? If the application is trying to create to a new table ,
which does not require any locks to be released OR which has no impact on the
previous processing prior to DDL execution, why still autocommit is required?

We know that before DDL execution, COMMIT IS mandatory for system to make the previous changes permanent and release the locks. If the DDL
execute is trying to crate a new object, why still commit is required before DDL's
are executed. Can we not do like the below for new objects creation?

BEGIN
SAVEPOINT S1;
EXECUTE_DDL;
COMMIT;
EXCEPTION WHEN OTHERS THEN
ROLLBACK TO S1;
END;

Thanks,
Boralli

and Tom said...


Begin
   Commit;
   Parse the DDL – verify privileges and syntax
   Begin
      Do_the_ddl;
      Commit;
   Exception
      When others then
         Rollback;
   End;
End;


Note the rollback - not a commit in there!


The DDL works in its own transaction - it will commit the outstanding work and then do its stuff - and either commits all of its stuff or it rolls back all of uts stuff.


The commit happens before the DDL even executes, so in answer to:

I think if the VERY reason of executing of DDL is defeated , then why still autocommit should happen?

is that the commit already happened before we know if the DDL will succeed or fail. *it already took place*



f the application is trying to create to a new table ,

which does not require any locks to be released OR which has no impact on the

previous processing prior to DDL execution, why still autocommit is required?


because if the create table fails, we need to UNDO it. We need to rollback. And we don't want to rollback your work - so the design decision was made that DDL commits.


The savepoint in theory would/could wok - technically speaking - HOWEVER - DDL is a very serial operation, most dictionary operations are. If we used a savepoint - we would not be able to "publish" that ddl - we'd be stuck in limbo. The table would be "created" but the current session would not be able to see it really (none of our recursive sql would be able to see it - they run in a separate transaction - it would not be committed). No other session would be able to see it. Furthermore - the dictionary would be inaccessible for other sessions attempting to do dictionary things (add a new partition, whatever...)


A savepoint might technically work - but it would introduce locking issues you don't really want and give you DDL that you cannot use until you commit anyway.

If you don't want the auto-commit, you can use an autonomous transaction...

Rating

  (1 rating)

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

Comments

what is the use of rollback in exception

Raghavendra boralli, December 08, 2014 - 5:47 pm UTC

Hi tom,

I had mentioned by mistake, the commit in exception block.
But after seeing rollback, I think since work is already committed before executing ddl, and parsing of ddl should've created any work, commit or rollback statements will do mainly release of locks. I am just thinking here, if we have a new TCL just to release the locks and use save point to rollback, will that system not to autocommit if DDL execution has failed?


Thanks
Boralli
Tom Kyte
December 09, 2014 - 3:05 pm UTC

the DDL is like a stored procedure - it does hundreds if not thousands of SQL operations.


DDL is done like this

a) commit - commit everything - start with a clean transaction
b) execute the DDL - this is hundreds of sql statements
c) commit or rollback depending on the success or failure


that is all. Don't over analyze it - it is just like a stored procedure that either commits all of its work or rolls everything back. that is all...