Java Connection 的 getTransactionIsolation() 方法及示例
在資料庫系統中,當多個事務同時並行執行時,隔離性屬性規定所有事務都將被執行,就好像它是系統中唯一的事務一樣。沒有任何事務會影響其他事務的存在。
JDBC 透過 Connection 介面提供 5 個事務隔離級別。
- TRANSACTION_NONE:用整數 0 表示,不支援事務。
- TRANSACTION_READ_COMMITTED:用整數 2 表示,支援事務,允許不可重複讀和幻讀。
- TRANSACTION_READ_UNCOMMITTED:用整數 1 表示,支援事務,允許髒讀、不可重複讀和幻讀。
- TRANSACTION_REPEATABLE_READ:用整數 4 表示,支援事務,只允許幻讀。
- TRANSACTION_SERIALIZABLE:用整數 8 表示,支援事務,不允許髒讀、不可重複讀和幻讀。
Connection 介面的 getTransactionIsolation() 方法檢索並返回底層資料庫當前事務隔離級別(整數格式)。
此方法返回一個整數值,表示 5 個事務隔離級別之一。
示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connection_getTransactionIsolation {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String url = "jdbc:mysql:///mydatabase";
Connection con = DriverManager.getConnection(url, "root", "password");
System.out.println("Connection established......");
//Retrieving the transaction isolation level
int transactionIsoltionLevel = con.getTransactionIsolation();
System.out.println("Transaction isolation level of the underlying
database is: "+transactionIsoltionLevel);
}
}輸出
Connection established...... Transaction isolation level of the underlying database is: 4
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP