http://forum.javaeye.com/viewtopic.php?t=1603
但是并沒(méi)有得出明確的結論。先讓我們看看事務(wù)的定義:
引用:
Transactions are described in terms of ACID properties, which are as follows:
n Atomic: all changes to the database made in a transaction are rolled back if any
change fails.
n Consistent: the effects of a transaction take the database from one consistent
state to another consistent state.
n Isolated: the intermediate steps in a transaction are not visible to other users of
the database.
n Durable: when a transaction is completed (committed or rolled back), its effects
persist in the database.
即ACID的定義,從上面看來(lái),似乎除了isolated之外,和只讀查詢(xún)都沒(méi)有關(guān)系。那么是否只讀查詢(xún)不需要事務(wù)呢?
再看看Oracle對于只讀事務(wù)的定義:
引用:
Read-Only Transactions
By default, Oracle guarantees statement-level read consistency. The set of data returned by a single query is consistent with respect to a single point in time. However, in some situations, you might also require transaction-level read consistency. This is the ability to run multiple queries within a single transaction, all of which are read-consistent with respect to the same point in time, so that queries in this transaction do not see the effects of intervening committed transactions.
If you want to run a number of queries against multiple tables and if you are not doing any updating, you prefer a read-only transaction. After indicating that your transaction is read-only, you can run as many queries as you like against any table, knowing that the results of each query are consistent with respect to the same point in time.
Oracle默認情況下保證了SQL語(yǔ)句級別的讀一致性,即在該條SQL語(yǔ)句執行期間,它只會(huì )看到執行前點(diǎn)的數據狀態(tài),而不會(huì )看到執行期間數據被其他SQL改變的狀態(tài)。
而Oracle的只讀查詢(xún)(read-only transaction)則保證了事務(wù)級別的讀一致性,即在該事務(wù)范圍內執行的多條SQL都只會(huì )看到執行前點(diǎn)的數據狀態(tài),而不會(huì )看到事務(wù)期間的任何被其他SQL改變的狀態(tài)。
因此我們可以得出結論:
如果你一次執行單條查詢(xún)語(yǔ)句,則沒(méi)有必要啟用事務(wù)支持,數據庫默認支持SQL執行期間的讀一致性;
如果你一次執行多條查詢(xún)語(yǔ)句,例如統計查詢(xún),報表查詢(xún),在這種場(chǎng)景下,多條查詢(xún)SQL必須保證整體的讀一致性,否則,在前條SQL查詢(xún)之后,后條SQL查詢(xún)之前,數據被其他用戶(hù)改變,則該次整體的統計查詢(xún)將會(huì )出現讀數據不一致的狀態(tài),此時(shí),應該啟用事務(wù)支持。
只讀事務(wù)與讀寫(xiě)事務(wù)區別
對于只讀查詢(xún),可以指定事務(wù)類(lèi)型為readonly,即只讀事務(wù)。由于只讀事務(wù)不存在數據的修改,因此數據庫將會(huì )為只讀事務(wù)提供一些優(yōu)化手段,例如Oracle對于只讀事務(wù),不啟動(dòng)回滾段,不記錄回滾log。
在JDBC中,指定只讀事務(wù)的辦法為:
connection.setReadOnly(true);
在Hibernate中,指定只讀事務(wù)的辦法為:
session.setFlushMode(FlushMode.NEVER);
此時(shí),Hibernate也會(huì )為只讀事務(wù)提供Session方面的一些優(yōu)化手段
在Spring的Hibernate封裝中,指定只讀事務(wù)的辦法為:
bean配置文件中,prop屬性增加“readOnly”
我在MySQL4.1試驗了一下,過(guò)程和結果如下:
數據庫:MySQL4.1
表類(lèi)型:InnoDB
Spring:1.1.2
Hibernate:2.1.7
使用Spring的聲明式事務(wù)管理
試驗過(guò)程如下:
不設置查詢(xún)方法的事務(wù)類(lèi)型(即不需要事務(wù)):訪(fǎng)問(wèn)查詢(xún)頁(yè)面,后臺執行Spring的Bean方法,讓Hibernate發(fā)送select語(yǔ)句,然后手工在MySQL里面修改該記錄某字段值,再訪(fǎng)問(wèn)查詢(xún)頁(yè)面,發(fā)現被修改過(guò)的字段值并沒(méi)有變化,Hibernate輸出的log顯示,數據庫還是把老的字段值返回,而沒(méi)有返回新的字段值。
設置查詢(xún)方法的事務(wù)類(lèi)型(只讀事務(wù)):訪(fǎng)問(wèn)查詢(xún)頁(yè)面,后臺執行Spring的Bean方法,讓Hibernate發(fā)送select語(yǔ)句,然后手工在MySQL里面修改該記錄某字段值,再訪(fǎng)問(wèn)查詢(xún)頁(yè)面,發(fā)現被修改過(guò)的字段值已經(jīng)變化,Hibernate輸出的log顯示,數據庫返回新的字段值。
這個(gè)試驗說(shuō)明,至少在MySQL4.1的InnoDB情況下,不使用只讀事務(wù)的查詢(xún)將無(wú)法讀取到數據更新值,必須使用只讀事務(wù)來(lái)保證讀記錄的數據一致性。這個(gè)結果非常令我詫異,和我預期完全兩樣。
我將在Oracle平臺上試試看會(huì )有什么樣的結果。
BTW: 如果MySQL的表類(lèi)型改為MyISAM,那么即使不設置事務(wù),也不會(huì )出現讀數據不一致的現象。
oracle有兩種方法保證在事務(wù)級讀數據一致性(Transaction-Level Read Consistency)
一是用SET TRANSACTION ISOLATION LEVEL SERIALIZABLE ,
當執行這條命令后讀數據時(shí)會(huì )產(chǎn)生一些重復copy, 你也可以做數據修改, 但在大量數據修改的情況下容易造成deadlock或異常, 用commit或rollback將把ISOLATION LEVEL設回為缺省模式read committed,
二是用SET TRANSCATION READ ONLY
當執行這條命令時(shí)數據庫會(huì )生成一個(gè)快照的latch, 這個(gè)latch會(huì )耗費一些resource, 如果你想進(jìn)行數據修改會(huì )導致異常. 用commit或rollback會(huì )把latch釋放掉, 也將把ISOLATION LEVEL設回為缺省模式read committed,
聯(lián)系客服