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");
}
}
}
}
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;
}
}
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");
}
}
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");
}
}
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;
}
}
Aggregations