Search in sources :

Example 26 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class Parser method parseShow.

private Prepared parseShow() {
    ArrayList<Value> paramValues = New.arrayList();
    StringBuilder buff = new StringBuilder("SELECT ");
    if (readIf("CLIENT_ENCODING")) {
        // for PostgreSQL compatibility
        buff.append("'UNICODE' AS CLIENT_ENCODING FROM DUAL");
    } else if (readIf("DEFAULT_TRANSACTION_ISOLATION")) {
        // for PostgreSQL compatibility
        buff.append("'read committed' AS DEFAULT_TRANSACTION_ISOLATION " + "FROM DUAL");
    } else if (readIf("TRANSACTION")) {
        // for PostgreSQL compatibility
        read("ISOLATION");
        read("LEVEL");
        buff.append("'read committed' AS TRANSACTION_ISOLATION " + "FROM DUAL");
    } else if (readIf("DATESTYLE")) {
        // for PostgreSQL compatibility
        buff.append("'ISO' AS DATESTYLE FROM DUAL");
    } else if (readIf("SERVER_VERSION")) {
        // for PostgreSQL compatibility
        buff.append("'" + Constants.PG_VERSION + "' AS SERVER_VERSION FROM DUAL");
    } else if (readIf("SERVER_ENCODING")) {
        // for PostgreSQL compatibility
        buff.append("'UTF8' AS SERVER_ENCODING FROM DUAL");
    } else if (readIf("TABLES")) {
        // for MySQL compatibility
        String schema = Constants.SCHEMA_MAIN;
        if (readIf("FROM")) {
            schema = readUniqueIdentifier();
        }
        buff.append("TABLE_NAME, TABLE_SCHEMA FROM " + "INFORMATION_SCHEMA.TABLES " + "WHERE TABLE_SCHEMA=? ORDER BY TABLE_NAME");
        paramValues.add(ValueString.get(schema));
    } else if (readIf("COLUMNS")) {
        // for MySQL compatibility
        read("FROM");
        String tableName = readIdentifierWithSchema();
        String schemaName = getSchema().getName();
        paramValues.add(ValueString.get(tableName));
        if (readIf("FROM")) {
            schemaName = readUniqueIdentifier();
        }
        buff.append("C.COLUMN_NAME FIELD, " + "C.TYPE_NAME || '(' || C.NUMERIC_PRECISION || ')' TYPE, " + "C.IS_NULLABLE \"NULL\", " + "CASE (SELECT MAX(I.INDEX_TYPE_NAME) FROM " + "INFORMATION_SCHEMA.INDEXES I " + "WHERE I.TABLE_SCHEMA=C.TABLE_SCHEMA " + "AND I.TABLE_NAME=C.TABLE_NAME " + "AND I.COLUMN_NAME=C.COLUMN_NAME)" + "WHEN 'PRIMARY KEY' THEN 'PRI' " + "WHEN 'UNIQUE INDEX' THEN 'UNI' ELSE '' END KEY, " + "IFNULL(COLUMN_DEFAULT, 'NULL') DEFAULT " + "FROM INFORMATION_SCHEMA.COLUMNS C " + "WHERE C.TABLE_NAME=? AND C.TABLE_SCHEMA=? " + "ORDER BY C.ORDINAL_POSITION");
        paramValues.add(ValueString.get(schemaName));
    } else if (readIf("DATABASES") || readIf("SCHEMAS")) {
        // for MySQL compatibility
        buff.append("SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA");
    }
    boolean b = session.getAllowLiterals();
    try {
        // need to temporarily enable it, in case we are in
        // ALLOW_LITERALS_NUMBERS mode
        session.setAllowLiterals(true);
        return prepare(session, buff.toString(), paramValues);
    } finally {
        session.setAllowLiterals(b);
    }
}
Also used : Value(org.h2.value.Value) SequenceValue(org.h2.expression.SequenceValue) ValueString(org.h2.value.ValueString)

Example 27 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class Command method executeQuery.

/**
 * Execute a query and return the result.
 * This method prepares everything and calls {@link #query(int)} finally.
 *
 * @param maxrows the maximum number of rows to return
 * @param scrollable if the result set must be scrollable (ignored)
 * @return the result set
 */
@Override
public ResultInterface executeQuery(int maxrows, boolean scrollable) {
    startTimeNanos = 0;
    long start = 0;
    Database database = session.getDatabase();
    Object sync = database.isMultiThreaded() ? (Object) session : (Object) database;
    session.waitIfExclusiveModeEnabled();
    boolean callStop = true;
    boolean writing = !isReadOnly();
    if (writing) {
        while (!database.beforeWriting()) {
        // wait
        }
    }
    synchronized (sync) {
        session.setCurrentCommand(this, false);
        try {
            while (true) {
                database.checkPowerOff();
                try {
                    ResultInterface result = query(maxrows);
                    callStop = !result.isLazy();
                    return result;
                } catch (DbException e) {
                    start = filterConcurrentUpdate(e, start);
                } catch (OutOfMemoryError e) {
                    callStop = false;
                    // there is a serious problem:
                    // the transaction may be applied partially
                    // in this case we need to panic:
                    // close the database
                    database.shutdownImmediately();
                    throw DbException.convert(e);
                } catch (Throwable e) {
                    throw DbException.convert(e);
                }
            }
        } catch (DbException e) {
            e = e.addSQL(sql);
            SQLException s = e.getSQLException();
            database.exceptionThrown(s, sql);
            if (s.getErrorCode() == ErrorCode.OUT_OF_MEMORY) {
                callStop = false;
                database.shutdownImmediately();
                throw e;
            }
            database.checkPowerOff();
            throw e;
        } finally {
            if (callStop) {
                stop();
            }
            if (writing) {
                database.afterWriting();
            }
        }
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) SQLException(java.sql.SQLException) Database(org.h2.engine.Database) DbException(org.h2.message.DbException)

Example 28 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class JdbcConnection method getTransactionIsolation.

/**
 * Returns the current transaction isolation level.
 *
 * @return the isolation level.
 * @throws SQLException if the connection is closed
 */
@Override
public int getTransactionIsolation() throws SQLException {
    try {
        debugCodeCall("getTransactionIsolation");
        checkClosed();
        getLockMode = prepareCommand("CALL LOCK_MODE()", getLockMode);
        ResultInterface result = getLockMode.executeQuery(0, false);
        result.next();
        int lockMode = result.currentRow()[0].getInt();
        result.close();
        int transactionIsolationLevel;
        switch(lockMode) {
            case Constants.LOCK_MODE_OFF:
                transactionIsolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
                break;
            case Constants.LOCK_MODE_READ_COMMITTED:
                transactionIsolationLevel = Connection.TRANSACTION_READ_COMMITTED;
                break;
            case Constants.LOCK_MODE_TABLE:
            case Constants.LOCK_MODE_TABLE_GC:
                transactionIsolationLevel = Connection.TRANSACTION_SERIALIZABLE;
                break;
            default:
                throw DbException.throwInternalError("lockMode:" + lockMode);
        }
        return transactionIsolationLevel;
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 29 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class JdbcConnection method setTransactionIsolation.

/**
 * Changes the current transaction isolation level. Calling this method will
 * commit an open transaction, even if the new level is the same as the old
 * one, except if the level is not supported. Internally, this method calls
 * SET LOCK_MODE, which affects all connections. The following isolation
 * levels are supported:
 * <ul>
 * <li>Connection.TRANSACTION_READ_UNCOMMITTED = SET LOCK_MODE 0: no locking
 * (should only be used for testing).</li>
 * <li>Connection.TRANSACTION_SERIALIZABLE = SET LOCK_MODE 1: table level
 * locking.</li>
 * <li>Connection.TRANSACTION_READ_COMMITTED = SET LOCK_MODE 3: table level
 * locking, but read locks are released immediately (default).</li>
 * </ul>
 * This setting is not persistent. Please note that using
 * TRANSACTION_READ_UNCOMMITTED while at the same time using multiple
 * connections may result in inconsistent transactions.
 *
 * @param level the new transaction isolation level:
 *            Connection.TRANSACTION_READ_UNCOMMITTED,
 *            Connection.TRANSACTION_READ_COMMITTED, or
 *            Connection.TRANSACTION_SERIALIZABLE
 * @throws SQLException if the connection is closed or the isolation level
 *             is not supported
 */
@Override
public void setTransactionIsolation(int level) throws SQLException {
    try {
        debugCodeCall("setTransactionIsolation", level);
        checkClosed();
        int lockMode;
        switch(level) {
            case Connection.TRANSACTION_READ_UNCOMMITTED:
                lockMode = Constants.LOCK_MODE_OFF;
                break;
            case Connection.TRANSACTION_READ_COMMITTED:
                lockMode = Constants.LOCK_MODE_READ_COMMITTED;
                break;
            case Connection.TRANSACTION_REPEATABLE_READ:
            case Connection.TRANSACTION_SERIALIZABLE:
                lockMode = Constants.LOCK_MODE_TABLE;
                break;
            default:
                throw DbException.getInvalidValueException("level", level);
        }
        commit();
        setLockMode = prepareCommand("SET LOCK_MODE ?", setLockMode);
        setLockMode.getParameters().get(0).setValue(ValueInt.get(lockMode), false);
        setLockMode.executeUpdate(false);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 30 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class JdbcDatabaseMetaData method supportsTransactionIsolationLevel.

/**
 * Returns whether a specific transaction isolation level is supported.
 *
 * @param level the transaction isolation level (Connection.TRANSACTION_*)
 * @return true
 */
@Override
public boolean supportsTransactionIsolationLevel(int level) throws SQLException {
    debugCodeCall("supportsTransactionIsolationLevel");
    if (level == Connection.TRANSACTION_READ_UNCOMMITTED) {
        // currently the combination of LOCK_MODE=0 and MULTI_THREADED
        // is not supported, also see code in Database#setLockMode(int)
        PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=?");
        prep.setString(1, "MULTI_THREADED");
        ResultSet rs = prep.executeQuery();
        return !rs.next() || !rs.getString(1).equals("1");
    }
    return true;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Transaction (org.h2.mvstore.db.TransactionStore.Transaction)22 MVStore (org.h2.mvstore.MVStore)18 TransactionStore (org.h2.mvstore.db.TransactionStore)18 Connection (java.sql.Connection)13 ResultSet (java.sql.ResultSet)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)8 DbException (org.h2.message.DbException)8 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)7 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)7 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)7 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)7 PreparedStatement (java.sql.PreparedStatement)7 Statement (java.sql.Statement)7 Enumeration (java.util.Enumeration)7 XAResource (javax.transaction.xa.XAResource)7 Test (org.junit.Test)7 SQLException (java.sql.SQLException)6 Vector (java.util.Vector)6 Constraint (org.h2.constraint.Constraint)6