search
HomeDatabaseMysql TutorialOracle事务 行级锁 保存点 回滚 提交

Oracle事务 行级锁 保存点 回滚 提交,这里注意一点,这里所说有A与B并不是指二个ORACLE帐户,而是二个连接会话。上面只是为了方

Oracle事务

一般事务(DML)即数据修改(增、删、改)的事务
事务会将所有在事务中被修改的数据行加上锁(行级锁),来阻止其它人(会话)同时对这些数据的修改操作。
当事务被提交或回滚后,这些数据才会被释放锁。

举个例子:
当A操作一条数据N1后,暂未提交事务 ,此时B又上来操作同一条数据N1,这时的情况是:
1、所有除A以外的人看不到被A所修改后的数据
2、B会处于等待状态,直到A提交或回滚了针对这条数据的修改(这也就是行级锁的概念)
3、当A提交事务后,所有人可以看到被A修改后的数据,看不到B修改后的数据。但B能看到自己修改后的数据(与A一样,因为B还未提交事务)。
4、当B提交事务后,所有人可以看到被B修改后的数据。

由上可以看出,对同一条数据的事务处理,必须按先后顺序进行。
即:A先提交了事务后,,B才可以提交事务,否则B将无法对这条数据进行修改(B会在操作这条数据时处于等待状态,直到A提交或回滚了事务,释放了这行数据的锁)。

这里注意一点,这里所说有A与B并不是指二个ORACLE帐户,而是二个连接会话。上面只是为了方便举例。
就是说即使是在同一台电脑上用同一个ORACLE帐户,但同时分别打开了二个连接会话(比如同时打开二个PL/SQL并用相同帐户连接到同一个库中),就会被视为A、B两个人。

在PS/SQL中,对数据修改完后,如果用户未提交事务,但关闭或断开了PS/SQL,此时ORACLE会立即提交此会话的事务。

我们可以事务中使用保存点来回滚到指定的时间节点上来,但如果用提交事务(commit)后,所有保存点将被删除。

举个举子:

savepoint a1;    --设置一个保存点 a1;

update tmp set username='张三' where userid='101'  --修改一条数据

savepoint a2;   --设置一个保存点 a1;

update tmp set username='李四' where userid='102  --再修改一条数据

 

rollback to a2;   --回滚到a2 保存点。 此时在 a2 保存点之后的所有数据修改视为无效。

rollback to a1;   --这里可以再从a2保存点再回滚到a1 保存点。 此时在 a1 保存点之后的所有数据修改视为无效。

rollback;           --回滚全部。即撤消至上一次提交事务后的所有数据修改。

commit;  --提交事务 将回滚后的事务提交,并会删除所有保存点。

注意:我们可以从a2向前再回滚到a1 ,但无法从a1回滚到a2。也就是只能向前回滚,不能从前面的点再向后回滚! 

只读事务 即,只用于select 查询的事务 

只读事务主要用于在某一个时间点对数据的查询统计。如:在18:00创建一个只读事务,然后可以开始统计某张表的记录数。此时18:00之后新添加到这张表中的数据就不会进入到统计中去。也就是创建只读事务的会话连接人,将无法看到在创建只读事务之后其它任何人对数据的真实修改。

方法是:

set transaction read only;  --在此之后,拥有此会话的连接人将无法看到其它人对数据的任何改动。用户可以用select 进行此时间点的统计查询

set transaction read write;  --取消只读事务

只读事务没有回滚和提交的功能也不记录回滚LOG。

只读事务最大的用处是保证在某个时间点查询结果的一致性。换句话说,如果你要统计很多信息,有多个select查询时,但你执行完第一个查询后(如用户信息统计),可能其它人进来修改了某些数据你正在查询的数据。这时你再进行第二个查询(如用户信息的统计或其它有关用户信息的查询)。这时候将出现数据查询结果的不一致,第二次查询的结果会由于数据被修改了而与第一次查询结果一不样。只读事务正是解决这类问题的办法。

其实ORACLE为了优化查询在对于单条查询时也会启动只读事务(就是在你开始执行查询时会有一个只读事务点,到查询结束前,在这期间的数据修改都被无视掉),你无须手动创建只读事务。但多条查询时我们就必须手动创建只读事务。

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL: Not a Programming Language, But...MySQL: Not a Programming Language, But...Apr 13, 2025 am 12:03 AM

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools