Java DatabaseMetaData .getDefaultTransactionIsolation ()

Syntax

DatabaseMetaData.getDefaultTransactionIsolation() has the following syntax.

int getDefaultTransactionIsolation()     throws SQLException

Example

In the following code shows how to use DatabaseMetaData.getDefaultTransactionIsolation() method.


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
// w  ww. ja  va2  s  .  com
public class Main {

  public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();      

    int txisolation = meta.getDefaultTransactionIsolation();
    System.out.println("Database default transaction isolation is "
            + txisolation + " ("
            + transactionIsolationToString(txisolation) + ").");
    conn.close();
  }

    /**
     * Convert a TransactionIsolation int (defined in java.sql.Connection) to
     * the corresponding printable string.
     */
    public static String transactionIsolationToString(int txisolation) {
        switch (txisolation) {
        case Connection.TRANSACTION_NONE:
            // transactions not supported.
            return "TRANSACTION_NONE";
        case Connection.TRANSACTION_READ_UNCOMMITTED:
            // All three phenomena can occur
            return "TRANSACTION_NONE";
        case Connection.TRANSACTION_READ_COMMITTED:
            // Dirty reads are prevented; non-repeatable reads and
            // phantom reads can occur.
            return "TRANSACTION_READ_COMMITTED";
        case Connection.TRANSACTION_REPEATABLE_READ:
            // Dirty reads and non-repeatable reads are prevented;
            // phantom reads can occur.
            return "TRANSACTION_REPEATABLE_READ";
        case Connection.TRANSACTION_SERIALIZABLE:
            // All three phenomena prvented; slowest!
            return "TRANSACTION_SERIALIZABLE";
        default:
            throw new IllegalArgumentException(txisolation
                    + " not a valid TX_ISOLATION");
        }
    }


  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.sql »




DatabaseMetaData
ParameterMetaData
PreparedStatement
ResultSet
Timestamp