Home >> FAQs/Tutorials >> Oracle DBA FAQ >> Index

Oracle DBA FAQ - Understanding SQL Transaction Management

By: FYIcenter.com

Part:   1  2   3  4  5  6  7 

(Continued from previous part...)

How To Rollback the Current Transaction?

If you have used some DML statements updated some data objects, you find a problem with those updates, and you don't want those updates to be permanently recorded in the database, you can use the ROLLBACK statement. It will remove all the database changes made in the current transaction and end the current transaction. The following tutorial exercise shows you how to use ROLLBACK statements:

SQL> connect HR/fyicenter

SQL> INSERT INTO fyi_links (url, id) 
  2  VALUES ('google.com', 102);

SQL> INSERT INTO fyi_links (url, id) 
  3  VALUES ('myspace.com', 103);

SQL> SELECT * FROM fyi_links;
     ID URL              NOTES          COUNTS CREATED
------- ---------------- ---------- ---------- ---------
    101 fyicenter.com                          07-MAY-06
    110 centerfyi.com                          07-MAY-06
    102 google.com                             07-MAY-06
    103 myspace.com                            07-MAY-06

SQL> ROLLBACK;
Rollback complete.

SQL> SELECT * FROM fyi_links;
     ID URL              NOTES          COUNTS CREATED
------- ---------------- ---------- ---------- ---------
    101 fyicenter.com                          07-MAY-06
    110 centerfyi.com                          07-MAY-06

As you can see, the two new records inserted into the table were removed by the ROLLBACK statement.

What Happens to the Current Transaction If a DDL Statement Is Executed?

If a DDL statement is executed, the current transaction will be committed and ended. All the database changes made in the current transaction will become permanent. This is called an implicit commit by a DDL statement. The following tutorial exercise shows you that the CREATE TABLE statement forced the current transaction to be committed and ended. The subsequent ROLLBACK statement has no effects on the closed transaction.

SQL> connect HR/fyicenter

SQL> INSERT INTO fyi_links (url, id) 
  2  VALUES ('oracle.com', 112);
  
SQL> INSERT INTO fyi_links (url, id) 
  2  VALUES ('sql.com', 113);

SQL> CREATE TABLE fyi_temp AS (SELECT * FROM fyi_links);
Table created.

SQL> ROLLBACK;
Rollback complete.

SQL> SELECT * FROM fyi_links;
     ID URL              NOTES          COUNTS CREATED
------- ---------------- ---------- ---------- ---------
    101 fyicenter.com                          07-MAY-06
    110 centerfyi.com                          07-MAY-06
    112 oracle.com                             07-MAY-06
    113 sql.com                                07-MAY-06

What Happens to the Current Transaction If the Session Is Ended?

If a session is ended, the current transaction in that session will be committed and ended. All the database changes made in the current transaction will become permanent. This is called an implicit commit when session is ended. The following tutorial exercise shows you that the "disconnect" command forces the current transaction to be committed and ended. When the session is reconnected, you can see the changes made by the UPDATE statements.

SQL> connect HR/fyicenter

SQL> UPDATE fyi_links SET url = 'FYICENTER.COM'
  2  WHERE id = 101;
  
SQL> UPDATE fyi_links SET url = 'CENTERFYI.COM' 
  2  WHERE id = 110;

SQL> disconnect
SQL> connect HR/fyicenter

SQL> SELECT * FROM fyi_links;
     ID URL              NOTES          COUNTS CREATED
------- ---------------- ---------- ---------- ---------
    101 FYICENTER.COM                          07-MAY-06
    110 CENTERFYI.COM                          07-MAY-06
    112 oracle.com                             07-MAY-06
    113 sql.com                                07-MAY-06

What Happens to the Current Transaction If the Session Is Killed?

If a session is killed by the DBA, the current transaction in that session will be rolled back and ended. All the database changes made in the current transaction will be removed. This is called an implicit rollback when session is killed. The following tutorial exercise shows you that the DBA KILL SESSION command forces the current transaction to be rolled back with all the changes uncommitted.

SQL> connect HR/fyicenter

SQL> SELECT * FROM fyi_links;
     ID URL              NOTES          COUNTS CREATED
------- ---------------- ---------- ---------- ---------
    101 FYICENTER.COM                          07-MAY-06
    110 CENTERFYI.COM                          07-MAY-06
    112 oracle.com                             07-MAY-06
    113 sql.com                                07-MAY-06

SQL> DELETE FROM fyi_links where id = 112;
1 row deleted.

SQL> DELETE FROM fyi_links where id = 113;
1 row deleted.

SQL> SELECT * FROM fyi_links;
     ID URL              NOTES          COUNTS CREATED
------- ---------------- ---------- ---------- ---------
    101 FYICENTER.COM                          07-MAY-06
    110 CENTERFYI.COM                          07-MAY-06

(Continued on next part...)

Part:   1  2   3  4  5  6  7 


Selected Developer Jobs:

More...