use of org.postgresql.util.PSQLException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateInvalidIntegerExceptionDateTime.
@Test
public void translateInvalidIntegerExceptionDateTime() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for type timestamp with time zone: \"str1\"");
// noinspection ThrowableResultOfMethodCallIgnored
MolgenisValidationException e = PostgreSqlExceptionTranslator.translateInvalidIntegerException(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "Value [str1] of this entity attribute is not of type [DATE_TIME].");
}
use of org.postgresql.util.PSQLException in project debezium by debezium.
the class PostgresReplicationConnection method createReplicationStream.
private ReplicationStream createReplicationStream(final LogSequenceNumber lsn) throws SQLException {
PGReplicationStream s;
try {
s = startPgReplicationStream(lsn, plugin.forceRds() ? messageDecoder::optionsWithoutMetadata : messageDecoder::optionsWithMetadata);
messageDecoder.setContainsMetadata(plugin.forceRds() ? false : true);
} catch (PSQLException e) {
if (e.getMessage().matches("(?s)ERROR: option .* is unknown.*")) {
// It is possible we are connecting to an old wal2json plug-in
LOGGER.warn("Could not register for streaming with metadata in messages, falling back to messages without metadata");
s = startPgReplicationStream(lsn, messageDecoder::optionsWithoutMetadata);
messageDecoder.setContainsMetadata(false);
} else if (e.getMessage().matches("(?s)ERROR: requested WAL segment .* has already been removed.*")) {
LOGGER.error("Cannot rewind to last processed WAL position", e);
throw new ConnectException("The offset to start reading from has been removed from the database write-ahead log. Create a new snapshot and consider setting of PostgreSQL parameter wal_keep_segments = 0.");
} else {
throw e;
}
}
final PGReplicationStream stream = s;
final long lsnLong = lsn.asLong();
return new ReplicationStream() {
private static final int CHECK_WARNINGS_AFTER_COUNT = 100;
private int warningCheckCounter = CHECK_WARNINGS_AFTER_COUNT;
// make sure this is volatile since multiple threads may be interested in this value
private volatile LogSequenceNumber lastReceivedLSN;
@Override
public void read(ReplicationMessageProcessor processor) throws SQLException, InterruptedException {
ByteBuffer read = stream.read();
// the lsn we started from is inclusive, so we need to avoid sending back the same message twice
if (lsnLong >= stream.getLastReceiveLSN().asLong()) {
return;
}
deserializeMessages(read, processor);
}
@Override
public void readPending(ReplicationMessageProcessor processor) throws SQLException, InterruptedException {
ByteBuffer read = stream.readPending();
// the lsn we started from is inclusive, so we need to avoid sending back the same message twice
if (read == null || lsnLong >= stream.getLastReceiveLSN().asLong()) {
return;
}
deserializeMessages(read, processor);
}
private void deserializeMessages(ByteBuffer buffer, ReplicationMessageProcessor processor) throws SQLException, InterruptedException {
lastReceivedLSN = stream.getLastReceiveLSN();
messageDecoder.processMessage(buffer, processor, typeRegistry);
}
@Override
public void close() throws SQLException {
processWarnings(true);
stream.close();
}
@Override
public void flushLastReceivedLsn() throws SQLException {
if (lastReceivedLSN == null) {
// nothing to flush yet, since we haven't read anything...
return;
}
doFlushLsn(lastReceivedLSN);
}
@Override
public void flushLsn(long lsn) throws SQLException {
doFlushLsn(LogSequenceNumber.valueOf(lsn));
}
private void doFlushLsn(LogSequenceNumber lsn) throws SQLException {
stream.setFlushedLSN(lsn);
stream.setAppliedLSN(lsn);
stream.forceUpdateStatus();
}
@Override
public Long lastReceivedLsn() {
return lastReceivedLSN != null ? lastReceivedLSN.asLong() : null;
}
private void processWarnings(final boolean forced) throws SQLException {
if (--warningCheckCounter == 0 || forced) {
warningCheckCounter = CHECK_WARNINGS_AFTER_COUNT;
for (SQLWarning w = connection().getWarnings(); w != null; w = w.getNextWarning()) {
LOGGER.debug("Server-side message: '{}', state = {}, code = {}", w.getMessage(), w.getSQLState(), w.getErrorCode());
}
}
}
};
}
use of org.postgresql.util.PSQLException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateUniqueKeyViolationKeyIsDuplicatedDoubleQuotes.
@Test
public void translateUniqueKeyViolationKeyIsDuplicatedDoubleQuotes() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getSQLState()).thenReturn("23505");
when(serverErrorMessage.getTable()).thenReturn("myTable");
when(serverErrorMessage.getDetail()).thenReturn("Key (\"myColumn\")=(myValue) is duplicated.");
// noinspection ThrowableResultOfMethodCallIgnored
MolgenisValidationException e = postgreSqlExceptionTranslator.translateUniqueKeyViolation(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "The attribute 'myAttr' of entity 'myEntity' contains duplicate value 'myValue'.");
}
use of org.postgresql.util.PSQLException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateInvalidIntegerExceptionBoolean.
@Test
public void translateInvalidIntegerExceptionBoolean() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for type boolean: \"str1\"");
// noinspection ThrowableResultOfMethodCallIgnored
MolgenisValidationException e = PostgreSqlExceptionTranslator.translateInvalidIntegerException(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "Value [str1] of this entity attribute is not of type [BOOL].");
}
use of org.postgresql.util.PSQLException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateUniqueKeyViolationCompositeKey.
@Test
public void translateUniqueKeyViolationCompositeKey() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getSQLState()).thenReturn("23505");
when(serverErrorMessage.getTable()).thenReturn("myTable");
when(serverErrorMessage.getDetail()).thenReturn("Key (myIdColumn, myColumn)=(myIdValue, myValue) already exists.");
// noinspection ThrowableResultOfMethodCallIgnored
MolgenisValidationException e = postgreSqlExceptionTranslator.translateUniqueKeyViolation(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "Duplicate list value 'myValue' for attribute 'myAttr' from entity 'myEntity' with id 'myIdValue'.");
}
Aggregations