Class RetryDriver
- All Implemented Interfaces:
Driver
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classConfiguration holder for retry parameters, parsed once at connection time. -
Driver Parameters
URL format:
jdbc:retry[param=value,...]:<target-url>Parameter Type Default Constraints Description maxRetriesinteger 3≥ 0 Maximum retry attempts initialDelayinteger 100≥ 0 Initial delay in ms before first retry maxDelayinteger 5000≥ 0 Maximum delay cap in ms backoffMultiplierfloat 2.0- Multiplier for exponential backoff jitterboolean true- Add random jitter to delays retryOnSqlStatesstring - - Semicolon-separated SQL states to retry on Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanacceptsSubName(String subname) protected booleanacceptsSubProtocol(String subprotocol) connect(String url, Properties info) protected CallableStatementproxyCallableStatement(CallableStatement delegate, Connection conn) protected CallableStatementproxyCallableStatement(CallableStatement delegate, Connection conn, String sql) Proxy a CallableStatement with access to the original SQL.protected ConnectionproxyConnection(Connection delegate, String url, Properties info, Driver driver) protected PreparedStatementproxyPreparedStatement(PreparedStatement delegate, Connection conn) protected PreparedStatementproxyPreparedStatement(PreparedStatement delegate, Connection conn, String sql) Proxy a PreparedStatement with access to the original SQL.protected StatementproxyStatement(Statement delegate, Connection conn) Methods inherited from class org.pjdbc.sql.AbstractProxyDriver
getPropertyInfo, isCompositionValidationEnabled, isValidateParametersEnabled, proxyConnection, proxyConnection, proxyResultSetMethods inherited from class org.pjdbc.sql.AbstractDriver
acceptsProtocol, acceptsURL, getMajorVersion, getMinorVersion, getParentLogger, getUrlParameter, getUrlParameter, getUrlParameters, jdbcCompliant, parseUrl, protocol, subname, subprotocol, validateParameters
-
Constructor Details
-
RetryDriver
public RetryDriver()
-
-
Method Details
-
acceptsSubProtocol
- Specified by:
acceptsSubProtocolin classAbstractDriver
-
acceptsSubName
- Overrides:
acceptsSubNamein classAbstractProxyDriver
-
connect
- Specified by:
connectin interfaceDriver- Overrides:
connectin classAbstractProxyDriver- Throws:
SQLException
-
proxyConnection
protected Connection proxyConnection(Connection delegate, String url, Properties info, Driver driver) throws SQLException - Overrides:
proxyConnectionin classAbstractProxyDriver- Throws:
SQLException
-
proxyStatement
- Overrides:
proxyStatementin classAbstractProxyDriver- Throws:
SQLException
-
proxyPreparedStatement
protected PreparedStatement proxyPreparedStatement(PreparedStatement delegate, Connection conn) throws SQLException - Overrides:
proxyPreparedStatementin classAbstractProxyDriver- Throws:
SQLException
-
proxyPreparedStatement
protected PreparedStatement proxyPreparedStatement(PreparedStatement delegate, Connection conn, String sql) throws SQLException Description copied from class:AbstractProxyDriverProxy a PreparedStatement with access to the original SQL. Override this method when you need to recreate the statement (e.g., on connection failure). The default implementation ignores the SQL and delegates toAbstractProxyDriver.proxyPreparedStatement(PreparedStatement, Connection).- Overrides:
proxyPreparedStatementin classAbstractProxyDriver- Parameters:
delegate- the delegate PreparedStatementconn- the connectionsql- the SQL used to prepare the statement- Returns:
- the proxied PreparedStatement
- Throws:
SQLException- if an error occurs
-
proxyCallableStatement
protected CallableStatement proxyCallableStatement(CallableStatement delegate, Connection conn) throws SQLException - Overrides:
proxyCallableStatementin classAbstractProxyDriver- Throws:
SQLException
-
proxyCallableStatement
protected CallableStatement proxyCallableStatement(CallableStatement delegate, Connection conn, String sql) throws SQLException Description copied from class:AbstractProxyDriverProxy a CallableStatement with access to the original SQL. Override this method when you need to recreate the statement (e.g., on connection failure). The default implementation ignores the SQL and delegates toAbstractProxyDriver.proxyCallableStatement(CallableStatement, Connection).- Overrides:
proxyCallableStatementin classAbstractProxyDriver- Parameters:
delegate- the delegate CallableStatementconn- the connectionsql- the SQL used to prepare the statement- Returns:
- the proxied CallableStatement
- Throws:
SQLException- if an error occurs
-