Class DatabaseException

All Implemented Interfaces:
Serializable

@NotThreadSafe public class DatabaseException extends RuntimeException
Thrown when an error occurs when interacting with a Database.

If the cause of this exception is a SQLException, the getErrorCode() and getSqlState() accessors are shorthand for retrieving the corresponding SQLException values.

Since:
1.0.0
Author:
Mark Allen
See Also:
  • Constructor Details

    • DatabaseException

      public DatabaseException(@Nullable String message)
      Creates a DatabaseException with the given message.
      Parameters:
      message - a message describing this exception
    • DatabaseException

      public DatabaseException(@Nullable Throwable cause)
      Creates a DatabaseException which wraps the given cause.
      Parameters:
      cause - the cause of this exception
    • DatabaseException

      public DatabaseException(@Nullable String message, @Nullable Throwable cause)
      Creates a DatabaseException which wraps the given cause.
      Parameters:
      message - a message describing this exception
      cause - the cause of this exception
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Throwable
    • getErrorCode

      public @NonNull Optional<Integer> getErrorCode()
      Shorthand for SQLException.getErrorCode() if this exception was caused by a SQLException.
      Returns:
      the value of SQLException.getErrorCode(), or empty if not available
    • getSqlState

      public @NonNull Optional<String> getSqlState()
      Shorthand for SQLException.getSQLState() if this exception was caused by a SQLException.
      Returns:
      the value of SQLException.getSQLState(), or empty if not available
    • isUniqueConstraintViolation

      Determines if this exception is recognized as a unique constraint violation.

      This method is intentionally conservative: it returns true only when Pyranid recognizes a database-specific error code or SQLState as a unique or primary-key uniqueness violation.

      Returns:
      true if this exception is recognized as a unique constraint violation
      Since:
      4.3.0
    • isForeignKeyViolation

      public @NonNull Boolean isForeignKeyViolation()
      Determines if this exception is recognized as a foreign-key violation.

      This method is intentionally conservative: it returns true only when Pyranid recognizes a database-specific error code or SQLState as a foreign-key violation. For example, SQL Server error 547 is not classified as a foreign-key violation by code alone because the same error code also covers other constraint failures, such as CHECK constraints.

      Returns:
      true if this exception is recognized as a foreign-key violation
      Since:
      4.3.0
    • isDeadlock

      public @NonNull Boolean isDeadlock()
      Determines if this exception is recognized as a deadlock.

      This method is intentionally conservative: it returns true only when Pyranid recognizes a database-specific error code or SQLState as a deadlock.

      Returns:
      true if this exception is recognized as a deadlock
      Since:
      4.3.0
    • isTransient

      public @NonNull Boolean isTransient()
      Determines if this exception is recognized as transient.

      This method is intentionally conservative: it returns true for JDBC transient/recoverable exception classes, SQLState classes for connection exceptions and transaction rollbacks, or database-specific deadlock/lock-timeout signals recognized by Pyranid. A true result does not guarantee that retrying the operation will succeed.

      Returns:
      true if this exception is recognized as transient
      Since:
      4.3.0
    • isSerializationFailure

      public @NonNull Boolean isSerializationFailure()
      Determines if this exception is recognized as a serialization failure.

      This method is intentionally conservative: it returns true only when Pyranid recognizes a database-specific error code or SQLState as a serialization failure.

      Returns:
      true if this exception is recognized as a serialization failure
      Since:
      4.4.0
    • isTimeout

      public @NonNull Boolean isTimeout()
      Determines if this exception is recognized as a timeout or cancellation.

      This method is intentionally conservative: it returns true only when Pyranid recognizes a JDBC timeout, database-specific timeout, or database-specific cancellation signal. A true result does not mean the operation is automatically safe to retry.

      Returns:
      true if this exception is recognized as a timeout or cancellation
      Since:
      4.4.0
    • getColumn

      public @NonNull Optional<String> getColumn()
      Returns:
      the value of the offending column, or empty if not available
      Since:
      1.0.12
    • getConstraint

      public @NonNull Optional<String> getConstraint()
      Returns:
      the value of the offending constraint, or empty if not available
      Since:
      1.0.12
    • getDatatype

      public @NonNull Optional<String> getDatatype()
      Returns:
      the value of the offending datatype, or empty if not available
      Since:
      1.0.12
    • getDetail

      public @NonNull Optional<String> getDetail()
      Returns:
      the value of the offending detail, or empty if not available
      Since:
      1.0.12
    • getFile

      public @NonNull Optional<String> getFile()
      Returns:
      the value of the offending file, or empty if not available
      Since:
      1.0.12
    • getHint

      public @NonNull Optional<String> getHint()
      Returns:
      the value of the error hint, or empty if not available
      Since:
      1.0.12
    • getInternalPosition

      public @NonNull Optional<Integer> getInternalPosition()
      Returns:
      the value of the offending internalPosition, or empty if not available
      Since:
      1.0.12
    • getInternalQuery

      public @NonNull Optional<String> getInternalQuery()
      Returns:
      the value of the offending internalQuery, or empty if not available
      Since:
      1.0.12
    • getLine

      public @NonNull Optional<Integer> getLine()
      Returns:
      the value of the offending line, or empty if not available
      Since:
      1.0.12
    • getDbmsMessage

      public @NonNull Optional<String> getDbmsMessage()
      Returns:
      the value of the error dbmsMessage, or empty if not available
      Since:
      1.0.12
    • getPosition

      public @NonNull Optional<Integer> getPosition()
      Returns:
      the value of the offending position, or empty if not available
      Since:
      1.0.12
    • getRoutine

      public @NonNull Optional<String> getRoutine()
      Returns:
      the value of the offending routine, or empty if not available
      Since:
      1.0.12
    • getSchema

      public @NonNull Optional<String> getSchema()
      Returns:
      the value of the offending schema, or empty if not available
      Since:
      1.0.12
    • getSeverity

      public @NonNull Optional<String> getSeverity()
      Returns:
      the error severity, or empty if not available
      Since:
      1.0.12
    • getTable

      public @NonNull Optional<String> getTable()
      Returns:
      the value of the offending table, or empty if not available
      Since:
      1.0.12
    • getWhere

      public @NonNull Optional<String> getWhere()
      Returns:
      the value of the offending where, or empty if not available
      Since:
      1.0.12