Ix. Oracle Transactions

Source: Internet
Author: User
Tags savepoint sqlplus

First, what is a business
Transactions are used to guarantee the consistency of data, which consists of a set of related DML statements that are either all successful or fail altogether in the group's DML (data manipulation language, additions and deletions, no queries).
such as: online transfer is typical to use transactions to handle, to ensure the consistency of data.

Ii. Transactions and Locks
When performing transactional operations (DML statements), Oracle locks the table on which it is acting, preventing other users from modifying the structure of the table. This is very important for our users.

III. Submission of services
A transaction can be committed when executed with a commit statement. When a commit statement is executed, the transaction is confirmed to be changed and the transaction is ended. Delete the savepoint, release the lock, and when the commit statement is used to end the transaction, other sessions will be able to view the new data after the transaction has changed. The save point is for rollback. There is no limit to the number of save points.

Iv. Rolling back transactions
Before we introduce the rollback TRANSACTION, let's introduce the concept and function of the savepoint (savepoint). A savepoint is a point in a transaction. Used to cancel a partial transaction, and when the transaction is ended, all the savepoint-defined save points are automatically deleted. When rollback is executed, the specified savepoint can be rolled back to the specified point, where we draw a description.

v. Several important operations of the transaction
1. Set Save Point SavePoint a
2. Cancel partial transaction rollback to a
3. Cancel all transaction rollback
eg
Sql> SavePoint A; --Create Save point A
SavePoint created

sql> Delete from emp where empno=7782;
1 row deleted

Sql> SavePoint B; --Create Save to B
SavePoint created

sql> Delete from emp where empno=7934;
1 row deleted

Sql> SELECT * from emp where empno=7934; --Cannot query to empno for 7934 this record, because this record has been deleted
EMPNO ename JOB MGR hiredate SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------

sql> rollback to B; --To restore this record by keeping points
Rollback Complete

Sql> SELECT * from emp where empno=7934;
EMPNO ename JOB MGR hiredate SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7934 MILLER Clerk 7782 1982/1/23 1300.00 10

Sql> SELECT * from emp where empno=7782; --Cannot query to empno for 7982 this record, because this record has been deleted
EMPNO ename JOB MGR hiredate SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------

sql> rollback to A; --To restore this record by keeping points
Rollback Complete

Sql> SELECT * from emp where empno=7782;
EMPNO ename JOB MGR hiredate SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7782 CLARK MANAGER 7839 1981/6/9 2450.00 10

Sql>
Note: This rollback transaction must not be used before commit, and if the transaction is committed, it will be useless no matter how many save points you have just made. If you do not perform a commit manually, but exit, it is automatically committed.
eg
Sql> SavePoint A;
SavePoint created

sql> Delete from emp where empno=7782;
1 row deleted

Sql> commit;
Commit Complete

sql> rollback to A;
Rollback to a
ORA-01086: Never created save point ' A '
Sql>

Vi. How transactions are used in Java programs
In Java operations database, in order to ensure data consistency, such as account operation (1) from one account to reduce 10$ (2) in another account to add 10$, we see how to use the transaction?

Java code

Package junit.test;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.sql.Statement;

public class Transationtest {

public static void Main (string[] args) {

Connection conn = null;
try {
1. Load Driver
Class.forName ("Oracle.jdbc.driver.OracleDriver");
2. Get Connected
conn = Drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:orcl", "Scott", "Oracle");
Statement sm = conn.createstatement ();
Subtract 100 from Scott's Sal.
Sm.executeupdate ("Update emp set sal=sal-100 where ename= ' SCOTT '");
int i = 7/0; Reported java.lang.ArithmeticException:/by Zero exception
Add 100 to Smith's Sal.
Sm.executeupdate ("Update emp set sal=sal+100 where ename= ' SMITH '");
Close an open resource
Sm.close ();
Conn.close ();
} catch (Exception e) {
If an exception occurs, roll back
try {
Conn.rollback ();
} catch (SQLException E1) {
E1.printstacktrace ();
}
E.printstacktrace ();
}

}

}

Run, there will be exceptions, look at the database, SCOTT's Sal minus 100, but Smith's Sal is not changed, very scary ...
How can we ensure that both operations succeed at the same time or fail at the same time?


Java Code

Package junit.test;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.sql.Statement;

public class Transationtest {

public static void Main (string[] args) {

Connection conn = null;
try {
1. Load Driver
Class.forName ("Oracle.jdbc.driver.OracleDriver");
2. Get Connected
conn = Drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:orcl", "Scott", "Oracle");
Join transaction Processing
Conn.setautocommit (false);//setting cannot be submitted by default
Statement sm = conn.createstatement ();
Subtract 100 from Scott's Sal.
Sm.executeupdate ("Update emp set sal=sal-100 where ename= ' SCOTT '");
int i = 7/0;
Add 100 to Smith's Sal.
Sm.executeupdate ("Update emp set sal=sal+100 where ename= ' SMITH '");
Commit a transaction
Conn.commit ();
Close an open resource
Sm.close ();
Conn.close ();
} catch (Exception e) {
If an exception occurs, roll back
try {
Conn.rollback ();
} catch (SQLException E1) {
E1.printstacktrace ();
}
E.printstacktrace ();
}

}

}

Run again, there will be exceptions, view the database, the data has not changed.

Vii. read-Only transactions
Read-only transactions are operations that allow only queries to be performed, not transactions that perform any other DML operations, and use read-only transactions to ensure that users can only get data at a point in time.
Assume that the ticket sales start at 18 o'clock every day to count today's sale, you can use a read-only transaction. After a read-only transaction has been set, the read-only transaction will not have the latest data changes, although the other sessions may commit new transactions, thus guaranteeing the acquisition of data information at a specific point in time.
Set read-only transaction: set transaction read only;

For example, there are two user system, Scott each with Sqlplus Landing, operation as follows:
The first step: Use the System user login Sqlplus, set the read-only transaction.
Sql> set transaction Read only;
Transaction set.

The second step: using Scott User Login Sqlplus, the operation is as follows:
Sql> Select COUNT (*) from EMP; --Query the total number of records for the EMP table
COUNT (*)
----------
13

sql> INSERT INTO EMP values (7777, ' Zhangsan ', ' MANAGER ', 7782, to_date (' 1988-02-18 ', ' yyyy-mm-dd '), 38.38, 45.45, 10); --Inserting a record into the EMP table
1 row inserted

Sql> Select COUNT (*) from EMP; --Query the total number of records for the EMP table
COUNT (*)
----------
14

Sql> commit; --Submit
Commit Complete

Step three: Query the Scott.emp table with the system user
Sql> Select COUNT (*) from scott.emp;
COUNT (*)
----------
13
Sql>

Ix. Oracle Transactions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.