Package org.pjdbc.drivers
Class CircuitBreakerDriver
java.lang.Object
org.pjdbc.sql.AbstractDriver
org.pjdbc.sql.AbstractProxyDriver
org.pjdbc.drivers.CircuitBreakerDriver
- All Implemented Interfaces:
Driver
@DriverCapability(prefix="circuitbreaker",
description="Implements circuit breaker pattern for fault tolerance with CLOSED/OPEN/HALF_OPEN states",
capabilities="resilience")
@DriverParameter(name="name",type=STRING,description="Circuit breaker name for monitoring",defaultValue="default") @DriverParameter(name="failureThreshold",type=INTEGER,description="Failures before opening circuit",defaultValue="5",min=1L) @DriverParameter(name="successThreshold",type=INTEGER,description="Successes in half-open to close circuit",defaultValue="1",min=1L) @DriverParameter(name="resetTimeout",type=INTEGER,description="Time in ms before open -> half-open",defaultValue="30000",min=1L)
@DriverSideEffects(stateful=true)
public class CircuitBreakerDriver
extends AbstractProxyDriver
CircuitBreakerDriver implements the circuit breaker pattern for fault tolerance.
The circuit breaker has three states:
- CLOSED - Normal operation, requests pass through to the database
- OPEN - Circuit is tripped, all requests fail fast without attempting the operation
- HALF_OPEN - Testing state, allows limited requests to check if backend has recovered
State transitions:
- CLOSED to OPEN: After failureThreshold consecutive failures
- OPEN to HALF_OPEN: After resetTimeout milliseconds have elapsed
- HALF_OPEN to CLOSED: After successThreshold consecutive successes
- HALF_OPEN to OPEN: On any failure
URL format: jdbc:circuitbreaker[param=value,...]:jdbc:target:...
Example URLs:
jdbc:circuitbreaker:jdbc:postgresql://localhost/mydb jdbc:circuitbreaker[failureThreshold=3,resetTimeout=60000]:jdbc:postgresql://localhost/mydb jdbc:circuitbreaker[name=primary-db,failureThreshold=10]:jdbc:mysql://localhost/db
JMX Monitoring
Circuit breakers can be monitored via JMX. Enable with:
- System property:
-Dpjdbc.jmx.enabled=true - Programmatic:
CircuitBreakerRegistry.enableJmx()
When enabled, MBeans are registered under:
org.pjdbc:type=CircuitBreaker,name=<name>
Exposed attributes: state, failureCount, successCount, totalRequests, totalFailures, totalRejections, failureRatePercent. Operations: reset(), forceOpen(), forceClosed().
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classCircuit breaker state machine implementation.static enum -
Driver Parameters
URL format:
jdbc:circuitbreaker[param=value,...]:<target-url>Parameter Type Default Constraints Description namestring default- Circuit breaker name for monitoring failureThresholdinteger 5≥ 1 Failures before opening circuit successThresholdinteger 1≥ 1 Successes in half-open to close circuit resetTimeoutinteger 30000≥ 1 Time in ms before open -> half-open Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanacceptsSubName(String subname) protected booleanacceptsSubProtocol(String subprotocol) connect(String url, Properties info) getCircuitBreaker(Connection conn) Get the circuit breaker instance for a connection.protected CallableStatementproxyCallableStatement(CallableStatement delegate, Connection conn) protected ConnectionproxyConnection(Connection delegate, String url, Properties info, Driver driver) protected PreparedStatementproxyPreparedStatement(PreparedStatement delegate, Connection conn) protected StatementproxyStatement(Statement delegate, Connection conn) Methods inherited from class org.pjdbc.sql.AbstractProxyDriver
getPropertyInfo, isCompositionValidationEnabled, isValidateParametersEnabled, proxyCallableStatement, proxyConnection, proxyConnection, proxyPreparedStatement, proxyResultSetMethods inherited from class org.pjdbc.sql.AbstractDriver
acceptsProtocol, acceptsURL, getMajorVersion, getMinorVersion, getParentLogger, getUrlParameter, getUrlParameter, getUrlParameters, jdbcCompliant, parseUrl, protocol, subname, subprotocol, validateParameters
-
Constructor Details
-
CircuitBreakerDriver
public CircuitBreakerDriver()
-
-
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
-
proxyCallableStatement
protected CallableStatement proxyCallableStatement(CallableStatement delegate, Connection conn) throws SQLException - Overrides:
proxyCallableStatementin classAbstractProxyDriver- Throws:
SQLException
-
getCircuitBreaker
Get the circuit breaker instance for a connection. Useful for monitoring circuit state.
-