public void handleCommandStatus(String status) throws PSQLException { if (status.startsWith("COPY")) { int i = status.lastIndexOf(' '); handledRowCount = i > 3 ? Long.parseLong(status.substring(i + 1)) : -1; } else { throw new PSQLException(GT.tr("CommandComplete expected COPY but got: " + status), PSQLState.COMMUNICATION_ERROR); } }
public PSQLException(String msg, PSQLState state, Throwable cause) { super(msg, state == null ? null : state.getState()); initCause(cause); }
protected boolean willHealViaReparse(SQLException e) { // "prepared statement \"S_2\" does not exist" if (PSQLState.INVALID_SQL_STATEMENT_NAME.getState().equals(e.getSQLState())) { return true; } if (!PSQLState.NOT_IMPLEMENTED.getState().equals(e.getSQLState())) { return false; } if (!(e instanceof PSQLException)) { return false; } PSQLException pe = (PSQLException) e; ServerErrorMessage serverErrorMessage = pe.getServerErrorMessage(); if (serverErrorMessage == null) { return false; } // "cached plan must not change result type" String routine = pe.getServerErrorMessage().getRoutine(); return "RevalidateCachedQuery".equals(routine) // 9.2+ || "RevalidateCachedPlan".equals(routine); // <= 9.1 }
private SQLException receiveErrorResponse() throws IOException { // it's possible to get more than one error message for a query // see libpq comments wrt backend closing a connection // so, append messages to a string buffer and keep processing // check at the bottom to see if we need to throw an exception int elen = pgStream.receiveInteger4(); EncodingPredictor.DecodeResult totalMessage = pgStream.receiveErrorString(elen - 4); ServerErrorMessage errorMsg = new ServerErrorMessage(totalMessage); if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, " <=BE ErrorMessage({0})", errorMsg.toString()); } PSQLException error = new PSQLException(errorMsg); if (transactionFailCause == null) { transactionFailCause = error; } else { error.initCause(transactionFailCause); } return error; }
@Override public void merge(String project, Object user, Object anonymousId, Instant createdAt, Instant mergedAt) { try { mergeInternal(project, user, anonymousId, createdAt, mergedAt); } catch (RuntimeException e) { if (e.getCause() instanceof PSQLException && ((PSQLException) e.getCause()).getSQLState().equals("42P01")) { new PostgresqlModule.UserMergeTableHook(projectConfig, executor) .createTable(project).join(); mergeInternal(project, user, anonymousId, createdAt, mergedAt); } } }
if (e.getMessage().matches("(?s)ERROR: option .* is unknown.*")) { 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.");
private ByteBuffer receiveNextData(boolean block) throws SQLException { try { byte[] message = copyDual.readFromCopy(block); if (message != null) { return ByteBuffer.wrap(message); } else { return null; } } catch (PSQLException e) { //todo maybe replace on thread sleep? if (e.getCause() instanceof SocketTimeoutException) { //signal for keep alive return null; } throw e; } }
public Object retryOnFailure(ProceedingJoinPoint jp) throws Throwable { Object result = null; int retryCount = 0; do { try { Object[] args = jp.getArgs(); if ( args != null && args.length > 0 ) result = jp.proceed(jp.getArgs()); else result = jp.proceed(); break; } catch (DataAccessResourceFailureException ex) { if ( ex.getCause() instanceof PSQLException ) { PSQLException psqlEx = (PSQLException) ex.getCause(); String sqlState = psqlEx.getSQLState(); LOG.warn("Got exception with sqlState=" + sqlState + ": " + ex); if ( sqlStatesToRetry.contains(sqlState) && retryCount < maxRetriesOnConnDrop ) { LOG.warn("Retrying...(retryCount=" + retryCount + ")"); sleep(retryWaitInMillis); } else { throw ex; } } } retryCount++; } while ( retryCount <= maxRetriesOnConnDrop ); return result; }
if (e.getMessage().matches("(?s)ERROR: option .* is unknown.*")) { 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.");
private static PSQLException cannotCastException(final String fromType, final String toType, final Exception cause) { return new PSQLException( GT.tr("Cannot convert an instance of {0} to type {1}", fromType, toType), PSQLState.INVALID_PARAMETER_TYPE, cause); }
private String getRefTableFromForeignKeyPsqlException(PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); Matcher messageMatcher = Pattern.compile( "update or delete on table \"(.*)\" violates foreign key constraint \"(.*)\" on table \"(.*)\"") .matcher(serverErrorMessage.getMessage()); if (!messageMatcher.matches()) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } return messageMatcher.group(1); }
public PSQLException(String msg, PSQLState state, Throwable cause) { super(msg, state == null ? null : state.getState()); initCause(cause); }
private MolgenisDataException doTranslate(PSQLException pSqlException) { switch (pSqlException.getSQLState()) { case "22001": return translateValueTooLongViolation(); case "22007": // invalid_datetime_format case "22P02": // not an integer exception return translateInvalidIntegerException(pSqlException); case "23502": // not_null_violation return translateNotNullViolation(pSqlException); case "23503": // foreign_key_violation return translateForeignKeyViolation(pSqlException); case "23505": // unique_violation return translateUniqueKeyViolation(pSqlException); case "23514": // check_violation return translateCheckConstraintViolation(pSqlException); case "2BP01": return translateDependentObjectsStillExist(pSqlException); case "42703": return translateUndefinedColumnException(pSqlException); case PostgreSqlQueryGenerator.ERR_CODE_READONLY_VIOLATION: return translateReadonlyViolation(pSqlException); default: return null; } }
public static double toDouble(String s) throws SQLException { if (s != null) { try { s = s.trim(); return Double.parseDouble(s); } catch (NumberFormatException e) { throw new PSQLException(GT.tr("Bad value for type {0} : {1}", "double", s), PSQLState.NUMERIC_VALUE_OUT_OF_RANGE); } } return 0; // SQL NULL }
/** * Package private for testability * * @param pSqlException PostgreSQL exception * @return translated validation exception */ static MolgenisValidationException translateUndefinedColumnException( PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String message = serverErrorMessage.getMessage(); // FIXME exposes internal message ConstraintViolation constraintViolation = new ConstraintViolation(message); return new MolgenisValidationException(singleton(constraintViolation)); }
public PSQLException(String msg, PSQLState state, Throwable cause) { super(msg, state == null ? null : state.getState()); initCause(cause); }
protected void handleCopydata(byte[] data) throws PSQLException { throw new PSQLException(GT.tr("CopyIn copy direction can't receive data"), PSQLState.PROTOCOL_VIOLATION); } }
/** * Package private for testability * * @param pSqlException PostgreSQL exception * @return translated validation exception */ MolgenisValidationException translateReadonlyViolation(PSQLException pSqlException) { Matcher matcher = Pattern.compile( "Updating read-only column \"?(.*?)\"? of table \"?(.*?)\"? with id \\[(.*?)] is not allowed") .matcher(pSqlException.getServerErrorMessage().getMessage()); boolean matches = matcher.matches(); if (!matches) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String colName = matcher.group(1); String tableName = matcher.group(2); String id = matcher.group(3); ConstraintViolation constraintViolation = new ConstraintViolation( format( "Updating read-only attribute '%s' of entity '%s' with id '%s' is not allowed.", tryGetAttributeName(tableName, colName).orElse(TOKEN_UNKNOWN), tryGetEntityTypeName(tableName).orElse(TOKEN_UNKNOWN), id)); return new MolgenisValidationException(singleton(constraintViolation)); }
public int executeUpdate(String p_sql) throws SQLException { throw new PSQLException( GT.tr("Can''t use query methods that take a query string on a PreparedStatement."), PSQLState.WRONG_OBJECT_TYPE); }
ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String detail = serverErrorMessage.getDetail(); Matcher matcher =