Class JdbcSupport

java.lang.Object
me.ahoo.cosid.jdbc.JdbcSupport

public final class JdbcSupport extends Object
Shared JDBC helpers: statement query timeout, transaction rollback and duplicate key detection.
  • Field Details

    • DEFAULT_QUERY_TIMEOUT

      public static final Duration DEFAULT_QUERY_TIMEOUT
      Default query timeout applied to every distributor statement.
  • Method Details

    • toQueryTimeoutSeconds

      public static int toQueryTimeoutSeconds(Duration queryTimeout)
      Converts a query timeout to the whole seconds expected by Statement.setQueryTimeout(int). Sub-second values are rounded up so that a positive timeout never turns into "no timeout".
      Parameters:
      queryTimeout - query timeout, null or zero means no timeout
      Returns:
      timeout in seconds, 0 means no timeout
    • isDuplicateKey

      public static boolean isDuplicateKey(SQLException sqlException)
      Whether the exception (or any exception in its chain) reports a duplicate key.

      SQLState class 23 alone is too broad: it also covers NOT NULL, foreign key and CHECK violations, and MySQL, Oracle and SQL Server report every integrity violation as 23000. So this matches SQLState 23505 (PostgreSQL, H2, Derby, DB2, HSQLDB) or a class 23 error with a known duplicate-key vendor code: MySQL/MariaDB 1062, Oracle 1 (ORA-00001), SQL Server 2627/2601. A SQLIntegrityConstraintViolationException without any SQLState is also treated as a duplicate.

      Parameters:
      sqlException - the exception thrown by the driver
      Returns:
      true if it is a duplicate key violation