JDBC事務隔離級別

在JDK源代碼java.sql.Connection類中,明確定義了JDBC支持的4個類型的事務隔離級別,在Connection類的開頭部分,就定義了如下5個常量,TRANSACTION_NONE常量只是用來指示不支持事務。

 /**
 * 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;
隔離級別 說明
READ_UNCOMMITTED 一個事務可以讀取另外一個事物尚未提交的數據,就是俗稱“臟讀”(dirty read),在沒有提交數據時能夠讀到已經更新的數據
TRANSACTION_READ_COMMITTED 在一個事務中進行查詢時,允許讀取提交前的數據,數據提交后,當前查詢就可以讀取到數據。update數據時候并不鎖住表
TRANSACTION_REPEATABLE_READ 在一個事務中進行查詢時,不允許讀取其他事務update的數據,允許讀取到其他事務提交的新增數據
TRANSACTION_SERIALIZABLE 在一個事務中進行查詢時,不允許任何對這個查詢表的數據修改

在Spring中設置食物隔離級別對應的接口:

/**
 * 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;
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 作者:行者 凝煙潤雨細如簾,觸水漣漪暈繞蒹;玉露瓊盤連柳岸,閑鴉野鶴對鳴蟾。芙蓉未染纖塵落,雪藕初成靜色添;意恐蓮...
    隨意詩社閱讀 537評論 0 2
  • 很多的時候發現女人還是愛黃金的 哈哈哈 怪不得小胖子一直說 真心不知道為什么中國有那么多開金店的,原來都是你們這幫...
    肥耳朵豬閱讀 470評論 0 0