Search in sources :

Example 1 with MVTable

use of org.h2.mvstore.db.MVTable in project h2database by h2database.

the class Session method commit.

/**
 * Commit the current transaction. If the statement was not a data
 * definition statement, and if there are temporary tables that should be
 * dropped or truncated at commit, this is done as well.
 *
 * @param ddl if the statement was a data definition statement
 */
public void commit(boolean ddl) {
    checkCommitRollback();
    currentTransactionName = null;
    transactionStart = 0;
    if (transaction != null) {
        // TODO should not rely on locking
        if (!locks.isEmpty()) {
            for (Table t : locks) {
                if (t instanceof MVTable) {
                    ((MVTable) t).commit();
                }
            }
        }
        transaction.commit();
        transaction = null;
    }
    if (containsUncommitted()) {
        // need to commit even if rollback is not possible
        // (create/drop table and so on)
        database.commit(this);
    }
    removeTemporaryLobs(true);
    if (undoLog.size() > 0) {
        // commit the rows when using MVCC
        if (database.isMultiVersion()) {
            ArrayList<Row> rows = New.arrayList();
            synchronized (database) {
                while (undoLog.size() > 0) {
                    UndoLogRecord entry = undoLog.getLast();
                    entry.commit();
                    rows.add(entry.getRow());
                    undoLog.removeLast(false);
                }
                for (Row r : rows) {
                    r.commit();
                }
            }
        }
        undoLog.clear();
    }
    if (!ddl) {
        // do not clean the temp tables if the last command was a
        // create/drop
        cleanTempTables(false);
        if (autoCommitAtTransactionEnd) {
            autoCommit = true;
            autoCommitAtTransactionEnd = false;
        }
    }
    int rows = getDatabase().getSettings().analyzeSample / 10;
    if (tablesToAnalyze != null) {
        for (Table table : tablesToAnalyze) {
            Analyze.analyzeTable(this, table, rows, false);
        }
        // analyze can lock the meta
        database.unlockMeta(this);
    }
    tablesToAnalyze = null;
    endTransaction();
}
Also used : MVTable(org.h2.mvstore.db.MVTable) Table(org.h2.table.Table) MVTable(org.h2.mvstore.db.MVTable) Row(org.h2.result.Row) Constraint(org.h2.constraint.Constraint)

Example 2 with MVTable

use of org.h2.mvstore.db.MVTable in project h2database by h2database.

the class Session method rollbackTo.

/**
 * Partially roll back the current transaction.
 *
 * @param savepoint the savepoint to which should be rolled back
 * @param trimToSize if the list should be trimmed
 */
public void rollbackTo(Savepoint savepoint, boolean trimToSize) {
    int index = savepoint == null ? 0 : savepoint.logIndex;
    while (undoLog.size() > index) {
        UndoLogRecord entry = undoLog.getLast();
        entry.undo(this);
        undoLog.removeLast(trimToSize);
    }
    if (transaction != null) {
        long savepointId = savepoint == null ? 0 : savepoint.transactionSavepoint;
        HashMap<String, MVTable> tableMap = database.getMvStore().getTables();
        Iterator<Change> it = transaction.getChanges(savepointId);
        while (it.hasNext()) {
            Change c = it.next();
            MVTable t = tableMap.get(c.mapName);
            if (t != null) {
                long key = ((ValueLong) c.key).getLong();
                ValueArray value = (ValueArray) c.value;
                short op;
                Row row;
                if (value == null) {
                    op = UndoLogRecord.INSERT;
                    row = t.getRow(this, key);
                } else {
                    op = UndoLogRecord.DELETE;
                    row = createRow(value.getList(), Row.MEMORY_CALCULATE);
                }
                row.setKey(key);
                UndoLogRecord log = new UndoLogRecord(t, op, row);
                log.undo(this);
            }
        }
    }
    if (savepoints != null) {
        String[] names = savepoints.keySet().toArray(new String[savepoints.size()]);
        for (String name : names) {
            Savepoint sp = savepoints.get(name);
            int savepointIndex = sp.logIndex;
            if (savepointIndex > index) {
                savepoints.remove(name);
            }
        }
    }
}
Also used : ValueLong(org.h2.value.ValueLong) MVTable(org.h2.mvstore.db.MVTable) ValueString(org.h2.value.ValueString) Change(org.h2.mvstore.db.TransactionStore.Change) Constraint(org.h2.constraint.Constraint) Row(org.h2.result.Row) ValueArray(org.h2.value.ValueArray)

Example 3 with MVTable

use of org.h2.mvstore.db.MVTable in project h2database by h2database.

the class MVTableEngine method createTable.

@Override
public TableBase createTable(CreateTableData data) {
    Database db = data.session.getDatabase();
    Store store = init(db);
    MVTable table = new MVTable(data, store);
    table.init(data.session);
    store.tableMap.put(table.getMapName(), table);
    return table;
}
Also used : Database(org.h2.engine.Database) MVStore(org.h2.mvstore.MVStore) FileStore(org.h2.mvstore.FileStore)

Example 4 with MVTable

use of org.h2.mvstore.db.MVTable in project h2database by h2database.

the class MVTable method getDeadlockDetails.

private static String getDeadlockDetails(ArrayList<Session> sessions, boolean exclusive) {
    // We add the thread details here to make it easier for customers to
    // match up these error messages with their own logs.
    StringBuilder buff = new StringBuilder();
    for (Session s : sessions) {
        Table lock = s.getWaitForLock();
        Thread thread = s.getWaitForLockThread();
        buff.append("\nSession ").append(s.toString()).append(" on thread ").append(thread.getName()).append(" is waiting to lock ").append(lock.toString()).append(exclusive ? " (exclusive)" : " (shared)").append(" while locking ");
        int i = 0;
        for (Table t : s.getLocks()) {
            if (i++ > 0) {
                buff.append(", ");
            }
            buff.append(t.toString());
            if (t instanceof MVTable) {
                if (((MVTable) t).lockExclusiveSession == s) {
                    buff.append(" (exclusive)");
                } else {
                    buff.append(" (shared)");
                }
            }
        }
        buff.append('.');
    }
    return buff.toString();
}
Also used : Table(org.h2.table.Table) Constraint(org.h2.constraint.Constraint) Session(org.h2.engine.Session)

Aggregations

Constraint (org.h2.constraint.Constraint)3 MVTable (org.h2.mvstore.db.MVTable)2 Row (org.h2.result.Row)2 Table (org.h2.table.Table)2 Database (org.h2.engine.Database)1 Session (org.h2.engine.Session)1 FileStore (org.h2.mvstore.FileStore)1 MVStore (org.h2.mvstore.MVStore)1 Change (org.h2.mvstore.db.TransactionStore.Change)1 ValueArray (org.h2.value.ValueArray)1 ValueLong (org.h2.value.ValueLong)1 ValueString (org.h2.value.ValueString)1