JDBC事務(wù)隔離級(jí)別

在JDK源代碼java.sql.Connection類中,明確定義了JDBC支持的4個(gè)類型的事務(wù)隔離級(jí)別,在Connection類的開(kāi)頭部分,就定義了如下5個(gè)常量,TRANSACTION_NONE常量只是用來(lái)指示不支持事務(wù)。

 /**
 * A constant indicating that transactions are not supported.
 */
int TRANSACTION_NONE             = 0;

/**
 * A constant indicating that
 * dirty reads, non-repeatable reads and phantom reads can occur.
 * This level allows a row changed by one transaction to be read
 * by another transaction before any changes in that row have been
 * committed (a "dirty read").  If any of the changes are rolled back,
 * the second transaction will have retrieved an invalid row.
 */
int TRANSACTION_READ_UNCOMMITTED = 1;

/**
 * A constant indicating that
 * dirty reads are prevented; non-repeatable reads and phantom
 * reads can occur.  This level only prohibits a transaction
 * from reading a row with uncommitted changes in it.
 */
int TRANSACTION_READ_COMMITTED   = 2;

/**
 * A constant indicating that
 * dirty reads and non-repeatable reads are prevented; phantom
 * reads can occur.  This level prohibits a transaction from
 * reading a row with uncommitted changes in it, and it also
 * prohibits the situation where one transaction reads a row,
 * a second transaction alters the row, and the first transaction
 * rereads the row, getting different values the second time
 * (a "non-repeatable read").
 */
int TRANSACTION_REPEATABLE_READ  = 4;

/**
 * A constant indicating that
 * dirty reads, non-repeatable reads and phantom reads are prevented.
 * This level includes the prohibitions in
 * <code>TRANSACTION_REPEATABLE_READ</code> and further prohibits the
 * situation where one transaction reads all rows that satisfy
 * a <code>WHERE</code> condition, a second transaction inserts a row that
 * satisfies that <code>WHERE</code> condition, and the first transaction
 * rereads for the same condition, retrieving the additional
 * "phantom" row in the second read.
 */
int TRANSACTION_SERIALIZABLE     = 8;
隔離級(jí)別 說(shuō)明
READ_UNCOMMITTED 一個(gè)事務(wù)可以讀取另外一個(gè)事物尚未提交的數(shù)據(jù),就是俗稱“臟讀”(dirty read),在沒(méi)有提交數(shù)據(jù)時(shí)能夠讀到已經(jīng)更新的數(shù)據(jù)
TRANSACTION_READ_COMMITTED 在一個(gè)事務(wù)中進(jìn)行查詢時(shí),允許讀取提交前的數(shù)據(jù),數(shù)據(jù)提交后,當(dāng)前查詢就可以讀取到數(shù)據(jù)。update數(shù)據(jù)時(shí)候并不鎖住表
TRANSACTION_REPEATABLE_READ 在一個(gè)事務(wù)中進(jìn)行查詢時(shí),不允許讀取其他事務(wù)update的數(shù)據(jù),允許讀取到其他事務(wù)提交的新增數(shù)據(jù)
TRANSACTION_SERIALIZABLE 在一個(gè)事務(wù)中進(jìn)行查詢時(shí),不允許任何對(duì)這個(gè)查詢表的數(shù)據(jù)修改

在Spring中設(shè)置食物隔離級(jí)別對(duì)應(yīng)的接口:

/**
 * Attempts to change the transaction isolation level for this
 * <code>Connection</code> object to the one given.
 * The constants defined in the interface <code>Connection</code>
 * are the possible transaction isolation levels.
 * <P>
 * <B>Note:</B> If this method is called during a transaction, the result
 * is implementation-defined.
 *
 * @param level one of the following <code>Connection</code> constants:
 *        <code>Connection.TRANSACTION_READ_UNCOMMITTED</code>,
 *        <code>Connection.TRANSACTION_READ_COMMITTED</code>,
 *        <code>Connection.TRANSACTION_REPEATABLE_READ</code>, or
 *        <code>Connection.TRANSACTION_SERIALIZABLE</code>.
 *        (Note that <code>Connection.TRANSACTION_NONE</code> cannot be used
 *        because it specifies that transactions are not supported.)
 * @exception SQLException if a database access error occurs, this
 * method is called on a closed connection
 *            or the given parameter is not one of the <code>Connection</code>
 *            constants
 * @see DatabaseMetaData#supportsTransactionIsolationLevel
 * @see #getTransactionIsolation
 */
void setTransactionIsolation(int level) throws SQLException;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 作者:行者 凝煙潤(rùn)雨細(xì)如簾,觸水漣漪暈繞蒹;玉露瓊盤連柳岸,閑鴉野鶴對(duì)鳴蟾。芙蓉未染纖塵落,雪藕初成靜色添;意恐蓮...
    隨意詩(shī)社閱讀 553評(píng)論 0 2
  • 很多的時(shí)候發(fā)現(xiàn)女人還是愛(ài)黃金的 哈哈哈 怪不得小胖子一直說(shuō) 真心不知道為什么中國(guó)有那么多開(kāi)金店的,原來(lái)都是你們這幫...
    肥耳朵豬閱讀 513評(píng)論 0 0

友情鏈接更多精彩內(nèi)容