Class RetryDriver

All Implemented Interfaces:
Driver

@DriverCapability(prefix="retry", description="Automatically retries failed queries on transient errors", capabilities="resilience") @DriverParameter(name="maxRetries",type=INTEGER,description="Maximum retry attempts",defaultValue="3",min=0L) @DriverParameter(name="initialDelay",type=INTEGER,description="Initial delay in ms before first retry",defaultValue="100",min=0L) @DriverParameter(name="maxDelay",type=INTEGER,description="Maximum delay cap in ms",defaultValue="5000",min=0L) @DriverParameter(name="backoffMultiplier",type=FLOAT,description="Multiplier for exponential backoff",defaultValue="2.0") @DriverParameter(name="jitter",type=BOOLEAN,description="Add random jitter to delays",defaultValue="true") @DriverParameter(name="retryOnSqlStates",type=STRING,description="Semicolon-separated SQL states to retry on") public class RetryDriver extends AbstractProxyDriver
RetryDriver automatically retries failed queries on transient errors.

WARNING: Idempotency Required

This driver should only be used with idempotent operations. Retrying non-idempotent operations (INSERT, UPDATE, DELETE) can cause data corruption, duplicate records, or inconsistent state.

Safe to retry:

  • SELECT queries (read-only)
  • Idempotent writes (e.g., UPDATE with WHERE clause on unique key)
  • Operations wrapped in application-level idempotency checks

NOT safe to retry without additional safeguards:

  • INSERT statements (may create duplicates)
  • UPDATE without unique key constraint (may apply multiple times)
  • DELETE statements (usually safe but verify business logic)
  • Statements with side effects (triggers, sequences)

For non-idempotent operations, consider using ReadonlyDriver in combination with RetryDriver, or implement application-level idempotency using idempotency keys or optimistic locking.

PreparedStatement Parameter Tracking

This driver tracks all parameter bindings (setXxx calls) on PreparedStatements. When a connection failure occurs (SQL states starting with "08"), the driver automatically reconnects, recreates the PreparedStatement, and replays all parameter bindings before retrying the operation.

Stream parameters (InputStream, Reader) are buffered into memory when set, enabling replay after reconnection. This adds memory overhead proportional to stream size.

Note: CallableStatement retry after connection failure is not yet supported due to the additional complexity of OUT parameter handling.

Default Retryable SQL States

  • 08001, 08003, 08004, 08006, 08007 - Connection errors
  • 40001, 40P01 - Deadlock/serialization failures
  • 57P01 - Admin shutdown
  • HYT00, HYT01 - Timeout errors

Example URLs

 jdbc:retry:jdbc:postgresql://localhost/mydb
 jdbc:retry[maxRetries=5,initialDelay=200]:jdbc:postgresql://localhost/mydb
 jdbc:retry[retryOnSqlStates=40001;08006]:jdbc:mysql://localhost/db
 
See Also: