Oracle事务 行级锁 保存点 回滚 提交
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为了优化查询在对于单条查询时也会启动只读事务(就是在你开始执行查询时会有一个只读事务点,到查询结束前,在这期间的数据修改都被无视掉),你无须手动创建只读事务。但多条查询时我们就必须手动创建只读事务。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.
