Transaction

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

(SQL)

Transaction Processing
ORACLE TRANSACTIONS
A transaction begins with the first executable SQL statement after a
commit, rollback or connection made to the Oracle engine. All
changes made to an Oracle table data via unit a transaction are made or
undone at one instance.

Specifically a transaction is a group of events that occurs


between any of the following . events
 Connecting to Oracle.
 Disconnecting from Oracle.
 Committing changes to the database table
 Rollback.

Closing Transactions:
A transaction can be closed by using either a commit or
a rollback statement. By using these statements, table
data can be changed or all the changes made to the table
data undone.
Transaction Processing
• A Transaction is a logical unit of work on the database.
• A Transaction may be an entire program, a portion of a
program or even a single statement.
• A Transaction brings the database from one consistent
(correct/acceptable) state to another consistent state.
• Consistent state is one in which all data integrity constraints
are satisfied.
• All transactions are controlled and executed by the DBMS to
guarantee database integrity.
• While a transaction is in progress, the database may
temporarily be in some inconsistent state.
Transaction Processing
• Consider the example of a banking transaction involving two
relations:
1. ACC_TRANS(Acc_No, Amount, Date, Deposit/Withdraw,
……….)
2. ACC_BAL(Acc-No, Balance,………)
• Against every insertion in ACC_TRANS, the ACC_BAL must be
updated appropriately, otherwise the database will be in
some inconsistent or wrong state.
Example Transaction

• Register credit sale of 100 units of product


X to customer Y for $500
– Reduce product X’s quantity by 100
– Adding $500 to customer Y’s account
receivable
UPDATE PRODUCT
SET PROD_QTY = PROD_QTY - 100
WHERE PROD_CODE = ‘X’;
UPDATE ACCT_RECEIVABLE
SET ACCT_BALANCE = ACCT_BALANCE + 500
WHERE ACCT_NUM = ‘Y’;

5
Transaction Processing
• It concludes that
1. A Successful completion of a transaction will bring the
database into a new consistent state.
2. An incomplete transaction will leave the database in some
inconsistent state.
• An aborted transaction must be undone or rolled backed to
keep the database in some consistent state.
• A System component called Transaction Manager or
Recovery Manager supports the above feature.
Using COMMIT:
A COMMIT ends the current transaction and makes permanent any
changes made during the transaction. All transactional locks acquired
on tables are released.
Syntax: COMMIT;

Using ROLLBACK:
ROLLBACK does exactly the opposite of COMMIT. It ends the
transaction but .:does any changes made during the transaction. All
transactional locks acquired on tables are released.
syntax
ROLLBACK [WORK) [TO [SAVEPOINT] savepoint]
Where:
WORK : is optional and is provided for ANSI compatibility.
SAVEPOINT :is optional and is used to rollback a partial transaction, as
far as the specified savepoint.
savepoint : is a savepoint created during the current transaction.
Creating SAVEPOINT:
SAVEPOIIVT marks and saves the current point in the processing of a
transaction. When a SAVEPOINT is used with a ROLLBACK statement,
parts of a transaction can be undone. An active savepoint is one that is
specified since the last COMMIT or ROLLBACK.
Syntax : SAVEPOINT savepointname;

ROLLBACK can be fired from the SQL prompt with or without the SAVEPOINT
clause. The implication of each is described below:
A ROLLBACK operation performed without the SAVEPOINT clause amounts to
the following:
 Ends the transaction.
 Undoes all the changes in the current transaction.
 Erases all savepoints in that transaction.
 Releases the transactional locks.
A ROLLBACK operation performed with the TO SAVEPOINT clause amounts to
the following:
 A predetermined portion of the transaction is rolled back.
 Retains the save point roiled back to, but loses those created after the named
savepoint.
 Releases all transactional locks that were acquired since the savepoint was taken.
COMMIT, ROLLBACK and SAVEPOINT
– Initiate a transaction with the first DML command
to follow a COMMIT or ROLLBACK.
– Use COMMIT and ROLLBACK SQL statements to
terminate a transaction explicitly.
– ROLLBACK [TO SVAEPOINT savepoint]
– SVAEPOINT savepoint.
– SVAEPOINT gives more control on transaction
management.
– SVAEPOINT marks and saves the current point in a
transaction.
– There can be maximum 5 savepoints in a
transaction.
SAVEPOINT
Time
Transaction t1 SAVEPOINT s1;
---------- (a)
---------- (b)
t2 SAVEPOINT s2;
---------- (c)
---------- (d)
t3 SAVEPOINT s3;
---------- (e)
----------(f)
t4 ROLLBACK TO s2;
---------- (g)
---------- (h)
t5 COMMIT; // work done since the start of the
transaction plus (g) and (h) will be saved.
• A SAVEPOINT is a marker within a transaction
that allows for a partial rollback. As changes
are made in a transaction, we can create
SAVEPOINTs to mark different points within
the transaction. If we encounter an error, we
can rollback to a SAVEPOINT or all the way
back to the beginning of the transaction.
Example

• SQL> INSERT INTO AUTHOR VALUES ('A11l', 'john', 'garmany', '123-345-4567', 4 '1234 here st',
'denver', 'CO','90204', '9999');

1 row created.

SQL> savepoint in_author;

Savepoint created.

SQL> INSERT INTO BOOK_AUTHOR VALUES ('A111', 'B130', .20);


1 row created.

SQL> savepoint in_book_author;

Savepoint created.

• SQL> INSE R T INTO BOOK VALUES ('B130', 'P002', 'easy oracle sql', 'miscellaneous', 9.95, 1000, 15, 0, '', to_date ('02-20-
2005','MM-DD-YYYY'));
1 row created.

SQL> rollback to in_author;

Rollback complete.

• In the example above, I inserted a row into the AUTHOR table and created a SAVEPOINT called in_author. Next, I inserted a
row into the book_author table and created another SAVEPOINT called in_book_author. Finally, I inserted a row in the BOOK
table. I then issued a ROLLBACK to in_author.
13
Transaction Properties

14
Transaction Properties

15
Transaction Properties

16
Thank you for your attention.

Mr.Santosh Sutar
Assistant Professor

SICS(M.C.A),Korti,Pandharpur

Mob: 9850021283

E-mail: [email protected]

You might also like