【oracle 10g R2 新特性】Asynchronous Commit

victorymoshui發表於2011-12-16

對於Oracle的commit機制,相信大家都已經非常的熟悉了。Oracle對於commit採取了同步寫事務log的方式,也就是說,一旦發出commit命令,那麼必須等待事務相關的所有redo都已經從log buffer寫出到redo logfile以後,才會控制權返回發出commit的程式。


同步commit有兩個特點:

1.Immediate,發出commit命令後,立即將redo寫出

2.Wait,在redo寫出過程中,必須等待,等待事物相關的所有redo都已經從log buffer寫出到redo logfile中。

但是,Oracle10gR2開始,一種新的commit機制被引入,這就是非同步commit機制。也就是說,不必等到事務相關redo寫出就可以返回了,非同步commit也有兩種特點:

1.Nowait,發出commit命令後,不管redo是否寫出,立即返回控制權

2.Batch,redo的寫出可以想buffer一樣執行批量寫出,以提高效能

當然,同步commit也可以batch,非同步commit也可以將redo立即寫出,所以上述四個特性可以自由組合。

 

   Initialization Parameter and COMMIT Options for Managing Commit Redo

Option Specifies that . . .

WAIT

The commit does not return as successful until the redo corresponding to the commit is persisted in the online redo logs (default).

NOWAIT

The commit should return to the application without waiting for the redo to be written to the online redo logs.

IMMEDIATE

The log writer process should write the redo for the commit immediately (default). In other words, this option forces a disk I/O.

BATCH

Oracle Database should buffer the redo. The log writer process is permitted to write the redo to disk in its own time.


這樣,commit引入了新的語法:

COMMIT [WRITE [IMMEDIATE | BATCH] [WAIT | NOWAIT] ]

預設情況下,commit的機制和以前一樣,也就是相當於

COMMIT WRITE IMMEDIATE WAIT;

當然,這個預設機制可以通過初始化引數修改,所以Oracle10gR2又引入了一個新的引數COMMIT_WRITE,可能的取值包括

COMMIT_WRITE='{ IMMEDIATE | BATCH } , { WAIT | NOWAIT }'
 
 

可以在初始化引數檔案中設定commit機制為BATCH和NOWAIT

例:COMMIT_WRITE = BATCH, NOWAIT

也可以使用alter system在系統級別設定commit機制為BATCH和NOWAIT

例:ALTER SYSTEM SET COMMIT_WRITE = BATCH, NOWAIT

 
如果直接使用commit語句如下,即在commit後跟子句,可以忽略掉當前在初始化引數檔案中設定的commit機制,而使用當前設定的新機制。
 
例:COMMIT WRITE BATCH NOWAIT
注意:在分散式事務中,不可以修改IMMEDIATEWAIT 引數。

 

Note that the specification of the NOWAIT and BATCH options allows a small window of vulnerability in which Oracle Database can roll back a transaction that your application view as committed. Your application must be able to tolerate the following scenarios:

  • The database host crashes, which causes the database to lose redo that was buffered but not yet written to the online redo logs.

  • A file I/O problem prevents log writer from writing buffered redo to disk. If the redo logs are not multiplexed, then the commit is lost.


非同步commit由於不能確保事務的redo已經寫出到redo logfile當中,一旦例項崩潰,可能導致已經commit的事務無法恢復,使用該特性的時候需要慎重考慮。

其他檔案:

oracle分散式事務

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9390331/viewspace-713464/,如需轉載,請註明出處,否則將追究法律責任。

相關文章