Search in sources :

Example 6 with ErrorCode

use of org.h2.api.ErrorCode in project SpringStudy by myounghaklee.

the class TestDbException method testGetJdbcSQLException.

private void testGetJdbcSQLException() throws Exception {
    for (Field field : ErrorCode.class.getDeclaredFields()) {
        if (field.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)) {
            int errorCode = field.getInt(null);
            SQLException exception = DbException.getJdbcSQLException(errorCode);
            if (exception instanceof JdbcSQLException) {
                fail("Custom exception expected for " + ErrorCode.class.getName() + '.' + field.getName() + " (" + errorCode + ')');
            }
            if (!(exception instanceof JdbcException)) {
                fail("Custom exception for " + ErrorCode.class.getName() + '.' + field.getName() + " (" + errorCode + ") should implement JdbcException");
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) ErrorCode(org.h2.api.ErrorCode) JdbcException(org.h2.jdbc.JdbcException)

Example 7 with ErrorCode

use of org.h2.api.ErrorCode in project SpringStudy by myounghaklee.

the class MVPrimaryIndex method add.

@Override
public void add(SessionLocal session, Row row) {
    if (mainIndexColumn == SearchRow.ROWID_INDEX) {
        if (row.getKey() == 0) {
            row.setKey(lastKey.incrementAndGet());
        }
    } else {
        long c = row.getValue(mainIndexColumn).getLong();
        row.setKey(c);
    }
    if (mvTable.getContainsLargeObject()) {
        for (int i = 0, len = row.getColumnCount(); i < len; i++) {
            Value v = row.getValue(i);
            if (v instanceof ValueLob) {
                ValueLob lob = ((ValueLob) v).copy(database, getId());
                session.removeAtCommitStop(lob);
                if (v != lob) {
                    row.setValue(i, lob);
                }
            }
        }
    }
    TransactionMap<Long, SearchRow> map = getMap(session);
    long rowKey = row.getKey();
    try {
        Row old = (Row) map.putIfAbsent(rowKey, row);
        if (old != null) {
            int errorCode = ErrorCode.CONCURRENT_UPDATE_1;
            if (map.getImmediate(rowKey) != null || map.getFromSnapshot(rowKey) != null) {
                // committed
                errorCode = ErrorCode.DUPLICATE_KEY_1;
            }
            DbException e = DbException.get(errorCode, getDuplicatePrimaryKeyMessage(mainIndexColumn).append(' ').append(old).toString());
            e.setSource(this);
            throw e;
        }
    } catch (MVStoreException e) {
        throw mvTable.convertException(e);
    }
    // because it's possible to directly update the key using the _rowid_
    // syntax
    long last;
    while (rowKey > (last = lastKey.get())) {
        if (lastKey.compareAndSet(last, rowKey))
            break;
    }
}
Also used : MVStoreException(org.h2.mvstore.MVStoreException) ValueLob(org.h2.value.ValueLob) Value(org.h2.value.Value) VersionedValue(org.h2.value.VersionedValue) AtomicLong(java.util.concurrent.atomic.AtomicLong) Row(org.h2.result.Row) SearchRow(org.h2.result.SearchRow) SearchRow(org.h2.result.SearchRow) DbException(org.h2.message.DbException)

Example 8 with ErrorCode

use of org.h2.api.ErrorCode in project SpringStudy by myounghaklee.

the class Store method close.

/**
 * Close the store. Pending changes are persisted.
 * If time is allocated for housekeeping, chunks with a low
 * fill rate are compacted, and some chunks are put next to each other.
 * If time is unlimited then full compaction is performed, which uses
 * different algorithm - opens alternative temp store and writes all live
 * data there, then replaces this store with a new one.
 *
 * @param allowedCompactionTime time (in milliseconds) alloted for file
 *                              compaction activity, 0 means no compaction,
 *                              -1 means unlimited time (full compaction)
 */
public void close(int allowedCompactionTime) {
    try {
        FileStore fileStore = mvStore.getFileStore();
        if (!mvStore.isClosed() && fileStore != null) {
            boolean compactFully = allowedCompactionTime == -1;
            if (fileStore.isReadOnly()) {
                compactFully = false;
            } else {
                transactionStore.close();
            }
            if (compactFully) {
                allowedCompactionTime = 0;
            }
            mvStore.close(allowedCompactionTime);
            String fileName = fileStore.getFileName();
            if (compactFully && FileUtils.exists(fileName)) {
                // the file could have been deleted concurrently,
                // so only compact if the file still exists
                MVStoreTool.compact(fileName, true);
            }
        }
    } catch (MVStoreException e) {
        int errorCode = e.getErrorCode();
        if (errorCode == DataUtils.ERROR_WRITING_FAILED) {
        // disk full - ok
        } else if (errorCode == DataUtils.ERROR_FILE_CORRUPT) {
        // wrong encryption key - ok
        }
        mvStore.closeImmediately();
        throw DbException.get(ErrorCode.IO_EXCEPTION_1, e, "Closing");
    }
}
Also used : FileStore(org.h2.mvstore.FileStore) MVStoreException(org.h2.mvstore.MVStoreException)

Example 9 with ErrorCode

use of org.h2.api.ErrorCode in project SpringStudy by myounghaklee.

the class TestTriggersConstraints method testTriggerDeadlock.

private void testTriggerDeadlock() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    try (Connection conn = getConnection("trigger")) {
        Statement stat = conn.createStatement();
        stat.execute("create table test(id int) as select 1");
        stat.execute("create table test2(id int) as select 1");
        stat.execute("create trigger test_u before update on test2 " + "for each row call \"" + DeleteTrigger.class.getName() + "\"");
        conn.setAutoCommit(false);
        stat.execute("update test set id = 2");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                try (Connection conn2 = getConnection("trigger")) {
                    conn2.setAutoCommit(false);
                    try (Statement stat2 = conn2.createStatement()) {
                        latch.countDown();
                        latch.await();
                        stat2.execute("update test2 set id = 4");
                    }
                    conn2.rollback();
                } catch (SQLException e) {
                    int errorCode = e.getErrorCode();
                    assertTrue(String.valueOf(errorCode), ErrorCode.LOCK_TIMEOUT_1 == errorCode || ErrorCode.DEADLOCK_1 == errorCode);
                }
            }
        };
        task.execute();
        latch.countDown();
        latch.await();
        try {
            stat.execute("update test2 set id = 3");
        } catch (SQLException e) {
            int errorCode = e.getErrorCode();
            assertTrue(String.valueOf(errorCode), ErrorCode.LOCK_TIMEOUT_1 == errorCode || ErrorCode.DEADLOCK_1 == errorCode);
        }
        task.get();
        conn.rollback();
        stat.execute("drop table test");
        stat.execute("drop table test2");
    }
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with ErrorCode

use of org.h2.api.ErrorCode in project h2database by h2database.

the class MVPrimaryIndex method add.

@Override
public void add(SessionLocal session, Row row) {
    if (mainIndexColumn == SearchRow.ROWID_INDEX) {
        if (row.getKey() == 0) {
            row.setKey(lastKey.incrementAndGet());
        }
    } else {
        long c = row.getValue(mainIndexColumn).getLong();
        row.setKey(c);
    }
    if (mvTable.getContainsLargeObject()) {
        for (int i = 0, len = row.getColumnCount(); i < len; i++) {
            Value v = row.getValue(i);
            if (v instanceof ValueLob) {
                ValueLob lob = ((ValueLob) v).copy(database, getId());
                session.removeAtCommitStop(lob);
                if (v != lob) {
                    row.setValue(i, lob);
                }
            }
        }
    }
    TransactionMap<Long, SearchRow> map = getMap(session);
    long rowKey = row.getKey();
    try {
        Row old = (Row) map.putIfAbsent(rowKey, row);
        if (old != null) {
            int errorCode = ErrorCode.CONCURRENT_UPDATE_1;
            if (map.getImmediate(rowKey) != null || map.getFromSnapshot(rowKey) != null) {
                // committed
                errorCode = ErrorCode.DUPLICATE_KEY_1;
            }
            DbException e = DbException.get(errorCode, getDuplicatePrimaryKeyMessage(mainIndexColumn).append(' ').append(old).toString());
            e.setSource(this);
            throw e;
        }
    } catch (MVStoreException e) {
        throw mvTable.convertException(e);
    }
    // because it's possible to directly update the key using the _rowid_
    // syntax
    long last;
    while (rowKey > (last = lastKey.get())) {
        if (lastKey.compareAndSet(last, rowKey))
            break;
    }
}
Also used : MVStoreException(org.h2.mvstore.MVStoreException) ValueLob(org.h2.value.ValueLob) Value(org.h2.value.Value) VersionedValue(org.h2.value.VersionedValue) AtomicLong(java.util.concurrent.atomic.AtomicLong) Row(org.h2.result.Row) SearchRow(org.h2.result.SearchRow) SearchRow(org.h2.result.SearchRow) DbException(org.h2.message.DbException)

Aggregations

SQLException (java.sql.SQLException)9 Database (org.h2.engine.Database)7 DbException (org.h2.message.DbException)6 ConnectionInfo (org.h2.engine.ConnectionInfo)4 SessionLocal (org.h2.engine.SessionLocal)4 MVStoreException (org.h2.mvstore.MVStoreException)4 IOException (java.io.IOException)3 Field (java.lang.reflect.Field)3 JdbcSQLException (org.h2.jdbc.JdbcSQLException)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ErrorCode (org.h2.api.ErrorCode)2 Comment (org.h2.engine.Comment)2 DbObject (org.h2.engine.DbObject)2 JdbcException (org.h2.jdbc.JdbcException)2 FileStore (org.h2.mvstore.FileStore)2 Row (org.h2.result.Row)2