事务transaction:由1个或多个SQL组成的逻辑操作单位
开始于第1个可执行的SQL,结束于commit或rollback或DDL隐式地提交结束
事务的4要素:ACID
Atomicity原子性
Consistency一致性
Isolation隔离性
Durability持久性
Set transaction name…
transaction ID ,ITL记录对应的undo segment位置,放在block header里
V$transaction
System change number(SCN)
commit 同时擦出all savepoint,有个SCN,LGWR(redolog entry和SCN)
rollback 用到undo segment,释放所有的lock,savepoint
rollback to savepoint
savepoint in an uncommitted transaction
Main transaction autonomous transaction
同一事务中的一个或多个SQL语句同时更新分布式数据库不同节点的数据
two-phase commit
1.initiating node called global coordinator-----other node已做好准备commit
2.commit
in-doubt transaction不可信的分布式事务
每个本地的RECO会自动commit或rollback,以保证所有的节点一致
如果故障长时间的无法恢复,oracle允许DBA在本地手工地提交或撤销in-doubt transaction。如果其中一个database需要recover,其它节点的DBA也可以使用oracle的恢复功能到相同的时间点。
数据并发和一致性
Data concurrent:多个用户同时访问数据
Data consistency:指每个用户能取到具备一致性的数据,同时还能看到自己或其他用户提交的数据
串行化处理(serializability),实现数据一致性
Oracle:一致读查询 read-consistent queries
无阻塞查询 nonblocking queries
Statement-level read consistent 语句级的读一致性
Transaction-level read consistent事务级的读一致性
利用undo segment consistent read (CR) clones
SQL92标准定义的4种事务隔离级别(transaction isolation level)
未提交读read uncommitted
已提交读read committed
可重复读repeatable read
串行化realizable
dirty read nonrepeatable read phantom read不存在读
oracle的3种事务级别
read committed isolation level:只能看到查询执行前(不是事务开始前)已提交的数据
不会脏读
realizable isolation level:只能看到事务执行前已提交的数据
ORA-08177:cannot serialze access for this transaction
read-only isolation level:除了SYS,都不能修改数据
set transaction isolation level read committed
serializable
set transaction read only
ORA-1555:snapshot too old
锁机制:
exclusive lock排他锁
share lock共享锁
防止lost update(例)
lock conversion / lock escalation锁转换和锁升级
ORA-00060:deadlock while waiting for resource
锁的类型:oracle能自动地对事务中的资源锁定
s protect data
DDL locks protect the structure of schema objects
System locks protect internal database structures
DML lock:row lock(TX) 粒度最精细的锁
Oracle Database stores lock information in the data block that contains the locked row
table lock(TM):Row Share (RS)
Row Exclusive Table Lock (RX)
Share Table Lock (S)
Share Row Exclusive Table Lock (SRX)
Exclusive Table Lock (X)
DDL lock:Exclusive DDL Locks
Share DDL Locks
Breakable Parse Locks
System lock:保护数据库及内存结构的
Latch:保护SGA内的共享数据结构
Mutexes:·A mutex can reduce the possibility of contention
·A mutex consumes less memory than a latch.
·When in shared mode, a mutex permits concurrent reference by multiple sessions.
Internal Locks:Dictionary cache locks
File and log management locks
Tablespace and undo segment locks
1.更少的资源消耗,mutex与latch不同,它不是独立存在的,而是在每个内存结构中,并随着内存结构创建和释放,mutex同时也被创建和释放。mutex暂用的空间比latch小很多,创建和释放消耗更少的资源。
2.有效降低竞争,因为mutex是每个内存结构中的一部分,这样意味着mutex的数量可以有很多,而不同于latch,一个latch需要管理很多个内存结构,当你访问同一latch管理的不同内存结构时,也会发生竞争,而mutex则不会。另外,因为latch的数量有限,很多时候latch本身的竞争会很厉害,之前,我们只能增加latch数量或者减少latch持有的时间,而现在,mutex是一个更好的选择。
3.更快的pin,当block被访问时,它必须被pin在buffer cache中,当一个cursor执行时,它也必须被pin在library cache中,如果大量并发频繁执行同一个cursor,library cache pin会耗费大量的CPU资源。而mutex使用reference count来解决并发访问的问题,只要它大于零,就表示它已经被pin在了内存中,不能被交换出去。而且mutex.ref_count++这个操所是非常快的,只占用非常少的资源。
http://www.uml.org.cn/sjjm/201007283.asp
Manual Data Locks
·Applications require transaction-level read consistency or repeatable reads.
·Applications require that a transaction have exclusive access to a resource so that the transaction does not have to wait for other transactions to complete.
事务级
The SET TRANSACTION ISOLATION LEVEL statement
The LOCK TABLE statement
The SELECT ... FOR UPDATE statement
会话级
Alter session set transaction isolation level…
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24005010/viewspace-687961/ ,如需转载,请注明出处,否则将追究法律责任。